Skip to content

Commit

Permalink
feat: add Grep, First & Last operators
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Aug 30, 2022
1 parent eebb2f4 commit 95ed457
Show file tree
Hide file tree
Showing 9 changed files with 2,206 additions and 27 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`ContainsKey`]: https://go-testdeep.zetta.rocks/operators/containskey/
[`Delay`]: https://go-testdeep.zetta.rocks/operators/delay/
[`Empty`]: https://go-testdeep.zetta.rocks/operators/empty/
[`First`]: https://go-testdeep.zetta.rocks/operators/first/
[`Grep`]: https://go-testdeep.zetta.rocks/operators/grep/
[`Gt`]: https://go-testdeep.zetta.rocks/operators/gt/
[`Gte`]: https://go-testdeep.zetta.rocks/operators/gte/
[`HasPrefix`]: https://go-testdeep.zetta.rocks/operators/hasprefix/
Expand All @@ -314,6 +316,7 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`JSON`]: https://go-testdeep.zetta.rocks/operators/json/
[`JSONPointer`]: https://go-testdeep.zetta.rocks/operators/jsonpointer/
[`Keys`]: https://go-testdeep.zetta.rocks/operators/keys/
[`Last`]: https://go-testdeep.zetta.rocks/operators/last/
[`Lax`]: https://go-testdeep.zetta.rocks/operators/lax/
[`Len`]: https://go-testdeep.zetta.rocks/operators/len/
[`Lt`]: https://go-testdeep.zetta.rocks/operators/lt/
Expand Down Expand Up @@ -367,6 +370,8 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`CmpContains`]: https://go-testdeep.zetta.rocks/operators/contains/#cmpcontains-shortcut
[`CmpContainsKey`]: https://go-testdeep.zetta.rocks/operators/containskey/#cmpcontainskey-shortcut
[`CmpEmpty`]: https://go-testdeep.zetta.rocks/operators/empty/#cmpempty-shortcut
[`CmpFirst`]: https://go-testdeep.zetta.rocks/operators/first/#cmpfirst-shortcut
[`CmpGrep`]: https://go-testdeep.zetta.rocks/operators/grep/#cmpgrep-shortcut
[`CmpGt`]: https://go-testdeep.zetta.rocks/operators/gt/#cmpgt-shortcut
[`CmpGte`]: https://go-testdeep.zetta.rocks/operators/gte/#cmpgte-shortcut
[`CmpHasPrefix`]: https://go-testdeep.zetta.rocks/operators/hasprefix/#cmphasprefix-shortcut
Expand All @@ -375,6 +380,7 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`CmpJSON`]: https://go-testdeep.zetta.rocks/operators/json/#cmpjson-shortcut
[`CmpJSONPointer`]: https://go-testdeep.zetta.rocks/operators/jsonpointer/#cmpjsonpointer-shortcut
[`CmpKeys`]: https://go-testdeep.zetta.rocks/operators/keys/#cmpkeys-shortcut
[`CmpLast`]: https://go-testdeep.zetta.rocks/operators/last/#cmplast-shortcut
[`CmpLax`]: https://go-testdeep.zetta.rocks/operators/lax/#cmplax-shortcut
[`CmpLen`]: https://go-testdeep.zetta.rocks/operators/len/#cmplen-shortcut
[`CmpLt`]: https://go-testdeep.zetta.rocks/operators/lt/#cmplt-shortcut
Expand Down Expand Up @@ -427,6 +433,8 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`T.Contains`]: https://go-testdeep.zetta.rocks/operators/contains/#tcontains-shortcut
[`T.ContainsKey`]: https://go-testdeep.zetta.rocks/operators/containskey/#tcontainskey-shortcut
[`T.Empty`]: https://go-testdeep.zetta.rocks/operators/empty/#tempty-shortcut
[`T.First`]: https://go-testdeep.zetta.rocks/operators/first/#tfirst-shortcut
[`T.Grep`]: https://go-testdeep.zetta.rocks/operators/grep/#tgrep-shortcut
[`T.Gt`]: https://go-testdeep.zetta.rocks/operators/gt/#tgt-shortcut
[`T.Gte`]: https://go-testdeep.zetta.rocks/operators/gte/#tgte-shortcut
[`T.HasPrefix`]: https://go-testdeep.zetta.rocks/operators/hasprefix/#thasprefix-shortcut
Expand All @@ -435,6 +443,7 @@ See [FAQ](https://go-testdeep.zetta.rocks/faq/).
[`T.JSON`]: https://go-testdeep.zetta.rocks/operators/json/#tjson-shortcut
[`T.JSONPointer`]: https://go-testdeep.zetta.rocks/operators/jsonpointer/#tjsonpointer-shortcut
[`T.Keys`]: https://go-testdeep.zetta.rocks/operators/keys/#tkeys-shortcut
[`T.Last`]: https://go-testdeep.zetta.rocks/operators/last/#tlast-shortcut
[`T.CmpLax`]: https://go-testdeep.zetta.rocks/operators/lax/#tcmplax-shortcut
[`T.Len`]: https://go-testdeep.zetta.rocks/operators/len/#tlen-shortcut
[`T.Lt`]: https://go-testdeep.zetta.rocks/operators/lt/#tlt-shortcut
Expand Down
68 changes: 67 additions & 1 deletion td/cmp_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"
)

// allOperators lists the 63 operators.
// allOperators lists the 66 operators.
// nil means not usable in JSON().
var allOperators = map[string]any{
"All": All,
Expand All @@ -28,6 +28,8 @@ var allOperators = map[string]any{
"ContainsKey": ContainsKey,
"Delay": nil,
"Empty": Empty,
"First": First,
"Grep": Grep,
"Gt": Gt,
"Gte": Gte,
"HasPrefix": HasPrefix,
Expand All @@ -37,6 +39,7 @@ var allOperators = map[string]any{
"JSON": nil,
"JSONPointer": JSONPointer,
"Keys": Keys,
"Last": Last,
"Lax": nil,
"Len": Len,
"Lt": Lt,
Expand Down Expand Up @@ -315,6 +318,48 @@ func CmpEmpty(t TestingT, got any, args ...any) bool {
return Cmp(t, got, Empty(), args...)
}

// CmpFirst is a shortcut for:
//
// td.Cmp(t, got, td.First(filter, expectedValue), args...)
//
// See [First] for details.
//
// Returns true if the test is OK, false if it fails.
//
// If t is a [*T] then its Config field is inherited.
//
// args... are optional and allow to name the test. This name is
// used in case of failure to qualify the test. If len(args) > 1 and
// the first item of args is a string and contains a '%' rune then
// [fmt.Fprintf] is used to compose the name, else args are passed to
// [fmt.Fprint]. Do not forget it is the name of the test, not the
// reason of a potential failure.
func CmpFirst(t TestingT, got, filter, expectedValue any, args ...any) bool {
t.Helper()
return Cmp(t, got, First(filter, expectedValue), args...)
}

// CmpGrep is a shortcut for:
//
// td.Cmp(t, got, td.Grep(filter, expectedValue), args...)
//
// See [Grep] for details.
//
// Returns true if the test is OK, false if it fails.
//
// If t is a [*T] then its Config field is inherited.
//
// args... are optional and allow to name the test. This name is
// used in case of failure to qualify the test. If len(args) > 1 and
// the first item of args is a string and contains a '%' rune then
// [fmt.Fprintf] is used to compose the name, else args are passed to
// [fmt.Fprint]. Do not forget it is the name of the test, not the
// reason of a potential failure.
func CmpGrep(t TestingT, got, filter, expectedValue any, args ...any) bool {
t.Helper()
return Cmp(t, got, Grep(filter, expectedValue), args...)
}

// CmpGt is a shortcut for:
//
// td.Cmp(t, got, td.Gt(minExpectedValue), args...)
Expand Down Expand Up @@ -483,6 +528,27 @@ func CmpKeys(t TestingT, got, val any, args ...any) bool {
return Cmp(t, got, Keys(val), args...)
}

// CmpLast is a shortcut for:
//
// td.Cmp(t, got, td.Last(filter, expectedValue), args...)
//
// See [Last] for details.
//
// Returns true if the test is OK, false if it fails.
//
// If t is a [*T] then its Config field is inherited.
//
// args... are optional and allow to name the test. This name is
// used in case of failure to qualify the test. If len(args) > 1 and
// the first item of args is a string and contains a '%' rune then
// [fmt.Fprintf] is used to compose the name, else args are passed to
// [fmt.Fprint]. Do not forget it is the name of the test, not the
// reason of a potential failure.
func CmpLast(t TestingT, got, filter, expectedValue any, args ...any) bool {
t.Helper()
return Cmp(t, got, Last(filter, expectedValue), args...)
}

// CmpLax is a shortcut for:
//
// td.Cmp(t, got, td.Lax(expectedValue), args...)
Expand Down
Loading

0 comments on commit 95ed457

Please sign in to comment.