Skip to content
Closed
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
10 changes: 7 additions & 3 deletions fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<<FATAL>>"
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
Expand Down
38 changes: 35 additions & 3 deletions fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<<FATAL>>") {
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()

Expand Down Expand Up @@ -385,6 +408,7 @@ func Setup(verbose bool) *FixtureTestState {
type FakeTestingT struct {
log *bytes.Buffer
failed bool
fatal bool
}

func (self *FakeTestingT) Helper() {}
Expand All @@ -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 "<<FATAL>>" + result
}
return ""
}