Last active
June 22, 2022 03:55
-
-
Save mdwhatcott/54bd7da72d1cbb473a61a2acdc429bfb to your computer and use it in GitHub Desktop.
The tiniest testing library
This file contains hidden or 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
package assert | |
import ( | |
"reflect" | |
"testing" | |
) | |
// That allows assertions as in: assert.That(t, actual).Equals(expected) | |
func That(t *testing.T, actual interface{}) *assertion { | |
return &assertion{t: t, actual: actual} | |
} | |
type assertion struct { | |
t *testing.T | |
actual interface{} | |
} | |
func (this *assertion) IsNil() { this.t.Helper(); this.Equals(nil) } | |
func (this *assertion) IsTrue() { this.t.Helper(); this.Equals(true) } | |
func (this *assertion) IsFalse() { this.t.Helper(); this.Equals(false) } | |
func (this *assertion) Equals(expected interface{}) { | |
this.t.Helper() | |
if !reflect.DeepEqual(this.actual, expected) { | |
this.t.Errorf("\n"+ | |
"Expected: %#v\n"+ | |
"Actual: %#v", | |
expected, | |
this.actual, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment