forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
136 lines (125 loc) · 3.07 KB
/
Copy pathjson_test.go
File metadata and controls
136 lines (125 loc) · 3.07 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package ogen
import (
"net"
"net/url"
"testing"
"time"
"github.com/go-faster/jx"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/ogen-go/ogen/conv"
api "github.com/ogen-go/ogen/internal/sample_api"
"github.com/ogen-go/ogen/internal/techempower"
"github.com/ogen-go/ogen/json"
)
func decodeObject(t testing.TB, data []byte, v json.Unmarshaler) {
d := jx.GetDecoder()
d.ResetBytes(data)
defer jx.PutDecoder(d)
if rs, ok := v.(json.Resettable); ok {
rs.Reset()
}
require.NoError(t, d.ObjBytes(func(d *jx.Decoder, _ []byte) error {
return v.Decode(d)
}))
}
func encodeObject(v json.Marshaler) []byte {
e := jx.GetEncoder()
e.ObjStart()
if settable, ok := v.(json.Settable); ok && !settable.IsSet() {
e.ObjEnd()
return e.Bytes()
}
e.FieldStart("key")
v.Encode(e)
e.ObjEnd()
return e.Bytes()
}
func TestJSONGenerics(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
Name string
Value api.OptNilString
Result string
}{
{
Name: "Zero",
Result: "{}",
},
{
Name: "Set",
Result: `{"key":"foo"}`,
Value: api.NewOptNilString("foo"),
},
{
Name: "Nil",
Result: `{"key":null}`,
Value: api.OptNilString{Null: true, Set: true},
},
} {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
result := encodeObject(tc.Value)
require.Equal(t, tc.Result, string(result), "encoding result mismatch")
var v api.OptNilString
decodeObject(t, result, &v)
require.Equal(t, tc.Value, v)
require.Equal(t, tc.Result, string(encodeObject(v)))
})
}
}
func TestJSONExample(t *testing.T) {
t.Parallel()
date := time.Date(2011, 10, 10, 7, 12, 34, 4125, time.UTC)
pet := api.Pet{
Friends: []api.Pet{},
Birthday: conv.Date(date),
ID: 42,
Name: "SomePet",
TestArray1: [][]string{
{
"Foo", "Bar",
},
{
"Baz",
},
},
Nickname: api.NewNilString("Nick"),
NullStr: api.NewOptNilString("Bar"),
Rate: time.Second,
Tag: api.NewOptUUID(uuid.New()),
TestDate: api.NewOptTime(conv.Date(date)),
TestDateTime: api.NewOptTime(conv.DateTime(date)),
TestDuration: api.NewOptDuration(time.Minute),
TestFloat1: api.NewOptFloat64(1.0),
TestInteger1: api.NewOptInt(10),
TestTime: api.NewOptTime(conv.Time(date)),
UniqueID: uuid.New(),
URI: url.URL{Scheme: "s3", Host: "foo", Path: "bar"},
IP: net.IPv4(127, 0, 0, 1),
IPV4: net.IPv4(127, 0, 0, 1),
IPV6: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
Next: api.NewOptData(api.Data{
Description: api.NewDescriptionSimpleDataDescription(api.DescriptionSimple{
Description: "foo",
}),
ID: api.NewIntID(10),
}),
}
t.Logf("%s", json.Encode(pet))
require.True(t, jx.Valid(json.Encode(pet)), "invalid json")
}
func TestTechEmpowerJSON(t *testing.T) {
hw := techempower.WorldObject{
ID: 10,
RandomNumber: 2134,
}
e := jx.GetEncoder()
hw.Encode(e)
var parsed techempower.WorldObject
d := jx.GetDecoder()
d.ResetBytes(e.Bytes())
t.Log(e)
require.NoError(t, parsed.Decode(d))
require.Equal(t, hw, parsed)
}