From 0cfa4be86a54116e6d100cf740e95b262bf6ce2a Mon Sep 17 00:00:00 2001 From: Toshiya Doi Date: Wed, 18 Mar 2020 07:30:28 +0900 Subject: [PATCH] Add tests for utilities of PR/issue state format --- command/issue_test.go | 22 ++++++++++++++++++++++ command/pr.go | 3 +-- command/pr_test.go | 23 +++++++++++++++++++++++ go.mod | 1 + go.sum | 1 + 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/command/issue_test.go b/command/issue_test.go index 7552b1e17..2eeec2bb1 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -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) + } + }) + } +} diff --git a/command/pr.go b/command/pr.go index af33d18c0..542eafbc5 100644 --- a/command/pr.go +++ b/command/pr.go @@ -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 { diff --git a/command/pr_test.go b/command/pr_test.go index a7a039c2e..d007ac1ea 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -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) + } + }) + } +} diff --git a/go.mod b/go.mod index 63a39c1bf..f45918576 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 8d7665e96..737961e33 100644 --- a/go.sum +++ b/go.sum @@ -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=