forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_test.go
172 lines (157 loc) · 3.5 KB
/
id_test.go
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
package graphql
import (
"bytes"
"encoding/json"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMarshalID(t *testing.T) {
marshalID := func(s string) string {
var buf bytes.Buffer
MarshalID(s).MarshalGQL(&buf)
return buf.String()
}
assert.Equal(t, `"hello"`, marshalID("hello"))
assert.Equal(t, `"he\tllo"`, marshalID("he\tllo"))
assert.Equal(t, `"he\tllo"`, marshalID("he llo"))
assert.Equal(t, `"he\nllo"`, marshalID("he\nllo"))
assert.Equal(t, `"he\r\nllo"`, marshalID("he\r\nllo"))
assert.Equal(t, `"he\\llo"`, marshalID(`he\llo`))
assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, marshalID(`quotes"nested"in"quotes"`))
assert.Equal(t, `"\u0000"`, marshalID("\u0000"))
assert.Equal(t, "\"\U000fe4ed\"", marshalID("\U000fe4ed"))
assert.Equal(t, "\"\\u001B\"", marshalID("\u001B"))
}
func TestUnmarshalID(t *testing.T) {
tests := []struct {
Name string
Input any
Expected string
ShouldError bool
}{
{
Name: "string",
Input: "str",
Expected: "str",
},
{
Name: "json.Number float64",
Input: json.Number("1.2"),
Expected: "1.2",
},
{
Name: "int64",
Input: int64(12),
Expected: "12",
},
{
Name: "int64 max",
Input: math.MaxInt64,
Expected: "9223372036854775807",
},
{
Name: "int64 min",
Input: math.MinInt64,
Expected: "-9223372036854775808",
},
{
Name: "bool true",
Input: true,
Expected: "true",
},
{
Name: "bool false",
Input: false,
Expected: "false",
},
{
Name: "nil",
Input: nil,
Expected: "null",
},
{
Name: "float64",
Input: 1.234567,
Expected: "1.234567",
},
{
Name: "float64 0",
Input: 0.0,
Expected: "0.000000",
},
{
Name: "float64 loss of precision",
Input: 0.0000005,
Expected: "0.000000",
},
{
Name: "float64 rounding up",
Input: 0.0000006,
Expected: "0.000001",
},
{
Name: "float64 negative",
Input: -1.234560,
Expected: "-1.234560",
},
{
Name: "float64 math.Inf(0)",
Input: math.Inf(0),
Expected: "+Inf",
},
{
Name: "float64 math.Inf(-1)",
Input: math.Inf(-1),
Expected: "-Inf",
},
{
Name: "float64 -math.Inf(0)",
Input: -math.Inf(0),
Expected: "-Inf",
},
{
Name: "not a string",
Input: struct{}{},
ShouldError: true,
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
id, err := UnmarshalID(tt.Input)
assert.Equal(t, tt.Expected, id)
if tt.ShouldError {
assert.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
func TestMarshalUintID(t *testing.T) {
assert.Equal(t, `"12"`, m2s(MarshalUintID(12)))
}
func TestUnMarshalUintID(t *testing.T) {
result, err := UnmarshalUintID("12")
assert.Equal(t, uint(12), result)
require.NoError(t, err)
result, err = UnmarshalUintID(12)
assert.Equal(t, uint(12), result)
require.NoError(t, err)
result, err = UnmarshalUintID(int64(12))
assert.Equal(t, uint(12), result)
require.NoError(t, err)
result, err = UnmarshalUintID(int32(12))
assert.Equal(t, uint(12), result)
require.NoError(t, err)
result, err = UnmarshalUintID(int(12))
assert.Equal(t, uint(12), result)
require.NoError(t, err)
result, err = UnmarshalUintID(uint32(12))
assert.Equal(t, uint(12), result)
require.NoError(t, err)
result, err = UnmarshalUintID(uint64(12))
assert.Equal(t, uint(12), result)
require.NoError(t, err)
}