Add tests for utilities of PR/issue state format

This commit is contained in:
Toshiya Doi 2020-03-18 07:30:28 +09:00
parent 46a632c8f6
commit 0cfa4be86a
5 changed files with 48 additions and 2 deletions

View file

@ -11,6 +11,7 @@ import (
"testing"
"github.com/cli/cli/utils"
"github.com/google/go-cmp/cmp"
)
func TestIssueStatus(t *testing.T) {
@ -585,3 +586,24 @@ func TestIssueCreate_webTitleBody(t *testing.T) {
eq(t, url, "https://github.com/OWNER/REPO/issues/new?title=mytitle&body=mybody")
eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n")
}
func TestIssueStateTitleWithColor(t *testing.T) {
tests := map[string]struct {
state string
want string
}{
"Open state": {state: "OPEN", want: "Open"},
"Closed state": {state: "CLOSED", want: "Closed"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := issueStateTitleWithColor(tc.state)
diff := cmp.Diff(tc.want, got)
if diff != "" {
t.Fatalf(diff)
}
})
}
}

View file

@ -236,9 +236,8 @@ func prStateTitleWithColor(pr api.PullRequest) string {
func colorFuncForPR(pr api.PullRequest) func(string) string {
if pr.State == "OPEN" && pr.IsDraft {
return utils.Gray
} else {
return utils.ColorFuncForState(pr.State)
}
return utils.ColorFuncForState(pr.State)
}
func prView(cmd *cobra.Command, args []string) error {

View file

@ -12,6 +12,7 @@ import (
"testing"
"github.com/cli/cli/utils"
"github.com/google/go-cmp/cmp"
"github.com/google/shlex"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -780,3 +781,25 @@ func TestReplaceExcessiveWhitespace(t *testing.T) {
eq(t, replaceExcessiveWhitespace("hello goodbye"), "hello goodbye")
eq(t, replaceExcessiveWhitespace(" hello \n goodbye "), "hello goodbye")
}
func TestPrStateTitleWithColor(t *testing.T) {
tests := map[string]struct {
state string
want string
}{
"Format OPEN state": {state: "OPEN", want: "Open"},
"Format CLOSED state": {state: "CLOSED", want: "Closed"},
"Format MERGED state": {state: "MERGED", want: "Merged"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := issueStateTitleWithColor(tc.state)
diff := cmp.Diff(tc.want, got)
if diff != "" {
t.Fatalf(diff)
}
})
}
}

1
go.mod
View file

@ -7,6 +7,7 @@ require (
github.com/briandowns/spinner v1.9.0
github.com/charmbracelet/glamour v0.1.1-0.20200304134224-7e5c90143acc
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/google/go-cmp v0.2.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/hashicorp/go-version v1.2.0
github.com/henvic/httpretty v0.0.4

1
go.sum
View file

@ -60,6 +60,7 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f h1:5CjVwnuUcp5adK4gmY6i72gpVFVnZDP2h5TmPScB6u4=
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=