Skip to content

Commit

Permalink
elevate test coverage to 100%
Browse files Browse the repository at this point in the history
- returns the test coverage of go-polo back to 100%
- adds tests for Key and KeySort
- adds a test for nil byte array decoding
- adds tests for new decode function for all integer sizes
- adds tests for IsNull method of Depolorizer
  • Loading branch information
sarvalabs-manish committed Oct 25, 2024
1 parent 36a8be5 commit 69de527
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 0 deletions.
204 changes: 204 additions & 0 deletions depolorizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,29 @@ func TestDepolorizer_DepolorizeUint(t *testing.T) {
depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value uint

value, err = depolorizer.DepolorizeUint()
assert.Nil(t, err)
assert.Equal(t, uint(300), value)
assert.False(t, depolorizer.Done())

value, err = depolorizer.DepolorizeUint()
assert.Nil(t, err)
assert.Equal(t, uint(250), value)
assert.True(t, depolorizer.Done())

_, err = depolorizer.DepolorizeUint()
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeUint64(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 47, 3, 35, 1, 44, 250})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value uint64

value, err = depolorizer.DepolorizeUint64()
Expand All @@ -272,13 +295,100 @@ func TestDepolorizer_DepolorizeUint(t *testing.T) {
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeUint32(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 47, 3, 35, 1, 44, 250})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value uint32

value, err = depolorizer.DepolorizeUint32()
assert.Nil(t, err)
assert.Equal(t, uint32(300), value)
assert.False(t, depolorizer.Done())

value, err = depolorizer.DepolorizeUint32()
assert.Nil(t, err)
assert.Equal(t, uint32(250), value)
assert.True(t, depolorizer.Done())

_, err = depolorizer.DepolorizeUint32()
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeUint16(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 47, 3, 35, 1, 44, 250})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value uint16

value, err = depolorizer.DepolorizeUint16()
assert.Nil(t, err)
assert.Equal(t, uint16(300), value)
assert.False(t, depolorizer.Done())

value, err = depolorizer.DepolorizeUint16()
assert.Nil(t, err)
assert.Equal(t, uint16(250), value)
assert.True(t, depolorizer.Done())

_, err = depolorizer.DepolorizeUint16()
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeUint8(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 31, 3, 250})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value uint8

value, err = depolorizer.DepolorizeUint8()
assert.Nil(t, err)
assert.Equal(t, uint8(250), value)
assert.True(t, depolorizer.Done())

_, err = depolorizer.DepolorizeUint8()
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeInt(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 47, 3, 36, 1, 44, 250})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value int

value, err = depolorizer.DepolorizeInt()
assert.Nil(t, err)
assert.Equal(t, 300, value)
assert.False(t, depolorizer.Done())

value, err = depolorizer.DepolorizeInt()
assert.Nil(t, err)
assert.Equal(t, -250, value)
assert.True(t, depolorizer.Done())

_, err = depolorizer.DepolorizeInt()
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeInt64(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 47, 3, 36, 1, 44, 250})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value int64

value, err = depolorizer.DepolorizeInt64()
Expand All @@ -295,6 +405,70 @@ func TestDepolorizer_DepolorizeInt(t *testing.T) {
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeInt32(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 47, 3, 36, 1, 44, 250})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value int32

value, err = depolorizer.DepolorizeInt32()
assert.Nil(t, err)
assert.Equal(t, int32(300), value)
assert.False(t, depolorizer.Done())

value, err = depolorizer.DepolorizeInt32()
assert.Nil(t, err)
assert.Equal(t, int32(-250), value)
assert.True(t, depolorizer.Done())

_, err = depolorizer.DepolorizeInt32()
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeInt16(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 47, 3, 36, 1, 44, 250})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value int16

value, err = depolorizer.DepolorizeInt16()
assert.Nil(t, err)
assert.Equal(t, int16(300), value)
assert.False(t, depolorizer.Done())

value, err = depolorizer.DepolorizeInt16()
assert.Nil(t, err)
assert.Equal(t, int16(-250), value)
assert.True(t, depolorizer.Done())

_, err = depolorizer.DepolorizeInt16()
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeInt8(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 31, 4, 100})
require.Nil(t, err)

depolorizer, err = depolorizer.DepolorizePacked()
require.Nil(t, err)

var value int8

value, err = depolorizer.DepolorizeInt8()
assert.Nil(t, err)
assert.Equal(t, int8(-100), value)
assert.True(t, depolorizer.Done())

_, err = depolorizer.DepolorizeInt8()
assert.EqualError(t, err, "insufficient data in wire for decode")
}

func TestDepolorizer_DepolorizeFloat32(t *testing.T) {
depolorizer, err := NewDepolorizer([]byte{14, 47, 7, 71, 66, 246, 233, 121, 194, 199, 250, 225})
require.Nil(t, err)
Expand Down Expand Up @@ -473,6 +647,36 @@ func TestDepolorizer_ZeroValue(t *testing.T) {
})
}

func TestDepolorizer_IsNull(t *testing.T) {
tests := []struct {
name string
data []byte
unpack bool
expected bool
}{
{"NonPacked_Null", []byte{0}, false, true},
{"NonPacked_NotNull", []byte{3, 1, 44}, false, false},
{"Packed_Null", []byte{14, 31, 0}, true, true},
{"Packed_NotNull", []byte{14, 47, 3, 1, 44}, true, false},
{"Packed_Empty", []byte{14, 15}, true, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
depolorizer, err := NewDepolorizer(tt.data)
require.Nil(t, err)

if tt.unpack {
depolorizer, err = depolorizer.Unpacked()
require.Nil(t, err)
}

isNull := depolorizer.IsNull()
assert.Equal(t, tt.expected, isNull)
})
}
}

func TestInsufficientWire(t *testing.T) {
tests := []struct {
name string
Expand Down
10 changes: 10 additions & 0 deletions polo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1832,3 +1832,13 @@ func TestPointerAlias(t *testing.T) {
object := Object{nil, nil}
testSerialization(t, object)
}

func TestNilByteArray(t *testing.T) {
wire := []byte{0}

var x [10]byte

err := Depolorize(&x, wire)
require.NoError(t, err)
require.Equal(t, x, [10]byte{})
}
74 changes: 74 additions & 0 deletions valuesort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,77 @@ func TestValueCmp_Panics(t *testing.T) {
})
})
}

func TestKey(t *testing.T) {
tests := []struct {
name string
idx int
val any
}{
{"Int Key", 0, 42},
{"String Key", 1, "test"},
{"Float Key", 2, 3.14},
{"Bool Key", 3, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key := NewKey(tt.idx, tt.val)
assert.Equal(t, tt.idx, key.Index())
assert.Equal(t, reflect.ValueOf(tt.val), key.val)
})
}
}

func TestKeySort(t *testing.T) {
tests := []struct {
name string
keys []Key
sorted []Key
}{
{
"Int Keys",
[]Key{NewKey(0, 3), NewKey(1, 1), NewKey(2, 2)},
[]Key{NewKey(1, 1), NewKey(2, 2), NewKey(0, 3)},
},
{
"String Keys",
[]Key{NewKey(0, "c"), NewKey(1, "a"), NewKey(2, "b")},
[]Key{NewKey(1, "a"), NewKey(2, "b"), NewKey(0, "c")},
},
{
"Float Keys",
[]Key{NewKey(0, 3.1), NewKey(1, 1.1), NewKey(2, 2.1)},
[]Key{NewKey(1, 1.1), NewKey(2, 2.1), NewKey(0, 3.1)},
},
{
"Bool Keys",
[]Key{NewKey(0, true), NewKey(1, false)},
[]Key{NewKey(1, false), NewKey(0, true)},
},
{
"Single Key",
[]Key{NewKey(0, 42)},
[]Key{NewKey(0, 42)},
},
{
"No Keys",
[]Key{},
nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ch := make(chan Key)
go KeySort(tt.keys, ch)

var sortedKeys []Key
for key := range ch {
sortedKeys = append(sortedKeys, key)
}

assert.Equal(t, tt.sorted, sortedKeys)
})
}
}

0 comments on commit 69de527

Please sign in to comment.