@@ -2,11 +2,14 @@ package option_test
22
33import (
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
1215func TestAny_IsSome (t * testing.T ) {
@@ -167,41 +170,64 @@ func TestAny_UnwrapOrElse(t *testing.T) {
167170func 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}
0 commit comments