github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/json/jsonutil/build_test.go (about) 1 package jsonutil_test 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/aavshr/aws-sdk-go/private/protocol/json/jsonutil" 10 ) 11 12 func S(s string) *string { 13 return &s 14 } 15 16 func D(s int64) *int64 { 17 return &s 18 } 19 20 func F(s float64) *float64 { 21 return &s 22 } 23 24 func T(s time.Time) *time.Time { 25 return &s 26 } 27 28 type J struct { 29 S *string 30 SS []string 31 D *int64 32 F *float64 33 T *time.Time 34 } 35 36 var zero = 0.0 37 38 var jsonTests = []struct { 39 in interface{} 40 out string 41 err string 42 }{ 43 { 44 J{}, 45 `{}`, 46 ``, 47 }, 48 { 49 J{ 50 S: S("str"), 51 SS: []string{"A", "B", "C"}, 52 D: D(123), 53 F: F(4.56), 54 T: T(time.Unix(987, 0)), 55 }, 56 `{"S":"str","SS":["A","B","C"],"D":123,"F":4.56,"T":987}`, 57 ``, 58 }, 59 { 60 J{ 61 S: S(`"''"`), 62 }, 63 `{"S":"\"''\""}`, 64 ``, 65 }, 66 { 67 J{ 68 S: S("\x00føø\u00FF\n\\\"\r\t\b\f"), 69 }, 70 `{"S":"\u0000føøÿ\n\\\"\r\t\b\f"}`, 71 ``, 72 }, 73 { 74 J{ 75 F: F(4.56 / zero), 76 }, 77 "", 78 `json: unsupported value: +Inf`, 79 }, 80 } 81 82 func TestBuildJSON(t *testing.T) { 83 for _, test := range jsonTests { 84 out, err := jsonutil.BuildJSON(test.in) 85 if test.err != "" { 86 if err == nil { 87 t.Errorf("expect error") 88 } 89 if e, a := test.err, err.Error(); !strings.Contains(a, e) { 90 t.Errorf("expect %v, to contain %v", e, a) 91 } 92 } else { 93 if err != nil { 94 t.Errorf("expect nil, %v", err) 95 } 96 if e, a := string(out), test.out; e != a { 97 t.Errorf("expect %v, got %v", e, a) 98 } 99 } 100 } 101 } 102 103 func BenchmarkBuildJSON(b *testing.B) { 104 for i := 0; i < b.N; i++ { 105 for _, test := range jsonTests { 106 jsonutil.BuildJSON(test.in) 107 } 108 } 109 } 110 111 func BenchmarkStdlibJSON(b *testing.B) { 112 for i := 0; i < b.N; i++ { 113 for _, test := range jsonTests { 114 json.Marshal(test.in) 115 } 116 } 117 }