-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarshal_test.go
More file actions
193 lines (171 loc) · 3.61 KB
/
marshal_test.go
File metadata and controls
193 lines (171 loc) · 3.61 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package querybuilder
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type enumType string
func (c enumType) IsEnum() {}
func (c enumType) Name() string { return string(c) }
func (c enumType) Value() string { return string(c) }
var _ enum = enumType("")
func TestMarshalGQL(t *testing.T) {
var (
str = "hello world"
unicode = "∆?–∂∂√˛viÙ˜Ÿ¿GÆÓ∂Ó˘◊ñ" //nolint:staticcheck // ST1018 unicode test string
strNullPtr *string
strPtrSlice = []*string{&str}
enumVal enumType = "test"
)
testCases := []struct {
v any
expect string
}{
{
v: str,
expect: "\"hello world\"",
},
{
v: &str,
expect: "\"hello world\"",
},
{
v: strNullPtr,
expect: "null",
},
{
v: 42,
expect: "42",
},
{
v: true,
expect: "true",
},
{
v: unicode,
expect: "\"∆?–∂∂√˛\\u0007v\\u001CiÙ˜Ÿ¿\\u0011GÆÓ∂Ó˘◊ñ\"",
},
// FIXME
// {
// v: nil,
// expect: "null",
// },
// {
// v: []*string{nil},
// expect: "",
// },
{
v: []string{"1", "2", "3"},
expect: `["1","2","3"]`,
},
{
v: strPtrSlice,
expect: `["hello world"]`,
},
{
v: &strPtrSlice,
expect: `["hello world"]`,
},
{
v: enumVal,
expect: "test",
},
}
for _, testCase := range testCases {
enc, err := MarshalGQL(context.TODO(), testCase.v)
require.NoError(t, err)
require.Equal(t, testCase.expect, enc)
}
}
func TestMarshalGQLStruct(t *testing.T) {
s := struct {
A string `json:"a,omitempty"`
B int `json:"b"`
Sub struct {
X []string `json:"x"`
} `json:"sub"`
}{
A: "test",
B: 42,
}
s.Sub.X = []string{"1"}
enc, err := MarshalGQL(context.TODO(), s)
require.NoError(t, err)
require.Equal(t, `{a:"test",b:42,sub:{x:["1"]}}`, enc)
}
type customMarshaller struct {
v string
count int
}
//nolint:staticcheck // ST1003 XXX_ prefix is part of the GraphQL marshaller interface
func (m *customMarshaller) XXX_GraphQLType() string { return "idTest" }
//nolint:staticcheck // ST1003 XXX_ prefix is part of the GraphQL marshaller interface
func (m *customMarshaller) XXX_GraphQLIDType() string { return "idTypeTest" }
//nolint:staticcheck // ST1003 XXX_ prefix is part of the GraphQL marshaller interface
func (m *customMarshaller) XXX_GraphQLID(context.Context) (string, error) {
m.count++
return m.v, nil
}
func (m *customMarshaller) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
V string `json:"v"`
Count int `json:"count"`
}{m.v, m.count})
}
var _ GraphQLMarshaller = &customMarshaller{}
func TestCustomMarshaller(t *testing.T) {
testCases := []struct {
v any
expect string
}{
{
v: &customMarshaller{v: "custom"},
expect: `"custom"`,
},
{
v: []*customMarshaller{
{v: "custom1"},
{v: "custom2"},
},
expect: `["custom1","custom2"]`,
},
}
for _, testCase := range testCases {
enc, err := MarshalGQL(context.TODO(), testCase.v)
require.NoError(t, err)
require.Equal(t, testCase.expect, enc)
}
}
func TestIsZeroValue(t *testing.T) {
// emptyPtr covers the case of nil reflect.Pointer:
var emptyPtr *string
zero := []any{
"",
0,
[]string{},
struct {
Foo string
}{},
emptyPtr,
}
stringPtr := "test"
nonZero := []any{
"hello",
42,
[]string{"world"},
struct {
Foo string
}{
Foo: "bar",
},
&stringPtr,
}
for _, i := range zero {
require.True(t, IsZeroValue(i), fmt.Sprintf("%v", i))
}
for _, i := range nonZero {
require.False(t, IsZeroValue(i), fmt.Sprintf("%v", i))
}
}