Skip to content

Commit 5167d62

Browse files
committed
Test for marshalling Organizations and make JSON comparison non sensitive to key order (google#1303)
Helps google#55.
1 parent 83f2244 commit 5167d62

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

‎github/github_test.go‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package github
77

88
import (
9-
"bytes"
109
"context"
1110
"encoding/json"
1211
"fmt"
@@ -135,32 +134,29 @@ func testBody(t *testing.T, r *http.Request, want string) {
135134
}
136135
}
137136

138-
// Helper function to test that a value is marshalled to JSON as expected.
137+
// Test whether the marshaling of v produces JSON that corresponds
138+
// to the want string.
139139
func testJSONMarshal(t *testing.T, v interface{}, want string) {
140-
j, err := json.Marshal(v)
140+
// Unmarshal the wanted JSON, to verify its correctness, and marshal it back
141+
// to sort the keys.
142+
u := reflect.New(reflect.TypeOf(v)).Interface()
143+
if err := json.Unmarshal([]byte(want), &u); err != nil {
144+
t.Errorf("Unable to unmarshal JSON for %v: %v", want, err)
145+
}
146+
w, err := json.Marshal(u)
141147
if err != nil {
142-
t.Errorf("Unable to marshal JSON for %v", v)
148+
t.Errorf("Unable to marshal JSON for %#v", u)
143149
}
144150

145-
w := new(bytes.Buffer)
146-
err = json.Compact(w, []byte(want))
151+
// Marshal the target value.
152+
j, err := json.Marshal(v)
147153
if err != nil {
148-
t.Errorf("String is not valid json: %s", want)
154+
t.Errorf("Unable to marshal JSON for %#v", v)
149155
}
150156

151-
if w.String() != string(j) {
157+
if string(w) != string(j) {
152158
t.Errorf("json.Marshal(%q) returned %s, want %s", v, j, w)
153159
}
154-
155-
// now go the other direction and make sure things unmarshal as expected
156-
u := reflect.ValueOf(v).Interface()
157-
if err := json.Unmarshal([]byte(want), u); err != nil {
158-
t.Errorf("Unable to unmarshal JSON for %v: %v", want, err)
159-
}
160-
161-
if !reflect.DeepEqual(v, u) {
162-
t.Errorf("json.Unmarshal(%q) returned %s, want %s", want, u, v)
163-
}
164160
}
165161

166162
func TestNewClient(t *testing.T) {

‎github/orgs_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,38 @@ import (
1414
"testing"
1515
)
1616

17+
func TestOrganization_marshal(t *testing.T) {
18+
testJSONMarshal(t, &Organization{}, "{}")
19+
20+
o := &Organization{
21+
BillingEmail: String("[email protected]"),
22+
Blog: String("https://github.com/blog"),
23+
Company: String("GitHub"),
24+
Email: String("[email protected]"),
25+
Location: String("San Francisco"),
26+
Name: String("github"),
27+
Description: String("GitHub, the company."),
28+
DefaultRepoPermission: String("read"),
29+
MembersCanCreateRepos: Bool(true),
30+
MembersAllowedRepositoryCreationType: String("all"),
31+
}
32+
want := `
33+
{
34+
"billing_email": "[email protected]",
35+
"blog": "https://github.com/blog",
36+
"company": "GitHub",
37+
"email": "[email protected]",
38+
"location": "San Francisco",
39+
"name": "github",
40+
"description": "GitHub, the company.",
41+
"default_repository_permission": "read",
42+
"members_can_create_repositories": true,
43+
"members_allowed_repository_creation_type": "all"
44+
}
45+
`
46+
testJSONMarshal(t, o, want)
47+
}
48+
1749
func TestOrganizationsService_ListAll(t *testing.T) {
1850
client, mux, _, teardown := setup()
1951
defer teardown()

0 commit comments

Comments
 (0)