Re

func Re(reg any, capture ...any) TestDeep

Re operator allows to apply a regexp on a string (or convertible), []byte, error or fmt.Stringer interface (error interface is tested before fmt.Stringer.)

reg is the regexp. It can be a string that is automatically compiled using regexp.Compile, or a *regexp.Regexp.

Optional capture parameter can be 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, "foobar zip!", td.Re(`^foobar`)) // succeeds
td.Cmp(t, "John Doe",
  td.Re(`^(\w+) (\w+)`, []string{"John", "Doe"})) // succeeds
td.Cmp(t, "John Doe",
  td.Re(`^(\w+) (\w+)`, td.Bag("Doe", "John"))) // succeeds

See also ReAll.

See also Re godoc.

Examples

Base example
Stringer example
Error example
Capture example
Compiled example
CompiledStringer example
CompiledError example
CompiledCapture example

CmpRe shortcut

func CmpRe(t TestingT, got, reg , capture any, args ...any) bool

CmpRe is a shortcut for:

td.Cmp(t, got, td.Re(reg, capture), args...)

See above for details.

Re optional parameter capture is here mandatory. nil value should be passed to mimic its absence in original Re call.

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 CmpRe godoc.

Examples

Base example
Stringer example
Error example
Capture example
Compiled example
CompiledStringer example
CompiledError example
CompiledCapture example

T.Re shortcut

func (t *T) Re(got, reg , capture any, args ...any) bool

Re is a shortcut for:

t.Cmp(got, td.Re(reg, capture), args...)

See above for details.

Re optional parameter capture is here mandatory. nil value should be passed to mimic its absence in original Re call.

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.Re godoc.

Examples

Base example
Stringer example
Error example
Capture example
Compiled example
CompiledStringer example
CompiledError example
CompiledCapture example