From 355cfe67c0ff50a6e179ebd2b4f3d7dd40f6d966 Mon Sep 17 00:00:00 2001 From: Michael Whatcott Date: Fri, 6 Oct 2023 10:04:46 -0600 Subject: [PATCH] Assertions whose output begins with "<>" end tests immediately. ...via `*testing.T.Fatalf`. --- fixture.go | 10 +++++++--- fixture_test.go | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/fixture.go b/fixture.go index e628347..97fd7b0 100644 --- a/fixture.go +++ b/fixture.go @@ -60,13 +60,17 @@ func (this *Fixture) Run(name string, test func(fixture *Fixture)) { // So is a convenience method for reporting assertion failure messages, // from the many assertion functions found in github.com/smarty/assertions/should. // Example: this.So(actual, should.Equal, expected) +// Example: this.So(actual, must.Equal, expected) // ends test IMMEDIATELY in case of failure func (this *Fixture) So(actual any, assert assertion, expected ...any) bool { + const fatalPrefix = "<>" failure := assert(actual, expected...) - failed := len(failure) > 0 - if failed { + if strings.HasPrefix(failure, fatalPrefix) { + failure = strings.TrimPrefix(failure, fatalPrefix) + this.t.Fatalf(reports.FailureReport(failure, reports.StackTrace())) + } else if len(failure) > 0 { this.fail(failure) } - return !failed + return len(failure) == 0 } // Assert tests a boolean which, if not true, marks the current test case as failed and diff --git a/fixture_test.go b/fixture_test.go index b92110c..49b46a4 100644 --- a/fixture_test.go +++ b/fixture_test.go @@ -76,6 +76,29 @@ func TestSoFailsAndLogs(t *testing.T) { } } +func TestSoFatal(t *testing.T) { + t.Parallel() + + test := Setup(false) + + result := test.fixture.So(true, MustBeFalse) + test.fixture.finalize() + + if result { + t.Error("Expected false result, got true") + } + output := strings.TrimSpace(test.out.String()) + if !strings.Contains(output, "Expected false, got true instead") { + t.Errorf("Unexpected output: [%s]", test.out.String()) + } + if strings.Contains(output, "<>") { + t.Errorf("Unexpected internal prefix should have been trimmed.") + } + if !test.fakeT.fatal { + t.Error("Test should have been marked as fatal.") + } +} + func TestAssertPasses(t *testing.T) { t.Parallel() @@ -385,6 +408,7 @@ func Setup(verbose bool) *FixtureTestState { type FakeTestingT struct { log *bytes.Buffer failed bool + fatal bool } func (self *FakeTestingT) Helper() {} @@ -394,22 +418,30 @@ func (self *FakeTestingT) Fail() { self.failed = tru func (self *FakeTestingT) Failed() bool { return self.failed } func (this *FakeTestingT) Errorf(format string, args ...any) {} func (this *FakeTestingT) Fatalf(format string, args ...any) { - this.Fail() + this.fatal = true this.Log(fmt.Sprintf(format, args...)) } ////////////////////////////////////////////////////////////////////////////// -func ShouldBeTrue(actual any, expected ...any) string { +func ShouldBeTrue(actual any, _ ...any) string { if actual != true { return "Expected true, got false instead" } return "" } -func ShouldBeFalse(actual any, expected ...any) string { +func ShouldBeFalse(actual any, _ ...any) string { if actual == true { return "Expected false, got true instead" } return "" } + +func MustBeFalse(actual any, _ ...any) string { + result := ShouldBeFalse(actual) + if len(result) > 0 { + return "<>" + result + } + return "" +}