ReAll
func ReAll(reg, capture any) TestDeep
ReAll operator allows to successively apply a regexp on a string
(or convertible), []byte
, error
or fmt.Stringer
interface (error
interface is tested before fmt.Stringer
) and to match its groups
contents.
reg is the regexp. It can be a string
that is automatically
compiled using regexp.Compile
, or a *regexp.Regexp
.
capture is used to match the contents of regexp groups. Groups
are presented as a []string
or [][]byte
depending the original
matched data. Note that an other operator can be used here.
td.Cmp(t, "John Doe",
td.ReAll(`(\w+)(?: |\z)`, []string{"John", "Doe"})) // succeeds
td.Cmp(t, "John Doe",
td.ReAll(`(\w+)(?: |\z)`, td.Bag("Doe", "John"))) // succeeds
See also Re
.
See also ReAll godoc.
Examples
Capture example
t := &testing.T{}
got := "foo bar biz"
ok := td.Cmp(t, got, td.ReAll(`(\w+)`, td.Set("biz", "foo", "bar")),
"checks value %s", got)
fmt.Println(ok)
// Matches, but all catured groups do not match Set
got = "foo BAR biz"
ok = td.Cmp(t, got, td.ReAll(`(\w+)`, td.Set("biz", "foo", "bar")),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CaptureComplex example
t := &testing.T{}
got := "11 45 23 56 85 96"
ok := td.Cmp(t, got,
td.ReAll(`(\d+)`, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 10 && n < 100
}))),
"checks value %s", got)
fmt.Println(ok)
// Matches, but 11 is not greater than 20
ok = td.Cmp(t, got,
td.ReAll(`(\d+)`, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 20 && n < 100
}))),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CompiledCapture example
t := &testing.T{}
expected := regexp.MustCompile(`(\w+)`)
got := "foo bar biz"
ok := td.Cmp(t, got, td.ReAll(expected, td.Set("biz", "foo", "bar")),
"checks value %s", got)
fmt.Println(ok)
// Matches, but all catured groups do not match Set
got = "foo BAR biz"
ok = td.Cmp(t, got, td.ReAll(expected, td.Set("biz", "foo", "bar")),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CompiledCaptureComplex example
t := &testing.T{}
expected := regexp.MustCompile(`(\d+)`)
got := "11 45 23 56 85 96"
ok := td.Cmp(t, got,
td.ReAll(expected, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 10 && n < 100
}))),
"checks value %s", got)
fmt.Println(ok)
// Matches, but 11 is not greater than 20
ok = td.Cmp(t, got,
td.ReAll(expected, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 20 && n < 100
}))),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CmpReAll shortcut
func CmpReAll(t TestingT, got, reg , capture any, args ...any) bool
CmpReAll is a shortcut for:
td.Cmp(t, got, td.ReAll(reg, capture), args...)
See above 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.
See also CmpReAll godoc.
Examples
Capture example
t := &testing.T{}
got := "foo bar biz"
ok := td.CmpReAll(t, got, `(\w+)`, td.Set("biz", "foo", "bar"),
"checks value %s", got)
fmt.Println(ok)
// Matches, but all catured groups do not match Set
got = "foo BAR biz"
ok = td.CmpReAll(t, got, `(\w+)`, td.Set("biz", "foo", "bar"),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CaptureComplex example
t := &testing.T{}
got := "11 45 23 56 85 96"
ok := td.CmpReAll(t, got, `(\d+)`, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 10 && n < 100
})),
"checks value %s", got)
fmt.Println(ok)
// Matches, but 11 is not greater than 20
ok = td.CmpReAll(t, got, `(\d+)`, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 20 && n < 100
})),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CompiledCapture example
t := &testing.T{}
expected := regexp.MustCompile(`(\w+)`)
got := "foo bar biz"
ok := td.CmpReAll(t, got, expected, td.Set("biz", "foo", "bar"),
"checks value %s", got)
fmt.Println(ok)
// Matches, but all catured groups do not match Set
got = "foo BAR biz"
ok = td.CmpReAll(t, got, expected, td.Set("biz", "foo", "bar"),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CompiledCaptureComplex example
t := &testing.T{}
expected := regexp.MustCompile(`(\d+)`)
got := "11 45 23 56 85 96"
ok := td.CmpReAll(t, got, expected, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 10 && n < 100
})),
"checks value %s", got)
fmt.Println(ok)
// Matches, but 11 is not greater than 20
ok = td.CmpReAll(t, got, expected, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 20 && n < 100
})),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
T.ReAll shortcut
func (t *T) ReAll(got, reg , capture any, args ...any) bool
ReAll is a shortcut for:
t.Cmp(got, td.ReAll(reg, capture), args...)
See above for details.
Returns true if the test is OK, false if it fails.
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.
See also T.ReAll godoc.
Examples
Capture example
t := td.NewT(&testing.T{})
got := "foo bar biz"
ok := t.ReAll(got, `(\w+)`, td.Set("biz", "foo", "bar"),
"checks value %s", got)
fmt.Println(ok)
// Matches, but all catured groups do not match Set
got = "foo BAR biz"
ok = t.ReAll(got, `(\w+)`, td.Set("biz", "foo", "bar"),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CaptureComplex example
t := td.NewT(&testing.T{})
got := "11 45 23 56 85 96"
ok := t.ReAll(got, `(\d+)`, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 10 && n < 100
})),
"checks value %s", got)
fmt.Println(ok)
// Matches, but 11 is not greater than 20
ok = t.ReAll(got, `(\d+)`, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 20 && n < 100
})),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CompiledCapture example
t := td.NewT(&testing.T{})
expected := regexp.MustCompile(`(\w+)`)
got := "foo bar biz"
ok := t.ReAll(got, expected, td.Set("biz", "foo", "bar"),
"checks value %s", got)
fmt.Println(ok)
// Matches, but all catured groups do not match Set
got = "foo BAR biz"
ok = t.ReAll(got, expected, td.Set("biz", "foo", "bar"),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false
CompiledCaptureComplex example
t := td.NewT(&testing.T{})
expected := regexp.MustCompile(`(\d+)`)
got := "11 45 23 56 85 96"
ok := t.ReAll(got, expected, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 10 && n < 100
})),
"checks value %s", got)
fmt.Println(ok)
// Matches, but 11 is not greater than 20
ok = t.ReAll(got, expected, td.ArrayEach(td.Code(func(num string) bool {
n, err := strconv.Atoi(num)
return err == nil && n > 20 && n < 100
})),
"checks value %s", got)
fmt.Println(ok)
// Output:
// true
// false