...
1 package githubv4_test
2
3 import (
4 "encoding/json"
5 "errors"
6 "net/url"
7 "reflect"
8 "testing"
9
10 "github.com/shurcooL/githubv4"
11 )
12
13 func TestURI_MarshalJSON(t *testing.T) {
14 tests := []struct {
15 name string
16 in githubv4.URI
17 want string
18 }{
19 {
20 in: githubv4.URI{URL: &url.URL{Scheme: "https", Host: "example.org", Path: "/foo/bar"}},
21 want: `"https://example.org/foo/bar"`,
22 },
23 }
24 for _, tc := range tests {
25 got, err := json.Marshal(tc.in)
26 if err != nil {
27 t.Fatalf("%s: got error: %v", tc.name, err)
28 }
29 if string(got) != tc.want {
30 t.Errorf("%s: got: %q, want: %q", tc.name, string(got), tc.want)
31 }
32 }
33 }
34
35 func TestURI_UnmarshalJSON(t *testing.T) {
36 tests := []struct {
37 name string
38 in string
39 want githubv4.URI
40 wantError error
41 }{
42 {
43 in: `"https://example.org/foo/bar"`,
44 want: githubv4.URI{URL: &url.URL{Scheme: "https", Host: "example.org", Path: "/foo/bar"}},
45 },
46 {
47 name: "null",
48 in: `null`,
49 want: githubv4.URI{},
50 },
51 {
52 name: "error JSON unmarshaling into string",
53 in: `86`,
54 wantError: errors.New("json: cannot unmarshal number into Go value of type string"),
55 },
56 }
57 for _, tc := range tests {
58 var got githubv4.URI
59 err := json.Unmarshal([]byte(tc.in), &got)
60 if got, want := err, tc.wantError; !equalError(got, want) {
61 t.Fatalf("%s: got error: %v, want: %v", tc.name, got, want)
62 }
63 if tc.wantError != nil {
64 continue
65 }
66 if !reflect.DeepEqual(got, tc.want) {
67 t.Errorf("%s: got: %v, want: %v", tc.name, got, tc.want)
68 }
69 }
70 }
71
72
73
74 func equalError(a, b error) bool {
75 return a == nil && b == nil || a != nil && b != nil && a.Error() == b.Error()
76 }
77
78 func TestNewScalars(t *testing.T) {
79 if got := githubv4.NewBase64String(""); got == nil {
80 t.Error("NewBase64String returned nil")
81 }
82 if got := githubv4.NewBoolean(false); got == nil {
83 t.Error("NewBoolean returned nil")
84 }
85 if got := githubv4.NewDate(githubv4.Date{}); got == nil {
86 t.Error("NewDate returned nil")
87 }
88 if got := githubv4.NewDateTime(githubv4.DateTime{}); got == nil {
89 t.Error("NewDateTime returned nil")
90 }
91 if got := githubv4.NewFloat(0.0); got == nil {
92 t.Error("NewFloat returned nil")
93 }
94 if got := githubv4.NewGitObjectID(""); got == nil {
95 t.Error("NewGitObjectID returned nil")
96 }
97 if got := githubv4.NewGitRefname(""); got == nil {
98 t.Error("NewGitRefname returned nil")
99 }
100 if got := githubv4.NewGitTimestamp(githubv4.GitTimestamp{}); got == nil {
101 t.Error("NewGitTimestamp returned nil")
102 }
103 if got := githubv4.NewHTML(""); got == nil {
104 t.Error("NewHTML returned nil")
105 }
106
107 if got := githubv4.NewID(""); got == nil {
108 t.Error("NewID returned nil")
109 }
110
111 if got := githubv4.NewID(0); got == nil {
112 t.Error("NewID returned nil")
113 }
114 if got := githubv4.NewInt(0); got == nil {
115 t.Error("NewInt returned nil")
116 }
117 if got := githubv4.NewString(""); got == nil {
118 t.Error("NewString returned nil")
119 }
120 if got := githubv4.NewURI(githubv4.URI{}); got == nil {
121 t.Error("NewURI returned nil")
122 }
123 if got := githubv4.NewX509Certificate(githubv4.X509Certificate{}); got == nil {
124 t.Error("NewX509Certificate returned nil")
125 }
126 }
127
View as plain text