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.)
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.
When matching many, many lines with a single regexp, it is
sometimes difficult to see where the regexp failed in the input
string. To avoid that, the regexp can be split by lines and so the
failure is easier to locate, thanks to List operator and Flatten:
t:=&testing.T{}got:="foo bar"ok:=td.Cmp(t,got,td.Re("(zip|bar)$"),"checks value %s",got)fmt.Println(ok)got="bar foo"ok=td.Cmp(t,got,td.Re("(zip|bar)$"),"checks value %s",got)fmt.Println(ok)// Output:// true// false
Stringer example
t:=&testing.T{}// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foo bar")ok:=td.Cmp(t,got,td.Re("(zip|bar)$"),"checks value %s",got)fmt.Println(ok)// Output:// true
Error example
t:=&testing.T{}got:=errors.New("foo bar")ok:=td.Cmp(t,got,td.Re("(zip|bar)$"),"checks value %s",got)fmt.Println(ok)// Output:// true
Capture example
t:=&testing.T{}got:="foo bar biz"ok:=td.Cmp(t,got,td.Re(`^(\w+) (\w+) (\w+)$`,td.Set("biz","foo","bar")),"checks value %s",got)fmt.Println(ok)got="foo bar! biz"ok=td.Cmp(t,got,td.Re(`^(\w+) (\w+) (\w+)$`,td.Set("biz","foo","bar")),"checks value %s",got)fmt.Println(ok)// Output:// true// false
Multilines example
t:=&testing.T{}got:=`multi
lines
probably
more
than 4
`expectedRe:=`^multi
lines?
(probably|possibly)
more
than \d+
\z`ok:=td.Cmp(t,got,td.Re(expectedRe))fmt.Println("Raw multi-lines string matches:",ok)// But for strings with many, many, many lines, when the regexp// doesn't match, it is sometimes difficult to see where the regexp// failed in the string. Here td.List & td.Flatten can help to apply// regexp line per line (note expectedRe is not anchored anymore):expectedRe=`multi
lines?
(probably|possibly)
more
than \d+
`ok=td.Cmp(t,strings.Split(got,"\n"),td.List(td.Flatten(strings.Split(expectedRe,"\n"),func(linestring)any{returntd.Re(`^`+line+`\z`)})))fmt.Println("All string lines match:",ok)// Output:// Raw multi-lines string matches: true// All string lines match: true
Compiled example
t:=&testing.T{}expected:=regexp.MustCompile("(zip|bar)$")got:="foo bar"ok:=td.Cmp(t,got,td.Re(expected),"checks value %s",got)fmt.Println(ok)got="bar foo"ok=td.Cmp(t,got,td.Re(expected),"checks value %s",got)fmt.Println(ok)// Output:// true// false
CompiledStringer example
t:=&testing.T{}expected:=regexp.MustCompile("(zip|bar)$")// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foo bar")ok:=td.Cmp(t,got,td.Re(expected),"checks value %s",got)fmt.Println(ok)// Output:// true
CompiledError example
t:=&testing.T{}expected:=regexp.MustCompile("(zip|bar)$")got:=errors.New("foo bar")ok:=td.Cmp(t,got,td.Re(expected),"checks value %s",got)fmt.Println(ok)// Output:// true
CompiledCapture example
t:=&testing.T{}expected:=regexp.MustCompile(`^(\w+) (\w+) (\w+)$`)got:="foo bar biz"ok:=td.Cmp(t,got,td.Re(expected,td.Set("biz","foo","bar")),"checks value %s",got)fmt.Println(ok)got="foo bar! biz"ok=td.Cmp(t,got,td.Re(expected,td.Set("biz","foo","bar")),"checks value %s",got)fmt.Println(ok)// Output:// true// false
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.
t:=&testing.T{}got:="foo bar"ok:=td.CmpRe(t,got,"(zip|bar)$",nil,"checks value %s",got)fmt.Println(ok)got="bar foo"ok=td.CmpRe(t,got,"(zip|bar)$",nil,"checks value %s",got)fmt.Println(ok)// Output:// true// false
Stringer example
t:=&testing.T{}// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foo bar")ok:=td.CmpRe(t,got,"(zip|bar)$",nil,"checks value %s",got)fmt.Println(ok)// Output:// true
Error example
t:=&testing.T{}got:=errors.New("foo bar")ok:=td.CmpRe(t,got,"(zip|bar)$",nil,"checks value %s",got)fmt.Println(ok)// Output:// true
Capture example
t:=&testing.T{}got:="foo bar biz"ok:=td.CmpRe(t,got,`^(\w+) (\w+) (\w+)$`,td.Set("biz","foo","bar"),"checks value %s",got)fmt.Println(ok)got="foo bar! biz"ok=td.CmpRe(t,got,`^(\w+) (\w+) (\w+)$`,td.Set("biz","foo","bar"),"checks value %s",got)fmt.Println(ok)// Output:// true// false
Multilines example
t:=&testing.T{}got:=`multi
lines
probably
more
than 4
`expectedRe:=`^multi
lines?
(probably|possibly)
more
than \d+
\z`ok:=td.CmpRe(t,got,expectedRe,nil)fmt.Println("Raw multi-lines string matches:",ok)// But for strings with many, many, many lines, when the regexp// doesn't match, it is sometimes difficult to see where the regexp// failed in the string. Here td.List & td.Flatten can help to apply// regexp line per line (note expectedRe is not anchored anymore):expectedRe=`multi
lines?
(probably|possibly)
more
than \d+
`ok=td.Cmp(t,strings.Split(got,"\n"),td.List(td.Flatten(strings.Split(expectedRe,"\n"),func(linestring)any{returntd.Re(`^`+line+`\z`)})))fmt.Println("All string lines match:",ok)// Output:// Raw multi-lines string matches: true// All string lines match: true
Compiled example
t:=&testing.T{}expected:=regexp.MustCompile("(zip|bar)$")got:="foo bar"ok:=td.CmpRe(t,got,expected,nil,"checks value %s",got)fmt.Println(ok)got="bar foo"ok=td.CmpRe(t,got,expected,nil,"checks value %s",got)fmt.Println(ok)// Output:// true// false
CompiledStringer example
t:=&testing.T{}expected:=regexp.MustCompile("(zip|bar)$")// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foo bar")ok:=td.CmpRe(t,got,expected,nil,"checks value %s",got)fmt.Println(ok)// Output:// true
CompiledError example
t:=&testing.T{}expected:=regexp.MustCompile("(zip|bar)$")got:=errors.New("foo bar")ok:=td.CmpRe(t,got,expected,nil,"checks value %s",got)fmt.Println(ok)// Output:// true
CompiledCapture example
t:=&testing.T{}expected:=regexp.MustCompile(`^(\w+) (\w+) (\w+)$`)got:="foo bar biz"ok:=td.CmpRe(t,got,expected,td.Set("biz","foo","bar"),"checks value %s",got)fmt.Println(ok)got="foo bar! biz"ok=td.CmpRe(t,got,expected,td.Set("biz","foo","bar"),"checks value %s",got)fmt.Println(ok)// Output:// true// false
T.Re shortcut
func(t*T)Re(got,reg,captureany,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.
t:=td.NewT(&testing.T{})got:="foo bar"ok:=t.Re(got,"(zip|bar)$",nil,"checks value %s",got)fmt.Println(ok)got="bar foo"ok=t.Re(got,"(zip|bar)$",nil,"checks value %s",got)fmt.Println(ok)// Output:// true// false
Stringer example
t:=td.NewT(&testing.T{})// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foo bar")ok:=t.Re(got,"(zip|bar)$",nil,"checks value %s",got)fmt.Println(ok)// Output:// true
Error example
t:=td.NewT(&testing.T{})got:=errors.New("foo bar")ok:=t.Re(got,"(zip|bar)$",nil,"checks value %s",got)fmt.Println(ok)// Output:// true
Capture example
t:=td.NewT(&testing.T{})got:="foo bar biz"ok:=t.Re(got,`^(\w+) (\w+) (\w+)$`,td.Set("biz","foo","bar"),"checks value %s",got)fmt.Println(ok)got="foo bar! biz"ok=t.Re(got,`^(\w+) (\w+) (\w+)$`,td.Set("biz","foo","bar"),"checks value %s",got)fmt.Println(ok)// Output:// true// false
Multilines example
t:=td.NewT(&testing.T{})got:=`multi
lines
probably
more
than 4
`expectedRe:=`^multi
lines?
(probably|possibly)
more
than \d+
\z`ok:=t.Re(got,expectedRe,nil)fmt.Println("Raw multi-lines string matches:",ok)// But for strings with many, many, many lines, when the regexp// doesn't match, it is sometimes difficult to see where the regexp// failed in the string. Here td.List & td.Flatten can help to apply// regexp line per line (note expectedRe is not anchored anymore):expectedRe=`multi
lines?
(probably|possibly)
more
than \d+
`ok=t.Cmp(strings.Split(got,"\n"),td.List(td.Flatten(strings.Split(expectedRe,"\n"),func(linestring)any{returntd.Re(`^`+line+`\z`)})))fmt.Println("All string lines match:",ok)// Output:// Raw multi-lines string matches: true// All string lines match: true
Compiled example
t:=td.NewT(&testing.T{})expected:=regexp.MustCompile("(zip|bar)$")got:="foo bar"ok:=t.Re(got,expected,nil,"checks value %s",got)fmt.Println(ok)got="bar foo"ok=t.Re(got,expected,nil,"checks value %s",got)fmt.Println(ok)// Output:// true// false
CompiledStringer example
t:=td.NewT(&testing.T{})expected:=regexp.MustCompile("(zip|bar)$")// bytes.Buffer implements fmt.Stringergot:=bytes.NewBufferString("foo bar")ok:=t.Re(got,expected,nil,"checks value %s",got)fmt.Println(ok)// Output:// true
CompiledError example
t:=td.NewT(&testing.T{})expected:=regexp.MustCompile("(zip|bar)$")got:=errors.New("foo bar")ok:=t.Re(got,expected,nil,"checks value %s",got)fmt.Println(ok)// Output:// true
CompiledCapture example
t:=td.NewT(&testing.T{})expected:=regexp.MustCompile(`^(\w+) (\w+) (\w+)$`)got:="foo bar biz"ok:=t.Re(got,expected,td.Set("biz","foo","bar"),"checks value %s",got)fmt.Println(ok)got="foo bar! biz"ok=t.Re(got,expected,td.Set("biz","foo","bar"),"checks value %s",got)fmt.Println(ok)// Output:// true// false