From e049560b2e145bcb08493873c62991cb91c80a6c Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Mon, 5 Jun 2023 20:58:57 +0100 Subject: [PATCH] Port to my little test helper --- go.mod | 4 ++ go.sum | 4 ++ internal/count/count_test.go | 74 +++++++++++++----------------------- main_test.go | 60 ++++++++++------------------- 4 files changed, 55 insertions(+), 87 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index fe9655b..8d12958 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module github.com/FollowTheProcess/gowc go 1.20 + +require github.com/FollowTheProcess/test v0.1.0 + +require github.com/google/go-cmp v0.5.9 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..17d0904 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/FollowTheProcess/test v0.1.0 h1:oEIzRCGrMMquPW0Gb7ScsVwDVDAWBF2Mjr8mmK10QDY= +github.com/FollowTheProcess/test v0.1.0/go.mod h1:ou4u+b2RmPFNwW6iGNMMcWdCQGfF6BN1UrbM7mKOONQ= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/internal/count/count_test.go b/internal/count/count_test.go index 03fc30b..e0f2952 100644 --- a/internal/count/count_test.go +++ b/internal/count/count_test.go @@ -5,12 +5,12 @@ import ( "io" "os" "path/filepath" - "reflect" "sort" "strings" "testing" "github.com/FollowTheProcess/gowc/internal/count" + "github.com/FollowTheProcess/test" ) const someText = ` @@ -97,13 +97,9 @@ func TestCount(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := count.One(tt.in, "") - if err != nil { - t.Fatalf("count returned an unexpected error: %v", err) - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("\nGot:\t%+v\nWanted:\t%+v\n", got, tt.want) - } + test.Ok(t, err) + test.Equal(t, got, tt.want) }) } } @@ -117,9 +113,7 @@ func TestCountAll(t *testing.T) { } results, err := count.All(files) - if err != nil { - t.Fatalf("count.All returned an error: %v", err) - } + test.Ok(t, err) want := count.Results{ { @@ -148,9 +142,7 @@ func TestCountAll(t *testing.T) { sort.Stable(ByName(results)) sort.Stable(ByName(want)) - if !sliceEqual(results, want) { - t.Errorf("\nGot:\t%+v\nWanted:\t%+v", results, want) - } + test.EqualFunc(t, results, want, resultsEqual) } func TestDisplay(t *testing.T) { @@ -163,16 +155,13 @@ func TestDisplay(t *testing.T) { } out := &bytes.Buffer{} - if err := count.Display(out, false); err != nil { - t.Fatalf("Display returned an unexpected error: %v", err) - } + err := count.Display(out, false) + test.Ok(t, err) got := out.String() want := "File\tBytes\tChars\tLines\tWords\ntest\t128\t78926\t42\t1000\n" - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) } func TestDisplayMultiple(t *testing.T) { @@ -201,16 +190,13 @@ func TestDisplayMultiple(t *testing.T) { } out := &bytes.Buffer{} - if err := counts.Display(out, false); err != nil { - t.Fatalf("Display returned an unexpected error: %v", err) - } + err := counts.Display(out, false) + test.Ok(t, err) got := out.String() want := "File\tBytes\tChars\tLines\tWords\none\t128\t78926\t42\t1000\ntwo\t128\t78926\t42\t1000\nthree\t128\t78926\t42\t1000\n" - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) } func TestDisplayJSON(t *testing.T) { @@ -223,16 +209,13 @@ func TestDisplayJSON(t *testing.T) { } out := &bytes.Buffer{} - if err := count.Display(out, true); err != nil { - t.Fatalf("Display returned an unexpected error: %v", err) - } + err := count.Display(out, true) + test.Ok(t, err) got := strings.TrimSpace(out.String()) want := `{"name":"test","lines":42,"bytes":128,"words":1000,"chars":78926}` - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) } func TestDisplayJSONMultiple(t *testing.T) { @@ -261,16 +244,13 @@ func TestDisplayJSONMultiple(t *testing.T) { } out := &bytes.Buffer{} - if err := counts.Display(out, true); err != nil { - t.Fatalf("Display returned an unexpected error: %v", err) - } + err := counts.Display(out, true) + test.Ok(t, err) got := strings.TrimSpace(out.String()) want := `[{"name":"one","lines":42,"bytes":128,"words":1000,"chars":78926},{"name":"two","lines":42,"bytes":128,"words":1000,"chars":78926},{"name":"three","lines":42,"bytes":128,"words":1000,"chars":78926}]` - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) } func BenchmarkCount(b *testing.B) { @@ -295,22 +275,20 @@ func BenchmarkCount(b *testing.B) { } } -func sliceEqual[T comparable](a, b []T) bool { +type ByName []count.Result + +func (a ByName) Len() int { return len(a) } +func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name } + +func resultsEqual(a, b count.Results) bool { if len(a) != len(b) { return false } - - for i, item := range a { - if b[i] != item { + for i := range a { + if a[i] != b[i] { return false } } - return true } - -type ByName []count.Result - -func (a ByName) Len() int { return len(a) } -func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name } diff --git a/main_test.go b/main_test.go index c934850..2a3156f 100644 --- a/main_test.go +++ b/main_test.go @@ -8,6 +8,8 @@ import ( "path/filepath" "strings" "testing" + + "github.com/FollowTheProcess/test" ) const binName = "./gowc" @@ -33,16 +35,13 @@ func TestHelp(t *testing.T) { cmd := exec.Command(binName, "-help") cmd.Stdout = stdout - if err := cmd.Run(); err != nil { - t.Fatalf("-help produced an error: %v", err) - } + err := cmd.Run() + test.Ok(t, err) want := usage got := stdout.String() - if got != want { - t.Errorf("\nGot:\t%s\nWanted:\t%s\n", got, want) - } + test.Equal(t, got, want) } func TestVersion(t *testing.T) { @@ -50,16 +49,13 @@ func TestVersion(t *testing.T) { cmd := exec.Command(binName, "-version") cmd.Stdout = stdout - if err := cmd.Run(); err != nil { - t.Fatalf("-version produced an error: %v", err) - } + err := cmd.Run() + test.Ok(t, err) want := "Version: 0.1.0\nCommit: blah\n" got := stdout.String() - if got != want { - t.Errorf("\nGot:\n%s\nWanted:\n%s\n", got, want) - } + test.Equal(t, got, want) } func TestBadFlag(t *testing.T) { @@ -78,17 +74,14 @@ func TestCountStdin(t *testing.T) { cmd.Stdout = stdout cmd.Stderr = stderr - if err := cmd.Run(); err != nil { - t.Fatalf("Reading from stdin returned an error: %s", stderr.String()) - } + err := cmd.Run() + test.Ok(t, err) got := stdout.String() want := "File\tBytes\tChars\tLines\tWords\nstdin\t12\t12\t1\t2\n" - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) } func TestCountStdinJSON(t *testing.T) { @@ -100,17 +93,14 @@ func TestCountStdinJSON(t *testing.T) { cmd.Stdout = stdout cmd.Stderr = stderr - if err := cmd.Run(); err != nil { - t.Fatalf("Reading from stdin returned an error: %s", stderr.String()) - } + err := cmd.Run() + test.Ok(t, err) got := strings.TrimSpace(stdout.String()) want := `{"name":"stdin","lines":1,"bytes":12,"words":2,"chars":12}` - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) } func TestCountStdinEmpty(t *testing.T) { @@ -127,9 +117,7 @@ func TestCountStdinEmpty(t *testing.T) { want := "nothing to read from stdin\n" - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) } func TestCountFile(t *testing.T) { @@ -142,17 +130,14 @@ func TestCountFile(t *testing.T) { cmd.Stdout = stdout cmd.Stderr = stderr - if err := cmd.Run(); err != nil { - t.Fatalf("Reading from moby dick returned an error: %s", stderr.String()) - } + err := cmd.Run() + test.Ok(t, err) got := stdout.String() want := fmt.Sprintf("File\t\t\t\t\tBytes\tChars\tLines\tWords\n%s\t1232922\t1232922\t23243\t214132\n", mobyDick) - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) } func TestCountFileJSON(t *testing.T) { @@ -165,15 +150,12 @@ func TestCountFileJSON(t *testing.T) { cmd.Stdout = stdout cmd.Stderr = stderr - if err := cmd.Run(); err != nil { - t.Fatalf("Reading from moby dick returned an error: %s", stderr.String()) - } + err := cmd.Run() + test.Ok(t, err) got := strings.TrimSpace(stdout.String()) want := fmt.Sprintf(`{"name":"%s","lines":23243,"bytes":1232922,"words":214132,"chars":1232922}`, mobyDick) - if got != want { - t.Errorf("\nGot:\t%#v\nWanted:\t%#v\n", got, want) - } + test.Equal(t, got, want) }