Skip to content

Commit 9629e86

Browse files
committed
add several new new checks and short-hands
1 parent c815c0d commit 9629e86

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

check.go

+22
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,28 @@ func DeepEqual(expect interface{}) CheckFunc {
261261
}
262262
}
263263

264+
// NotCompareEqual returns a check function that fails when the test value
265+
// compare equals to reject.
266+
func NotCompareEqual(reject interface{}) CheckFunc {
267+
return func(got interface{}) error {
268+
if reject == got {
269+
return FailReject(msgNotCompareEqual, got, reject)
270+
}
271+
return nil
272+
}
273+
}
274+
275+
// CompareEqual returns a check function that fails when the test value does not
276+
// compare equals to expect.
277+
func CompareEqual(expect interface{}) CheckFunc {
278+
return func(got interface{}) error {
279+
if expect != got {
280+
return FailExpect(msgCompareEqual, got, expect)
281+
}
282+
return nil
283+
}
284+
}
285+
264286
// NotReflectNil returns a check function that fails when the test value is
265287
// either an untyped nil value or reflects to a pointer with a nil value.
266288
func NotReflectNil() CheckFunc {

check_middleware.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package subtest
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
// OnFloat64 returns a check function where the test value is converted to
68
// float64 before it's passed to c.
@@ -52,3 +54,13 @@ func OnIndex(i int, c Check) CheckFunc {
5254
return nil
5355
}
5456
}
57+
58+
// Iterate runs the first check on index 0, the second on index 1 etc, and
59+
// returns an aggregated error.
60+
func Iterate(cs ...Check) Check {
61+
all := make(AllOf, 0, len(cs))
62+
for i, c := range cs {
63+
all = append(all, OnIndex(i, c))
64+
}
65+
return all
66+
}

subjson/check.go

+24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
package subjson
22

33
import (
4+
"encoding/json"
5+
46
"github.com/clarify/subtest"
57
)
68

9+
// Fields is a short-hand for OnMap(subtest.Fields{...})
10+
type Fields subtest.Fields
11+
12+
// Check validates the json decoded vf against m, expecting vf to return a json
13+
// map.
14+
func (m Fields) Check(vf subtest.ValueFunc) error {
15+
return OnMap(subtest.Fields(m)).Check(vf)
16+
}
17+
18+
// RawEqual is a short-hand for subtest.DeepEqual(json.RawMessage(s))
19+
type RawEqual []byte
20+
21+
// Check validates the raw json equals m.
22+
func (m RawEqual) Check(vf subtest.ValueFunc) error {
23+
return subtest.DeepEqual(json.RawMessage(m)).Check(vf)
24+
}
25+
26+
// IterateSlice is a short-hand for OnSlice(subtest.Iterate(cs...))
27+
func IterateSlice(cs ...subtest.Check) subtest.Check {
28+
return OnSlice(subtest.Iterate(cs...))
29+
}
30+
731
// LessThan is a short-hand for OnNumber(subtest.LessThan(expect)).
832
func LessThan(expect float64) subtest.CheckFunc {
933
return OnNumber(subtest.LessThan(expect))

0 commit comments

Comments
 (0)