From 4423e722a9ff219bbda3ef8cd81c8aea391a9c87 Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Sun, 22 Mar 2020 06:35:22 +0300 Subject: [PATCH] Move error message tests from helper test to separate test. --- testequal/assertions_test.go | 17 +++++++++++++++-- testequal/helper_test.go | 9 +-------- testequal/t.go | 3 ++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/testequal/assertions_test.go b/testequal/assertions_test.go index 9eef970..679ee78 100644 --- a/testequal/assertions_test.go +++ b/testequal/assertions_test.go @@ -82,14 +82,27 @@ func TestNotEqual(t *testing.T) { } } -type mockT struct{} +type mockT struct { + errMsg string +} -func (m *mockT) Errorf(format string, args ...interface{}) {} +func (m *mockT) Errorf(format string, args ...interface{}) { + m.errMsg = fmt.Sprintf(format, args...) +} func (m *mockT) FailNow() {} func (m *mockT) Helper() {} +func TestErrorMessage(t *testing.T) { + mockT := &mockT{} + RequireNotEqual(mockT, 1, 1, "1 != 1") + require.Contains(t, mockT.errMsg, "1 != 1") + + RequireEqual(mockT, 1, 2, "%d must be equal to %d", 1, 2) + require.Contains(t, mockT.errMsg, "1 must be equal to 2") +} + func BenchmarkRequireEqualInt64(b *testing.B) { t := &mockT{} b.ResetTimer() diff --git a/testequal/helper_test.go b/testequal/helper_test.go index 5b203a6..bbf31c2 100644 --- a/testequal/helper_test.go +++ b/testequal/helper_test.go @@ -13,7 +13,7 @@ func TestHelper(t *testing.T) { if os.Getenv("FAIL_ASSERTIONS") == "1" { AssertEqual(t, 1, 2, "%d must be equal to %d", 1, 2) AssertNotEqual(t, 1, 1, "1 != 1") - RequireEqual(t, 1, 2, errMsgLog{Exp: 1, Act: 2}) + RequireEqual(t, 1, 2) return } @@ -25,16 +25,9 @@ func TestHelper(t *testing.T) { err := cmd.Run() if e, ok := err.(*exec.ExitError); ok && !e.Success() { require.Contains(t, buf.String(), "helper_test.go:14") - require.Contains(t, buf.String(), "1 must be equal to 2") require.Contains(t, buf.String(), "helper_test.go:15") - require.Contains(t, buf.String(), "1 != 1") require.Contains(t, buf.String(), "helper_test.go:16") return } t.Fatalf("process ran with err %v, want exit status 1", err) } - -type errMsgLog struct { - Exp int - Act int -} diff --git a/testequal/t.go b/testequal/t.go index 2bf598e..ec1e38a 100644 --- a/testequal/t.go +++ b/testequal/t.go @@ -2,7 +2,8 @@ package testequal -// T is an interface wrapper around *testing.T. +// T is an interface wrapper for *testing.T +// that contains only a small subset of methods. type T interface { Errorf(format string, args ...interface{}) Helper()