diff --git a/github/github_test.go b/github/github_test.go index b5c4827..bf4d87e 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -6,6 +6,7 @@ package github import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -78,6 +79,24 @@ func testURLParseError(t *testing.T, err error) { } } +// Helper function to test that a value is marshalled to JSON as expected. +func testJSONMarshal(t *testing.T, v interface{}, want string) { + j, err := json.Marshal(v) + if err != nil { + t.Errorf("Unable to marshal JSON for %v", v) + } + + w := new(bytes.Buffer) + err = json.Compact(w, []byte(want)) + if err != nil { + t.Errorf("String is not valid json: %s", want) + } + + if w.String() != string(j) { + t.Errorf("json.Marshal(%q) returned %s, want %s", v, j, w) + } +} + func TestNewClient(t *testing.T) { c := NewClient(nil) diff --git a/github/users_test.go b/github/users_test.go index dec8193..f7ff2af 100644 --- a/github/users_test.go +++ b/github/users_test.go @@ -13,6 +13,46 @@ import ( "testing" ) +func TestUser_marshall(t *testing.T) { + testJSONMarshal(t, &User{}, "{}") + + u := &User{ + Login: String("l"), + ID: Int(1), + URL: String("u"), + AvatarURL: String("a"), + GravatarID: String("g"), + Name: String("n"), + Company: String("c"), + Blog: String("b"), + Location: String("l"), + Email: String("e"), + Hireable: Bool(true), + PublicRepos: Int(1), + Followers: Int(1), + Following: Int(1), + CreatedAt: &referenceTime, + } + want := `{ + "login": "l", + "id": 1, + "url": "u", + "avatar_url": "a", + "gravatar_id": "g", + "name": "n", + "company": "c", + "blog": "b", + "location": "l", + "email": "e", + "hireable": true, + "public_repos": 1, + "followers": 1, + "following": 1, + "created_at": ` + referenceTimeStr + ` + }` + testJSONMarshal(t, u, want) +} + func TestUsersService_Get_authenticatedUser(t *testing.T) { setup() defer teardown()