Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
74 changes: 26 additions & 48 deletions internal/count/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down Expand Up @@ -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)
})
}
}
Expand All @@ -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{
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 }
60 changes: 21 additions & 39 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"strings"
"testing"

"github.com/FollowTheProcess/test"
)

const binName = "./gowc"
Expand All @@ -33,33 +35,27 @@ 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) {
stdout := &bytes.Buffer{}

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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}