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