tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/codec/codec_test.go (about) 1 package codec 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 type testData struct { 9 Map map[string]bool 10 Arr []int 11 } 12 13 func TestJSONCodec(t *testing.T) { 14 c := &JSONCodec{} 15 var buf bytes.Buffer 16 17 if err := c.Encoder(&buf).Encode(testData{ 18 Map: map[string]bool{"true": true, "false": false}, 19 Arr: []int{1, 2, 3}, 20 }); err != nil { 21 t.Fatal(err) 22 } 23 24 var data testData 25 if err := c.Decoder(&buf).Decode(&data); err != nil { 26 t.Fatal(err) 27 } 28 29 if data.Map["true"] != true || data.Arr[2] != 3 { 30 t.Fatal("unexpected data:", data) 31 } 32 } 33 34 func TestCBORCodec(t *testing.T) { 35 c := &CBORCodec{} 36 var buf bytes.Buffer 37 38 if err := c.Encoder(&buf).Encode(testData{ 39 Map: map[string]bool{"true": true, "false": false}, 40 Arr: []int{1, 2, 3}, 41 }); err != nil { 42 t.Fatal(err) 43 } 44 45 var data testData 46 if err := c.Decoder(&buf).Decode(&data); err != nil { 47 t.Fatal(err) 48 } 49 50 if data.Map["true"] != true || data.Arr[2] != 3 { 51 t.Fatal("unexpected data:", data) 52 } 53 }