-
Notifications
You must be signed in to change notification settings - Fork 90
/
scalar_test.go
126 lines (120 loc) · 3.26 KB
/
scalar_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
package githubv4_test
import (
"encoding/json"
"errors"
"net/url"
"reflect"
"testing"
"github.com/shurcooL/githubv4"
)
func TestURI_MarshalJSON(t *testing.T) {
tests := []struct {
name string
in githubv4.URI
want string
}{
{
in: githubv4.URI{URL: &url.URL{Scheme: "https", Host: "example.org", Path: "/foo/bar"}},
want: `"https://example.org/foo/bar"`,
},
}
for _, tc := range tests {
got, err := json.Marshal(tc.in)
if err != nil {
t.Fatalf("%s: got error: %v", tc.name, err)
}
if string(got) != tc.want {
t.Errorf("%s: got: %q, want: %q", tc.name, string(got), tc.want)
}
}
}
func TestURI_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
in string
want githubv4.URI
wantError error
}{
{
in: `"https://example.org/foo/bar"`,
want: githubv4.URI{URL: &url.URL{Scheme: "https", Host: "example.org", Path: "/foo/bar"}},
},
{
name: "null",
in: `null`,
want: githubv4.URI{},
},
{
name: "error JSON unmarshaling into string",
in: `86`,
wantError: errors.New("json: cannot unmarshal number into Go value of type string"),
},
}
for _, tc := range tests {
var got githubv4.URI
err := json.Unmarshal([]byte(tc.in), &got)
if got, want := err, tc.wantError; !equalError(got, want) {
t.Fatalf("%s: got error: %v, want: %v", tc.name, got, want)
}
if tc.wantError != nil {
continue
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("%s: got: %v, want: %v", tc.name, got, tc.want)
}
}
}
// equalError reports whether errors a and b are considered equal.
// They're equal if both are nil, or both are not nil and a.Error() == b.Error().
func equalError(a, b error) bool {
return a == nil && b == nil || a != nil && b != nil && a.Error() == b.Error()
}
func TestNewScalars(t *testing.T) {
if got := githubv4.NewBase64String(""); got == nil {
t.Error("NewBase64String returned nil")
}
if got := githubv4.NewBoolean(false); got == nil {
t.Error("NewBoolean returned nil")
}
if got := githubv4.NewDate(githubv4.Date{}); got == nil {
t.Error("NewDate returned nil")
}
if got := githubv4.NewDateTime(githubv4.DateTime{}); got == nil {
t.Error("NewDateTime returned nil")
}
if got := githubv4.NewFloat(0.0); got == nil {
t.Error("NewFloat returned nil")
}
if got := githubv4.NewGitObjectID(""); got == nil {
t.Error("NewGitObjectID returned nil")
}
if got := githubv4.NewGitRefname(""); got == nil {
t.Error("NewGitRefname returned nil")
}
if got := githubv4.NewGitTimestamp(githubv4.GitTimestamp{}); got == nil {
t.Error("NewGitTimestamp returned nil")
}
if got := githubv4.NewHTML(""); got == nil {
t.Error("NewHTML returned nil")
}
// ID with underlying type string.
if got := githubv4.NewID(""); got == nil {
t.Error("NewID returned nil")
}
// ID with underlying type int.
if got := githubv4.NewID(0); got == nil {
t.Error("NewID returned nil")
}
if got := githubv4.NewInt(0); got == nil {
t.Error("NewInt returned nil")
}
if got := githubv4.NewString(""); got == nil {
t.Error("NewString returned nil")
}
if got := githubv4.NewURI(githubv4.URI{}); got == nil {
t.Error("NewURI returned nil")
}
if got := githubv4.NewX509Certificate(githubv4.X509Certificate{}); got == nil {
t.Error("NewX509Certificate returned nil")
}
}