| 1 |
// Copyright (c) 2018, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"regexp" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/test" |
| 15 |
"github.com/maxatome/go-testdeep/td" |
| 16 |
) |
| 17 |
|
| 18 |
func TestRe(t *testing.T) { |
| 19 |
// |
| 20 |
// string |
| 21 |
checkOK(t, "foo bar test", td.Re("bar")) |
| 22 |
checkOK(t, "foo bar test", td.Re(regexp.MustCompile("test$"))) |
| 23 |
|
| 24 |
checkOK(t, "foo bar test", |
| 25 |
td.ReAll(`(\w+)`, td.Bag("bar", "test", "foo"))) |
| 26 |
|
| 27 |
type MyString string |
| 28 |
checkOK(t, MyString("Ho zz hoho"), |
| 29 |
td.ReAll("(?i)(ho)", []string{"Ho", "ho", "ho"})) |
| 30 |
checkOK(t, MyString("Ho zz hoho"), |
| 31 |
td.ReAll("(?i)(ho)", []any{"Ho", "ho", "ho"})) |
| 32 |
|
| 33 |
// error interface |
| 34 |
checkOK(t, errors.New("pipo bingo"), td.Re("bin")) |
| 35 |
// fmt.Stringer interface |
| 36 |
checkOK(t, MyStringer{}, td.Re("bin")) |
| 37 |
|
| 38 |
checkError(t, 12, td.Re("bar"), |
| 39 |
expectedError{ |
| 40 |
Message: mustBe("bad type"), |
| 41 |
Path: mustBe("DATA"), |
| 42 |
Got: mustBe("int"), |
| 43 |
Expected: mustBe( |
| 44 |
"string (convertible) OR fmt.Stringer OR error OR []uint8"), |
| 45 |
}) |
| 46 |
|
| 47 |
checkError(t, "foo bar test", td.Re("pipo"), |
| 48 |
expectedError{ |
| 49 |
Message: mustBe("does not match Regexp"), |
| 50 |
Path: mustBe("DATA"), |
| 51 |
Got: mustContain(`"foo bar test"`), |
| 52 |
Expected: mustBe("pipo"), |
| 53 |
}) |
| 54 |
|
| 55 |
checkError(t, "foo bar test", td.Re("(pi)(po)", []string{"pi", "po"}), |
| 56 |
expectedError{ |
| 57 |
Message: mustBe("does not match Regexp"), |
| 58 |
Path: mustBe("DATA"), |
| 59 |
Got: mustContain(`"foo bar test"`), |
| 60 |
Expected: mustBe("(pi)(po)"), |
| 61 |
}) |
| 62 |
checkError(t, "foo bar test", td.Re("(pi)(po)", []any{"pi", "po"}), |
| 63 |
expectedError{ |
| 64 |
Message: mustBe("does not match Regexp"), |
| 65 |
Path: mustBe("DATA"), |
| 66 |
Got: mustContain(`"foo bar test"`), |
| 67 |
Expected: mustBe("(pi)(po)"), |
| 68 |
}) |
| 69 |
|
| 70 |
// |
| 71 |
// bytes |
| 72 |
checkOK(t, []byte("foo bar test"), td.Re("bar")) |
| 73 |
|
| 74 |
checkOK(t, []byte("foo bar test"), |
| 75 |
td.ReAll(`(\w+)`, td.Bag("bar", "test", "foo"))) |
| 76 |
|
| 77 |
type MySlice []byte |
| 78 |
checkOK(t, MySlice("Ho zz hoho"), |
| 79 |
td.ReAll("(?i)(ho)", []string{"Ho", "ho", "ho"})) |
| 80 |
checkOK(t, MySlice("Ho zz hoho"), |
| 81 |
td.ReAll("(?i)(ho)", []any{"Ho", "ho", "ho"})) |
| 82 |
|
| 83 |
checkError(t, []int{12}, td.Re("bar"), |
| 84 |
expectedError{ |
| 85 |
Message: mustBe("bad slice type"), |
| 86 |
Path: mustBe("DATA"), |
| 87 |
Got: mustBe("[]int"), |
| 88 |
Expected: mustBe("[]uint8"), |
| 89 |
}) |
| 90 |
|
| 91 |
checkError(t, []byte("foo bar test"), td.Re("pipo"), |
| 92 |
expectedError{ |
| 93 |
Message: mustBe("does not match Regexp"), |
| 94 |
Path: mustBe("DATA"), |
| 95 |
Got: mustContain(`foo bar test`), |
| 96 |
Expected: mustBe("pipo"), |
| 97 |
}) |
| 98 |
|
| 99 |
checkError(t, []byte("foo bar test"), |
| 100 |
td.Re("(pi)(po)", []string{"pi", "po"}), |
| 101 |
expectedError{ |
| 102 |
Message: mustBe("does not match Regexp"), |
| 103 |
Path: mustBe("DATA"), |
| 104 |
Got: mustContain(`foo bar test`), |
| 105 |
Expected: mustBe("(pi)(po)"), |
| 106 |
}) |
| 107 |
checkError(t, []byte("foo bar test"), |
| 108 |
td.Re("(pi)(po)", []any{"pi", "po"}), |
| 109 |
expectedError{ |
| 110 |
Message: mustBe("does not match Regexp"), |
| 111 |
Path: mustBe("DATA"), |
| 112 |
Got: mustContain(`foo bar test`), |
| 113 |
Expected: mustBe("(pi)(po)"), |
| 114 |
}) |
| 115 |
|
| 116 |
// |
| 117 |
// Bad usage |
| 118 |
const ( |
| 119 |
ur = "(STRING|*regexp.Regexp[, NON_NIL_CAPTURE])" |
| 120 |
ua = "(STRING|*regexp.Regexp, NON_NIL_CAPTURE)" |
| 121 |
) |
| 122 |
|
| 123 |
checkError(t, "never tested", |
| 124 |
td.Re(123), |
| 125 |
expectedError{ |
| 126 |
Message: mustBe("bad usage of Re operator"), |
| 127 |
Path: mustBe("DATA"), |
| 128 |
Summary: mustBe("usage: Re" + ur + ", but received int as 1st parameter"), |
| 129 |
}) |
| 130 |
|
| 131 |
checkError(t, "never tested", |
| 132 |
td.ReAll(123, nil), |
| 133 |
expectedError{ |
| 134 |
Message: mustBe("bad usage of ReAll operator"), |
| 135 |
Path: mustBe("DATA"), |
| 136 |
Summary: mustBe("usage: ReAll" + ua + ", but received int as 1st parameter"), |
| 137 |
}) |
| 138 |
|
| 139 |
checkError(t, "never tested", |
| 140 |
td.Re("bar", []string{}, 1), |
| 141 |
expectedError{ |
| 142 |
Message: mustBe("bad usage of Re operator"), |
| 143 |
Path: mustBe("DATA"), |
| 144 |
Summary: mustBe("usage: Re" + ur + ", too many parameters"), |
| 145 |
}) |
| 146 |
|
| 147 |
checkError(t, "never tested", |
| 148 |
td.ReAll(123, 456), |
| 149 |
expectedError{ |
| 150 |
Message: mustBe("bad usage of ReAll operator"), |
| 151 |
Path: mustBe("DATA"), |
| 152 |
Summary: mustBe("usage: ReAll" + ua + ", but received int as 1st parameter"), |
| 153 |
}) |
| 154 |
|
| 155 |
checkError(t, "never tested", |
| 156 |
td.ReAll(`12[3,4`, nil), |
| 157 |
expectedError{ |
| 158 |
Message: mustBe("invalid regexp given to ReAll operator"), |
| 159 |
Path: mustBe("DATA"), |
| 160 |
Summary: mustContain("error parsing regexp: "), |
| 161 |
}) |
| 162 |
|
| 163 |
// Erroneous op |
| 164 |
test.EqualStr(t, td.Re(123).String(), "Re(<ERROR>)") |
| 165 |
test.EqualStr(t, td.ReAll(123, nil).String(), "ReAll(<ERROR>)") |
| 166 |
} |
| 167 |
|
| 168 |
func TestReTypeBehind(t *testing.T) { |
| 169 |
equalTypes(t, td.Re("x"), nil) |
| 170 |
equalTypes(t, td.ReAll("x", nil), nil) |
| 171 |
|
| 172 |
// Erroneous op |
| 173 |
equalTypes(t, td.Re(123), nil) |
| 174 |
equalTypes(t, td.ReAll(123, nil), nil) |
| 175 |
} |