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