| 1 |
// Copyright 2013 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
func TestStringify(t *testing.T) { |
| 15 |
var nilPointer *string |
| 16 |
|
| 17 |
var tests = []struct { |
| 18 |
in interface{} |
| 19 |
out string |
| 20 |
}{ |
| 21 |
// basic types |
| 22 |
{"foo", `"foo"`}, |
| 23 |
{123, `123`}, |
| 24 |
{1.5, `1.5`}, |
| 25 |
{false, `false`}, |
| 26 |
{ |
| 27 |
[]string{"a", "b"}, |
| 28 |
`["a" "b"]`, |
| 29 |
}, |
| 30 |
{ |
| 31 |
struct { |
| 32 |
A []string |
| 33 |
}{nil}, |
| 34 |
// nil slice is skipped |
| 35 |
`{}`, |
| 36 |
}, |
| 37 |
{ |
| 38 |
struct { |
| 39 |
A string |
| 40 |
}{"foo"}, |
| 41 |
// structs not of a named type get no prefix |
| 42 |
`{A:"foo"}`, |
| 43 |
}, |
| 44 |
|
| 45 |
// pointers |
| 46 |
{nilPointer, `<nil>`}, |
| 47 |
{String("foo"), `"foo"`}, |
| 48 |
{Int(123), `123`}, |
| 49 |
{Bool(false), `false`}, |
| 50 |
{ |
| 51 |
[]*string{String("a"), String("b")}, |
| 52 |
`["a" "b"]`, |
| 53 |
}, |
| 54 |
|
| 55 |
// actual GitHub structs |
| 56 |
{ |
| 57 |
Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}, |
| 58 |
`github.Timestamp{2006-01-02 15:04:05 +0000 UTC}`, |
| 59 |
}, |
| 60 |
{ |
| 61 |
&Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}, |
| 62 |
`github.Timestamp{2006-01-02 15:04:05 +0000 UTC}`, |
| 63 |
}, |
| 64 |
{ |
| 65 |
User{ID: Int64(123), Name: String("n")}, |
| 66 |
`github.User{ID:123, Name:"n"}`, |
| 67 |
}, |
| 68 |
{ |
| 69 |
Repository{Owner: &User{ID: Int64(123)}}, |
| 70 |
`github.Repository{Owner:github.User{ID:123}}`, |
| 71 |
}, |
| 72 |
} |
| 73 |
|
| 74 |
for i, tt := range tests { |
| 75 |
s := Stringify(tt.in) |
| 76 |
if s != tt.out { |
| 77 |
t.Errorf("%d. Stringify(%q) => %q, want %q", i, tt.in, s, tt.out) |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Directly test the String() methods on various GitHub types. We don't do an |
| 83 |
// exhaustive test of all the various field types, since TestStringify() above |
| 84 |
// takes care of that. Rather, we just make sure that Stringify() is being |
| 85 |
// used to build the strings, which we do by verifying that pointers are |
| 86 |
// stringified as their underlying value. |
| 87 |
func TestString(t *testing.T) { |
| 88 |
var tests = []struct { |
| 89 |
in interface{} |
| 90 |
out string |
| 91 |
}{ |
| 92 |
{CodeResult{Name: String("n")}, `github.CodeResult{Name:"n"}`}, |
| 93 |
{CommitAuthor{Name: String("n")}, `github.CommitAuthor{Name:"n"}`}, |
| 94 |
{CommitFile{SHA: String("s")}, `github.CommitFile{SHA:"s"}`}, |
| 95 |
{CommitStats{Total: Int(1)}, `github.CommitStats{Total:1}`}, |
| 96 |
{CommitsComparison{TotalCommits: Int(1)}, `github.CommitsComparison{TotalCommits:1}`}, |
| 97 |
{Commit{SHA: String("s")}, `github.Commit{SHA:"s"}`}, |
| 98 |
{Event{ID: String("1")}, `github.Event{ID:"1"}`}, |
| 99 |
{GistComment{ID: Int64(1)}, `github.GistComment{ID:1}`}, |
| 100 |
{GistFile{Size: Int(1)}, `github.GistFile{Size:1}`}, |
| 101 |
{Gist{ID: String("1")}, `github.Gist{ID:"1"}`}, |
| 102 |
{GitObject{SHA: String("s")}, `github.GitObject{SHA:"s"}`}, |
| 103 |
{Gitignore{Name: String("n")}, `github.Gitignore{Name:"n"}`}, |
| 104 |
{Hook{ID: Int64(1)}, `github.Hook{ID:1}`}, |
| 105 |
{IssueComment{ID: Int64(1)}, `github.IssueComment{ID:1}`}, |
| 106 |
{Issue{Number: Int(1)}, `github.Issue{Number:1}`}, |
| 107 |
{Key{ID: Int64(1)}, `github.Key{ID:1}`}, |
| 108 |
{Label{ID: Int64(1), Name: String("l")}, `github.Label{ID:1, Name:"l"}`}, |
| 109 |
{Organization{ID: Int64(1)}, `github.Organization{ID:1}`}, |
| 110 |
{PullRequestComment{ID: Int64(1)}, `github.PullRequestComment{ID:1}`}, |
| 111 |
{PullRequest{Number: Int(1)}, `github.PullRequest{Number:1}`}, |
| 112 |
{PullRequestReview{ID: Int64(1)}, `github.PullRequestReview{ID:1}`}, |
| 113 |
{DraftReviewComment{Position: Int(1)}, `github.DraftReviewComment{Position:1}`}, |
| 114 |
{PullRequestReviewRequest{Body: String("r")}, `github.PullRequestReviewRequest{Body:"r"}`}, |
| 115 |
{PullRequestReviewDismissalRequest{Message: String("r")}, `github.PullRequestReviewDismissalRequest{Message:"r"}`}, |
| 116 |
{HeadCommit{SHA: String("s")}, `github.HeadCommit{SHA:"s"}`}, |
| 117 |
{PushEvent{PushID: Int64(1)}, `github.PushEvent{PushID:1}`}, |
| 118 |
{Reference{Ref: String("r")}, `github.Reference{Ref:"r"}`}, |
| 119 |
{ReleaseAsset{ID: Int64(1)}, `github.ReleaseAsset{ID:1}`}, |
| 120 |
{RepoStatus{ID: Int64(1)}, `github.RepoStatus{ID:1}`}, |
| 121 |
{RepositoryComment{ID: Int64(1)}, `github.RepositoryComment{ID:1}`}, |
| 122 |
{RepositoryCommit{SHA: String("s")}, `github.RepositoryCommit{SHA:"s"}`}, |
| 123 |
{RepositoryContent{Name: String("n")}, `github.RepositoryContent{Name:"n"}`}, |
| 124 |
{RepositoryRelease{ID: Int64(1)}, `github.RepositoryRelease{ID:1}`}, |
| 125 |
{Repository{ID: Int64(1)}, `github.Repository{ID:1}`}, |
| 126 |
{Team{ID: Int64(1)}, `github.Team{ID:1}`}, |
| 127 |
{TreeEntry{SHA: String("s")}, `github.TreeEntry{SHA:"s"}`}, |
| 128 |
{Tree{SHA: String("s")}, `github.Tree{SHA:"s"}`}, |
| 129 |
{User{ID: Int64(1)}, `github.User{ID:1}`}, |
| 130 |
{WebHookAuthor{Name: String("n")}, `github.CommitAuthor{Name:"n"}`}, |
| 131 |
{WebHookCommit{ID: String("1")}, `github.HeadCommit{ID:"1"}`}, |
| 132 |
{WebHookPayload{Ref: String("r")}, `github.PushEvent{Ref:"r"}`}, |
| 133 |
} |
| 134 |
|
| 135 |
for i, tt := range tests { |
| 136 |
s := tt.in.(fmt.Stringer).String() |
| 137 |
if s != tt.out { |
| 138 |
t.Errorf("%d. String() => %q, want %q", i, tt.in, tt.out) |
| 139 |
} |
| 140 |
} |
| 141 |
} |