-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchers.go
43 lines (35 loc) · 946 Bytes
/
matchers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// MIT License. Copyright (c) 2020 CQFN
// https://github.com/g4s8/go-matchers/blob/master/LICENSE
// Package matchers provide testing primitives for matching targets against expected behavior.
package matchers
import (
"testing"
)
// Matcher verifies target
type Matcher interface {
Check(interface{}) bool
String() string
}
// Assertion is a statement with assert
type Assertion interface {
That(stmt string, target interface{}, matcher Matcher)
}
type assertion struct {
t *testing.T
}
func checker(target interface{}, matcher Matcher) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
if !matcher.Check(target) {
t.Fatalf("expected value `%s`, but was `%v`", matcher, target)
}
}
}
func (a *assertion) That(stmt string, target interface{}, matcher Matcher) {
a.t.Helper()
a.t.Run(stmt, checker(target, matcher))
}
// Assert creates new assertion
func Assert(t *testing.T) Assertion {
return &assertion{t}
}