diff --git a/matchers/be_true.go b/matchers/be_true.go new file mode 100644 index 0000000..6378af8 --- /dev/null +++ b/matchers/be_true.go @@ -0,0 +1,21 @@ +package matchers + +import "fmt" + +type BeTrueMatcher struct{} + +func BeTrue() BeTrueMatcher { + return BeTrueMatcher{} +} + +func (m BeTrueMatcher) Match(actual interface{}) (interface{}, error) { + f, ok := actual.(bool) + if !ok { + return nil, fmt.Errorf("'%v' (%[1]T) is not a bool", actual) + } + + if !f { + return nil, fmt.Errorf("%t is not true", actual) + } + return actual, nil +} diff --git a/matchers/be_true_test.go b/matchers/be_true_test.go new file mode 100644 index 0000000..ada5049 --- /dev/null +++ b/matchers/be_true_test.go @@ -0,0 +1,39 @@ +package matchers_test + +import ( + "testing" + + "github.com/apoydence/onpar/matchers" +) + +func TestTruthiness(t *testing.T) { + t.Parallel() + + m := matchers.BeTrue() + + _, err := m.Match(42) + if err == nil { + t.Error("expected err to not be nil") + } + + v, err := m.Match(true) + if err != nil { + t.Error("expected err to be nil") + } + if v != true { + t.Errorf("expected %v to be true", v) + } + + _, err = m.Match(false) + if err == nil { + t.Error("expected err to not be nil") + } + + v, err = m.Match(2 == 2) + if err != nil { + t.Error("expected err to be nil") + } + if v != true { + t.Errorf("expected %v to be true", v) + } +}