Created
November 6, 2022 16:39
-
-
Save fredrb/d58a821dfbc74cf41d081d7f09be82cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Assertion interface { | |
// ... redacted for brevity | |
} | |
func With(t *testing.T) Assertion { | |
return &assertImpl{wrappedT: t} | |
} | |
type assertImpl struct { | |
wrappedT *testing.T | |
stopOnFailure bool | |
failedMessage string | |
} | |
func (a *assertImpl) Must() Assertion { | |
a.stopOnFailure = true | |
return a | |
} | |
func (a *assertImpl) Message(msg string) Assertion { | |
a.failedMessage = msg | |
return a | |
} | |
func (a *assertImpl) Messagef(msg string, args ...interface{}) Assertion { | |
a.failedMessage = fmt.Sprintf(msg, args...) | |
return a | |
} | |
func (a *assertImpl) Equal(sideA, sideB interface{}) { | |
// Equal logic using "github.com/google/go-cmp/cmp" | |
// But this can be substituted by the custom equal logic found in testify. | |
if !cmp.Equal(sideA, sideB) { | |
diff := cmp.Diff(sideA, sideB) | |
a.wrappedT.Errorf("Test failed: %s\n%s", a.failedMessage, diff) | |
if a.stopOnFailure { | |
a.wrappedT.FailNow() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment