github.com/hdt3213/godis@v1.2.9/redis/protocol/asserts/assert.go (about)

     1  package asserts
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hdt3213/godis/interface/redis"
     6  	"github.com/hdt3213/godis/lib/utils"
     7  	"github.com/hdt3213/godis/redis/protocol"
     8  	"runtime"
     9  	"testing"
    10  )
    11  
    12  // AssertIntReply checks if the given redis.Reply is the expected integer
    13  func AssertIntReply(t *testing.T, actual redis.Reply, expected int) {
    14  	intResult, ok := actual.(*protocol.IntReply)
    15  	if !ok {
    16  		t.Errorf("expected int protocol, actually %s, %s", actual.ToBytes(), printStack())
    17  		return
    18  	}
    19  	if intResult.Code != int64(expected) {
    20  		t.Errorf("expected %d, actually %d, %s", expected, intResult.Code, printStack())
    21  	}
    22  }
    23  
    24  func AssertIntReplyGreaterThan(t *testing.T, actual redis.Reply, expected int) {
    25  	intResult, ok := actual.(*protocol.IntReply)
    26  	if !ok {
    27  		t.Errorf("expected int protocol, actually %s, %s", actual.ToBytes(), printStack())
    28  		return
    29  	}
    30  	if intResult.Code < int64(expected) {
    31  		t.Errorf("expected %d, actually %d, %s", expected, intResult.Code, printStack())
    32  	}
    33  }
    34  
    35  // AssertBulkReply checks if the given redis.Reply is the expected string
    36  func AssertBulkReply(t *testing.T, actual redis.Reply, expected string) {
    37  	bulkReply, ok := actual.(*protocol.BulkReply)
    38  	if !ok {
    39  		t.Errorf("expected bulk protocol, actually %s, %s", actual.ToBytes(), printStack())
    40  		return
    41  	}
    42  	if !utils.BytesEquals(bulkReply.Arg, []byte(expected)) {
    43  		t.Errorf("expected %s, actually %s, %s", expected, actual.ToBytes(), printStack())
    44  	}
    45  }
    46  
    47  // AssertStatusReply checks if the given redis.Reply is the expected status
    48  func AssertStatusReply(t *testing.T, actual redis.Reply, expected string) {
    49  	statusReply, ok := actual.(*protocol.StatusReply)
    50  	if !ok {
    51  		// may be a protocol.OkReply e.g.
    52  		expectBytes := protocol.MakeStatusReply(expected).ToBytes()
    53  		if utils.BytesEquals(actual.ToBytes(), expectBytes) {
    54  			return
    55  		}
    56  		t.Errorf("expected bulk protocol, actually %s, %s", actual.ToBytes(), printStack())
    57  		return
    58  	}
    59  	if statusReply.Status != expected {
    60  		t.Errorf("expected %s, actually %s, %s", expected, actual.ToBytes(), printStack())
    61  	}
    62  }
    63  
    64  // AssertErrReply checks if the given redis.Reply is the expected error
    65  func AssertErrReply(t *testing.T, actual redis.Reply, expected string) {
    66  	errReply, ok := actual.(protocol.ErrorReply)
    67  	if !ok {
    68  		expectBytes := protocol.MakeErrReply(expected).ToBytes()
    69  		if utils.BytesEquals(actual.ToBytes(), expectBytes) {
    70  			return
    71  		}
    72  		t.Errorf("expected err protocol, actually %s, %s", actual.ToBytes(), printStack())
    73  		return
    74  	}
    75  	if errReply.Error() != expected {
    76  		t.Errorf("expected %s, actually %s, %s", expected, actual.ToBytes(), printStack())
    77  	}
    78  }
    79  
    80  // AssertNotError checks if the given redis.Reply is not error protocol
    81  func AssertNotError(t *testing.T, result redis.Reply) {
    82  	if result == nil {
    83  		t.Errorf("result is nil %s", printStack())
    84  		return
    85  	}
    86  	bytes := result.ToBytes()
    87  	if len(bytes) == 0 {
    88  		t.Errorf("result is empty %s", printStack())
    89  		return
    90  	}
    91  	if bytes[0] == '-' {
    92  		t.Errorf("result is err protocol %s", printStack())
    93  	}
    94  }
    95  
    96  // AssertNullBulk checks if the given redis.Reply is protocol.NullBulkReply
    97  func AssertNullBulk(t *testing.T, result redis.Reply) {
    98  	if result == nil {
    99  		t.Errorf("result is nil %s", printStack())
   100  		return
   101  	}
   102  	bytes := result.ToBytes()
   103  	if len(bytes) == 0 {
   104  		t.Errorf("result is empty %s", printStack())
   105  		return
   106  	}
   107  	expect := (&protocol.NullBulkReply{}).ToBytes()
   108  	if !utils.BytesEquals(expect, bytes) {
   109  		t.Errorf("result is not null-bulk-protocol %s", printStack())
   110  	}
   111  }
   112  
   113  // AssertMultiBulkReply checks if the given redis.Reply has the expected content
   114  func AssertMultiBulkReply(t *testing.T, actual redis.Reply, expected []string) {
   115  	multiBulk, ok := actual.(*protocol.MultiBulkReply)
   116  	if !ok {
   117  		t.Errorf("expected bulk protocol, actually %s, %s", actual.ToBytes(), printStack())
   118  		return
   119  	}
   120  	if len(multiBulk.Args) != len(expected) {
   121  		t.Errorf("expected %d elements, actually %d, %s",
   122  			len(expected), len(multiBulk.Args), printStack())
   123  		return
   124  	}
   125  	for i, v := range multiBulk.Args {
   126  		str := string(v)
   127  		if str != expected[i] {
   128  			t.Errorf("expected %s, actually %s, %s", expected[i], actual, printStack())
   129  		}
   130  	}
   131  }
   132  
   133  // AssertMultiBulkReplySize check if redis.Reply has expected length
   134  func AssertMultiBulkReplySize(t *testing.T, actual redis.Reply, expected int) {
   135  	multiBulk, ok := actual.(*protocol.MultiBulkReply)
   136  	if !ok {
   137  		if expected == 0 &&
   138  			utils.BytesEquals(actual.ToBytes(), protocol.MakeEmptyMultiBulkReply().ToBytes()) {
   139  			return
   140  		}
   141  		t.Errorf("expected bulk protocol, actually %s, %s", actual.ToBytes(), printStack())
   142  		return
   143  	}
   144  	if len(multiBulk.Args) != expected {
   145  		t.Errorf("expected %d elements, actually %d, %s", expected, len(multiBulk.Args), printStack())
   146  		return
   147  	}
   148  }
   149  
   150  func printStack() string {
   151  	_, file, no, ok := runtime.Caller(2)
   152  	if ok {
   153  		return fmt.Sprintf("at %s:%d", file, no)
   154  	}
   155  	return ""
   156  }