...
1 package graphql
2
3 import (
4 "bytes"
5 "math"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 )
10
11 func TestMarshalID(t *testing.T) {
12 marshalID := func(s string) string {
13 var buf bytes.Buffer
14 MarshalID(s).MarshalGQL(&buf)
15 return buf.String()
16 }
17
18 assert.Equal(t, `"hello"`, marshalID("hello"))
19 assert.Equal(t, `"he\tllo"`, marshalID("he\tllo"))
20 assert.Equal(t, `"he\tllo"`, marshalID("he llo"))
21 assert.Equal(t, `"he\nllo"`, marshalID("he\nllo"))
22 assert.Equal(t, `"he\r\nllo"`, marshalID("he\r\nllo"))
23 assert.Equal(t, `"he\\llo"`, marshalID(`he\llo`))
24 assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, marshalID(`quotes"nested"in"quotes"`))
25 assert.Equal(t, `"\u0000"`, marshalID("\u0000"))
26 assert.Equal(t, "\"\U000fe4ed\"", marshalID("\U000fe4ed"))
27 assert.Equal(t, "\"\\u001B\"", marshalID("\u001B"))
28 }
29
30 func TestUnmarshalID(t *testing.T) {
31 tests := []struct {
32 Name string
33 Input interface{}
34 Expected string
35 ShouldError bool
36 }{
37 {
38 Name: "int64",
39 Input: int64(12),
40 Expected: "12",
41 ShouldError: false,
42 },
43 {
44 Name: "int64 max",
45 Input: math.MaxInt64,
46 Expected: "9223372036854775807",
47 },
48 {
49 Name: "int64 min",
50 Input: math.MinInt64,
51 Expected: "-9223372036854775808",
52 },
53 }
54
55 for _, tt := range tests {
56 t.Run(tt.Name, func(t *testing.T) {
57 id, err := UnmarshalID(tt.Input)
58
59 assert.Equal(t, tt.Expected, id)
60 if tt.ShouldError {
61 assert.Error(t, err)
62 } else {
63 assert.NoError(t, err)
64 }
65 })
66 }
67 }
68
View as plain text