Skip to content

Commit 4eba6c0

Browse files
author
New year
committed
addtypes: adding test for type Any
1 parent 9d9e575 commit 4eba6c0

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

any_gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (o *Any) DecodeMsgpack(decoder *msgpack.Decoder) error {
8686
o.exists = false
8787

8888
return newDecodeError("Any", decoder.Skip())
89-
case checkBool(code):
89+
case checkAny(code):
9090
o.value, err = decodeAny(decoder)
9191
if err != nil {
9292
return newDecodeError("Any", err)

any_gen test.go renamed to any_gen_test.go

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package option_test
22

33
import (
44
"bytes"
5+
"log"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
"github.com/vmihailenco/msgpack/v5"
11+
12+
"github.com/tarantool/go-option"
1013
)
1114

1215
func TestAny_IsSome(t *testing.T) {
@@ -167,41 +170,64 @@ func TestAny_UnwrapOrElse(t *testing.T) {
167170
func TestAny_EncodeDecodeMsgpack(t *testing.T) {
168171
t.Parallel()
169172

170-
t.Run("some", func(t *testing.T) {
171-
t.Parallel()
172-
173-
var buf bytes.Buffer
174-
175-
enc := msgpack.NewEncoder(&buf)
176-
dec := msgpack.NewDecoder(&buf)
177-
178-
someAny := option.SomeAny(222222.11111)
179-
err := someAny.EncodeMsgpack(enc)
180-
require.NoError(t, err)
181-
182-
var unmarshaled option.Any
183-
err = unmarshaled.DecodeMsgpack(dec)
184-
require.NoError(t, err)
185-
assert.True(t, unmarshaled.IsSome())
186-
assert.EqualValues(t, 222222.11111, unmarshaled.Unwrap())
187-
})
173+
testCases := []struct {
174+
name string
175+
value any
176+
expected any
177+
}{
178+
{"string", "test string", "test string"},
179+
{"int", 42, 42},
180+
{"float", 3.14, 3.14},
181+
{"bool", true, true},
182+
{"slice", []int{1, 2, 3}, []any{1, 2, 3}},
183+
{"map", map[string]int{"a": 1}, map[string]any{"a": 1}},
184+
}
185+
186+
for _, tc := range testCases {
187+
tc := tc
188+
t.Run("some_"+tc.name, func(t *testing.T) {
189+
t.Parallel()
190+
191+
var buf bytes.Buffer
192+
enc := msgpack.NewEncoder(&buf)
193+
dec := msgpack.NewDecoder(&buf)
194+
195+
// Encode
196+
someAny := option.SomeAny(tc.value)
197+
err := someAny.EncodeMsgpack(enc)
198+
require.NoError(t, err)
199+
200+
// Decode
201+
var unmarshaled option.Any
202+
err = unmarshaled.DecodeMsgpack(dec)
203+
log.Println("someAny", someAny)
204+
require.NoError(t, err)
205+
log.Println("UNwrap", unmarshaled.Unwrap())
206+
// Verify
207+
assert.True(t, unmarshaled.IsSome())
208+
assert.Equal(t, tc.expected, unmarshaled.Unwrap())
209+
})
210+
}
188211

189212
t.Run("none", func(t *testing.T) {
190213
t.Parallel()
191214

192215
var buf bytes.Buffer
193-
194216
enc := msgpack.NewEncoder(&buf)
195217
dec := msgpack.NewDecoder(&buf)
196218

219+
// Encode nil
197220
emptyAny := option.NoneAny()
198221
err := emptyAny.EncodeMsgpack(enc)
199222
require.NoError(t, err)
200223

224+
// Decode
201225
var unmarshaled option.Any
202226
err = unmarshaled.DecodeMsgpack(dec)
203-
204227
require.NoError(t, err)
228+
229+
// Verify it's none
205230
assert.False(t, unmarshaled.IsSome())
231+
assert.Nil(t, unmarshaled.Unwrap())
206232
})
207233
}

msgpack.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ func encodeByte(encoder *msgpack.Encoder, b byte) error {
168168
return encoder.EncodeUint8(b) //nolint:wrapcheck
169169
}
170170

171+
func checkAny(code byte) bool{
172+
return code != msgpcode.Nil
173+
}
171174
func encodeAny(encoder *msgpack.Encoder, val any) error {
172175
return encoder.Encode(val)
173176
}

0 commit comments

Comments
 (0)