github.com/philpearl/plenc@v0.0.15/plenccodec/wrapper_test.go (about) 1 package plenccodec_test 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/philpearl/plenc" 8 ) 9 10 func TestSliceIntPtr(t *testing.T) { 11 one := 1 12 two := 2 13 tests := []struct { 14 name string 15 input []*int 16 exp []*int 17 }{ 18 {name: "empty slices look nil", input: []*int{}, exp: nil}, // empty slices come back as nil 19 {name: "slices work", input: []*int{&one, &two}, exp: []*int{&one, &two}}, 20 // TODO: is it right that these get dropped? or should they error? 21 // We've basically chosen to silently drop these to avoid checking! 22 {name: "nil pointers not allowed", input: []*int{&one, nil}, exp: []*int{&one}}, 23 {name: "nil pointers not allowed 2", input: []*int{&one, nil, &two}, exp: []*int{&one, &two}}, 24 } 25 for _, test := range tests { 26 t.Run(test.name, func(t *testing.T) { 27 data, err := plenc.Marshal(nil, &test.input) 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 var out []*int 33 if err := plenc.Unmarshal(data, &out); err != nil { 34 t.Fatal(err) 35 } 36 37 if diff := cmp.Diff(test.exp, out); diff != "" { 38 t.Fatalf("Not as expected. %s\n%x", diff, data) 39 } 40 }) 41 } 42 } 43 44 func TestSliceInt(t *testing.T) { 45 tests := []struct { 46 name string 47 input []int 48 exp []int 49 }{ 50 {name: "empty slices look nil", input: []int{}, exp: nil}, // empty slices come back as nil 51 {name: "slices work", input: []int{1, 2}, exp: []int{1, 2}}, 52 // TODO: is it right that these get dropped? or should they error? 53 // We've basically chosen to silently drop these to avoid checking! 54 {name: "nil pointers not allowed", input: []int{1, 0}, exp: []int{1, 0}}, 55 {name: "nil pointers not allowed 2", input: []int{1, 0, 2}, exp: []int{1, 0, 2}}, 56 } 57 for _, test := range tests { 58 t.Run(test.name, func(t *testing.T) { 59 data, err := plenc.Marshal(nil, &test.input) 60 if err != nil { 61 t.Fatal(err) 62 } 63 64 var out []int 65 if err := plenc.Unmarshal(data, &out); err != nil { 66 t.Fatal(err) 67 } 68 69 if diff := cmp.Diff(test.exp, out); diff != "" { 70 t.Fatalf("Not as expected. %s\n%x", diff, data) 71 } 72 }) 73 } 74 } 75 76 func TestSliceFloat(t *testing.T) { 77 tests := []struct { 78 name string 79 input []float64 80 exp []float64 81 }{ 82 {name: "empty slices look nil", input: []float64{}, exp: nil}, // empty slices come back as nil 83 {name: "slices work", input: []float64{1, 2}, exp: []float64{1, 2}}, 84 // TODO: is it right that these get dropped? or should they error? 85 // We've basically chosen to silently drop these to avoid checking! 86 {name: "nil pointers not allowed", input: []float64{1, 0}, exp: []float64{1, 0}}, 87 {name: "nil pointers not allowed 2", input: []float64{1, 0, 2}, exp: []float64{1, 0, 2}}, 88 } 89 for _, test := range tests { 90 t.Run(test.name, func(t *testing.T) { 91 data, err := plenc.Marshal(nil, &test.input) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 var out []float64 97 if err := plenc.Unmarshal(data, &out); err != nil { 98 t.Fatal(err) 99 } 100 101 if diff := cmp.Diff(test.exp, out); diff != "" { 102 t.Fatalf("Not as expected. %s\n%x", diff, data) 103 } 104 }) 105 } 106 }