Skip to content

Commit

Permalink
Add comment context onto more functions (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Aug 27, 2024
1 parent 2195bb2 commit 5f89192
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 10 deletions.
40 changes: 37 additions & 3 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,16 @@ func EqualFunc[T any](tb testing.TB, got, want T, equal func(a, b T) bool) {
func NotEqual[T comparable](tb testing.TB, got, want T) {
tb.Helper()
if got == want {
tb.Fatalf("\nValues were equal:\t%+v\n", got)
if comment := getComment(); comment != "" {
tb.Fatalf(
"\nEqual // %s\n%s\nGot:\t%+v\n\nExpected values to be different\n",
comment,
strings.Repeat("-", len("Equal")),
got,
)
} else {
tb.Fatalf("\nEqual\n%s\nGot:\t%+v\n\nExpected values to be different\n", strings.Repeat("-", len("Equal")), got)
}
}
}

Expand All @@ -141,7 +150,16 @@ func NotEqual[T comparable](tb testing.TB, got, want T) {
func NotEqualFunc[T any](tb testing.TB, got, want T, equal func(a, b T) bool) {
tb.Helper()
if equal(got, want) {
tb.Fatalf("\nValues were equal:\t%+v\n\nequal(got, want) returned true\n", got)
if comment := getComment(); comment != "" {
tb.Fatalf(
"\nEqual // %s\n%s\nGot:\t%+v\n\nequal(got, want) returned true\n",
comment,
strings.Repeat("-", len("Equal")),
got,
)
} else {
tb.Fatalf("\nEqual\n%s\nGot:\t%+v\n\nequal(got, want) returned true\n", strings.Repeat("-", len("Equal")), got)
}
}
}

Expand Down Expand Up @@ -192,7 +210,23 @@ func Err(tb testing.TB, err error) {
func WantErr(tb testing.TB, err error, want bool) {
tb.Helper()
if (err != nil) != want {
tb.Fatalf("\nWantErr\n-------\nGot error:\t%v\nWanted error:\t%v\n", err, want)
var reason string
var wanted error
if want {
reason = fmt.Sprintf("Wanted an error but got %v", err)
wanted = errors.New("error")
} else {
reason = fmt.Sprintf("Got an unexpected error: %v", err)
wanted = nil
}
fail := failure[any]{
got: err,
want: wanted,
title: "WantErr",
reason: reason,
comment: getComment(),
}
tb.Fatal(fail.String())
}
}

Expand Down
62 changes: 55 additions & 7 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,16 @@ func TestPassFail(t *testing.T) {
test.NotEqual(tb, "apples", "apples")
},
wantFail: true,
wantOut: "\nValues were equal:\tapples\n",
wantOut: "\nEqual\n-----\nGot:\tapples\n\nExpected values to be different\n",
},
{
name: "not equal string fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NotEqual(tb, "apples", "apples") // different apples
},
wantFail: true,
wantOut: "\nEqual // different apples\n-----\nGot:\tapples\n\nExpected values to be different\n",
},
{
name: "not equal int pass",
Expand All @@ -146,7 +155,16 @@ func TestPassFail(t *testing.T) {
test.NotEqual(tb, 1, 1)
},
wantFail: true,
wantOut: "\nValues were equal:\t1\n",
wantOut: "\nEqual\n-----\nGot:\t1\n\nExpected values to be different\n",
},
{
name: "not equal int fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NotEqual(tb, 1, 1) // 1 != 1?
},
wantFail: true,
wantOut: "\nEqual // 1 != 1?\n-----\nGot:\t1\n\nExpected values to be different\n",
},
{
name: "ok pass",
Expand Down Expand Up @@ -314,7 +332,19 @@ func TestPassFail(t *testing.T) {
test.NotEqualFunc(tb, "word", "different word", rubbishNotEqual)
},
wantFail: true,
wantOut: "\nValues were equal:\tword\n\nequal(got, want) returned true\n",
wantOut: "\nEqual\n-----\nGot:\tword\n\nequal(got, want) returned true\n",
},
{
name: "not equal func fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
rubbishNotEqual := func(a, b string) bool {
return true // Always equal
}
test.NotEqualFunc(tb, "word", "different word", rubbishNotEqual) // Bad equal
},
wantFail: true,
wantOut: "\nEqual // Bad equal\n-----\nGot:\tword\n\nequal(got, want) returned true\n",
},
{
name: "deep equal pass",
Expand Down Expand Up @@ -365,10 +395,19 @@ func TestPassFail(t *testing.T) {
name: "want err fail when got and not wanted",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, errors.New("uh oh"), false) // Didn't want an error but got one
test.WantErr(tb, errors.New("uh oh"), false)
},
wantFail: true,
wantOut: "\nWantErr\n-------\nGot:\tuh oh\nWanted:\t<nil>\n\nGot an unexpected error: uh oh\n",
},
{
name: "want err fail when got and not wanted with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, errors.New("uh oh"), false) // comment
},
wantFail: true,
wantOut: "\nWantErr\n-------\nGot error:\tuh oh\nWanted error:\tfalse\n",
wantOut: "\nWantErr // comment\n-------\nGot:\tuh oh\nWanted:\t<nil>\n\nGot an unexpected error: uh oh\n",
},
{
name: "want err pass when not got and not wanted",
Expand All @@ -383,10 +422,19 @@ func TestPassFail(t *testing.T) {
name: "want err fail when not got but wanted",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, nil, true) // Wanted an error but didn't get one
test.WantErr(tb, nil, true)
},
wantFail: true,
wantOut: "\nWantErr\n-------\nGot:\t<nil>\nWanted:\terror\n\nWanted an error but got <nil>\n",
},
{
name: "want err fail when not got but wanted with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, nil, true) // comment
},
wantFail: true,
wantOut: "\nWantErr\n-------\nGot error:\t<nil>\nWanted error:\ttrue\n",
wantOut: "\nWantErr // comment\n-------\nGot:\t<nil>\nWanted:\terror\n\nWanted an error but got <nil>\n",
},
{
name: "file pass",
Expand Down

0 comments on commit 5f89192

Please sign in to comment.