github.com/msales/pkg/v3@v3.24.0/cache/item_internal_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestItem_Bool(t *testing.T) {
    11  	decoder := stringDecoder{}
    12  	tests := []struct {
    13  		item   Item
    14  		ok     bool
    15  		expect bool
    16  	}{
    17  		{Item{decoder: decoder, value: []byte("1")}, true, true},
    18  		{Item{decoder: decoder, err: errors.New("")}, false, false},
    19  	}
    20  
    21  	for i, tt := range tests {
    22  		got, err := tt.item.Bool()
    23  		if ok := (err == nil); ok != tt.ok {
    24  			if err != nil {
    25  				assert.FailNow(t, "test %d, unexpected failure: %v", i, err)
    26  			} else {
    27  				assert.FailNow(t, "test %d, unexpected success", i)
    28  			}
    29  		}
    30  
    31  		assert.Equal(t, tt.expect, got)
    32  	}
    33  }
    34  
    35  func TestItem_Bytes(t *testing.T) {
    36  	tests := []struct {
    37  		item   Item
    38  		ok     bool
    39  		expect []byte
    40  	}{
    41  		{Item{value: []byte{0x01}}, true, []byte{0x01}},
    42  		{Item{err: errors.New("")}, false, nil},
    43  	}
    44  
    45  	for i, tt := range tests {
    46  		got, err := tt.item.Bytes()
    47  		if ok := (err == nil); ok != tt.ok {
    48  			if err != nil {
    49  				assert.FailNow(t, "test %d, unexpected failure: %v", i, err)
    50  			} else {
    51  				assert.FailNow(t, "test %d, unexpected success", i)
    52  			}
    53  		}
    54  
    55  		assert.Equal(t, tt.expect, got)
    56  	}
    57  }
    58  
    59  func TestItem_String(t *testing.T) {
    60  	tests := []struct {
    61  		item   Item
    62  		ok     bool
    63  		expect string
    64  	}{
    65  		{Item{value: []byte("hello")}, true, "hello"},
    66  		{Item{err: errors.New("")}, false, ""},
    67  	}
    68  
    69  	for i, tt := range tests {
    70  		got, err := tt.item.String()
    71  		if ok := (err == nil); ok != tt.ok {
    72  			if err != nil {
    73  				assert.FailNow(t, "test %d, unexpected failure: %v", i, err)
    74  			} else {
    75  				assert.FailNow(t, "test %d, unexpected success", i)
    76  			}
    77  		}
    78  
    79  		assert.Equal(t, tt.expect, got)
    80  	}
    81  }
    82  
    83  func TestItem_Int64(t *testing.T) {
    84  	decoder := stringDecoder{}
    85  	tests := []struct {
    86  		item   Item
    87  		ok     bool
    88  		expect int64
    89  	}{
    90  		{Item{decoder: decoder, value: []byte("1")}, true, 1},
    91  		{Item{decoder: decoder, value: []byte("a")}, false, 0},
    92  		{Item{decoder: decoder, err: errors.New("")}, false, 0},
    93  	}
    94  
    95  	for i, tt := range tests {
    96  		got, err := tt.item.Int64()
    97  		if ok := (err == nil); ok != tt.ok {
    98  			if err != nil {
    99  				assert.FailNow(t, "test %d, unexpected failure: %v", i, err)
   100  			} else {
   101  				assert.FailNow(t, "test %d, unexpected success", i)
   102  			}
   103  		}
   104  
   105  		assert.Equal(t, tt.expect, got)
   106  	}
   107  }
   108  
   109  func TestItem_Uint64(t *testing.T) {
   110  	decoder := stringDecoder{}
   111  	tests := []struct {
   112  		item   Item
   113  		ok     bool
   114  		expect uint64
   115  	}{
   116  		{Item{decoder: decoder, value: []byte("1")}, true, 1},
   117  		{Item{decoder: decoder, value: []byte("a")}, false, 0},
   118  		{Item{decoder: decoder, err: errors.New("")}, false, 0},
   119  	}
   120  
   121  	for i, tt := range tests {
   122  		got, err := tt.item.Uint64()
   123  		if ok := (err == nil); ok != tt.ok {
   124  			if err != nil {
   125  				assert.FailNow(t, "test %d, unexpected failure: %v", i, err)
   126  			} else {
   127  				assert.FailNow(t, "test %d, unexpected success", i)
   128  			}
   129  		}
   130  
   131  		assert.Equal(t, tt.expect, got)
   132  	}
   133  }
   134  
   135  func TestItem_Float64(t *testing.T) {
   136  	decoder := stringDecoder{}
   137  	tests := []struct {
   138  		item   Item
   139  		ok     bool
   140  		expect float64
   141  	}{
   142  		{Item{decoder: decoder, value: []byte("1.2")}, true, 1.2},
   143  		{Item{decoder: decoder, value: []byte("a")}, false, 0},
   144  		{Item{decoder: decoder, err: errors.New("")}, false, 0},
   145  	}
   146  
   147  	for i, tt := range tests {
   148  		got, err := tt.item.Float64()
   149  		if ok := (err == nil); ok != tt.ok {
   150  			if err != nil {
   151  				assert.FailNow(t, "test %d, unexpected failure: %v", i, err)
   152  			} else {
   153  				assert.FailNow(t, "test %d, unexpected success", i)
   154  			}
   155  		}
   156  
   157  		assert.Equal(t, tt.expect, got)
   158  	}
   159  }
   160  
   161  func TestItem_Err(t *testing.T) {
   162  	expect := errors.New("err")
   163  	tests := []struct {
   164  		item   Item
   165  		expect error
   166  	}{
   167  		{Item{}, nil},
   168  		{Item{err: expect}, expect},
   169  	}
   170  
   171  	for _, tt := range tests {
   172  		err := tt.item.Err()
   173  
   174  		assert.Equal(t, tt.expect, err)
   175  	}
   176  }