Skip to content

Commit

Permalink
use go-cmp for more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Jun 22, 2024
1 parent 7fe50ae commit db2b043
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
9 changes: 6 additions & 3 deletions measure_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"reflect"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

var measureCmpOpts = cmp.AllowUnexported(collectedMeasurements{}, measurementSummary{}, entrySummary{})

func TestCollectMeasurementsOK(t *testing.T) {
for _, path := range []string{"vim", "nvim"} {
t.Run(path, func(t *testing.T) {
Expand Down Expand Up @@ -108,8 +111,8 @@ func TestSummarizeStartuptime(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, have) {
t.Fatal("Unexpected average value", have, ", wanted", want)
if !cmp.Equal(want, have, measureCmpOpts) {
t.Fatal(cmp.Diff(want, have, measureCmpOpts))
}
}

Expand Down
12 changes: 4 additions & 8 deletions measurement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func TestPrintSummary(t *testing.T) {
Expand Down Expand Up @@ -94,14 +96,8 @@ func TestPrintSummary(t *testing.T) {
}
lines = lines[:len(lines)-1]

if len(lines) != len(tc.want) {
t.Fatalf("Number of lines does not match: %d v.s. %d. ('%s' v.s. '%s')", len(tc.want), len(lines), tc.want, lines)
}
for i := range lines {
want, have := tc.want[i], lines[i]
if have != want {
t.Errorf("Line %d does not match: Wanted '%s' but have '%s'", i+1, want, have)
}
if !cmp.Equal(lines, tc.want) {
t.Fatal(cmp.Diff(lines, tc.want))
}
})
}
Expand Down
21 changes: 6 additions & 15 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package main
import (
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

var parseCmpOpts = cmp.AllowUnexported(measurement{}, measurementEntry{})

func TestParseOK(t *testing.T) {
tmpfile, err := os.CreateTemp("", "__test_parse_ok")
if err != nil {
Expand Down Expand Up @@ -179,20 +182,8 @@ times in msec
t.Fatal(err)
}

w := tc.expected
if m.elapsedTotal != w.elapsedTotal {
t.Error("Want total", w.elapsedTotal, "but have", m.elapsedTotal)
}

if len(w.entries) != len(m.entries) {
t.Fatal("Want #entries", w.entries, "but have", m.entries)
}
for i := range w.entries {
have := m.entries[i]
want := w.entries[i]
if !reflect.DeepEqual(have, want) {
t.Errorf("%dth entry not match. Want '%+v', but have '%+v'", i, want, have)
}
if !cmp.Equal(m, tc.expected, parseCmpOpts) {
t.Fatal(cmp.Diff(m, tc.expected, parseCmpOpts))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion vim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestRunVimOK(t *testing.T) {
}
}

func TestStartError(t *testing.T) {
func TestRunVimError(t *testing.T) {
for _, exe := range []string{"vim", "nvim"} {
t.Run(exe, func(t *testing.T) {
dir, err := os.MkdirTemp("", "__vim_run_test_error_")
Expand Down

0 comments on commit db2b043

Please sign in to comment.