|
5 | 5 | package pathtoregexp
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "errors" |
8 | 9 | "fmt"
|
9 | 10 | "reflect"
|
10 | 11 | "testing"
|
@@ -3179,6 +3180,80 @@ func TestPathToRegexp(t *testing.T) {
|
3179 | 3180 | })
|
3180 | 3181 | }
|
3181 | 3182 |
|
| 3183 | +func TestMustCompile(t *testing.T) { |
| 3184 | + r := MustCompile("/user/:id(\\d+)", nil) |
| 3185 | + if r == nil { |
| 3186 | + t.Error("got nil want func") |
| 3187 | + } |
| 3188 | +} |
| 3189 | + |
| 3190 | +func TestDecodeURI(t *testing.T) { |
| 3191 | + tests := map[string]string{ |
| 3192 | + "%3B%2F%3F%3A%40%26%3D%2B%24%2C%23": "%3B%2F%3F%3A%40%26%3D%2B%24%2C%23", |
| 3193 | + "http%3A%2F%2Fwww.example.com%2Fstring%20with%20%2B%20and%20%3F%20and%20%26%20and%20spaces": "http%3A%2F%2Fwww.example.com%2Fstring with %2B and %3F and %26 and spaces", |
| 3194 | + "https://developer.mozilla.org/ru/docs/JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B": "https://developer.mozilla.org/ru/docs/JavaScript_шеллы", |
| 3195 | + } |
| 3196 | + for k, v := range tests { |
| 3197 | + result := decodeURI(k) |
| 3198 | + if result != v { |
| 3199 | + t.Errorf("got %v want %v", result, v) |
| 3200 | + } |
| 3201 | + } |
| 3202 | + |
| 3203 | + t.Run("malformed URI sequence", func(t *testing.T) { |
| 3204 | + defer func() { |
| 3205 | + if err := recover(); err == nil { |
| 3206 | + t.Errorf("got nil want panic") |
| 3207 | + } |
| 3208 | + }() |
| 3209 | + decodeURI("%E0%A4%A") |
| 3210 | + }) |
| 3211 | +} |
| 3212 | + |
| 3213 | +func TestAnyString(t *testing.T) { |
| 3214 | + tests := map[string][]string{ |
| 3215 | + "foo": {"", "", "foo", ""}, |
| 3216 | + "bar": {"bar", "", "foo", ""}, |
| 3217 | + "": {"", "", "", ""}, |
| 3218 | + } |
| 3219 | + for k, v := range tests { |
| 3220 | + result := anyString(v...) |
| 3221 | + if result != k { |
| 3222 | + t.Errorf("got %v want %v", result, k) |
| 3223 | + } |
| 3224 | + } |
| 3225 | +} |
| 3226 | + |
| 3227 | +func TestQuote(t *testing.T) { |
| 3228 | + tests := map[string]string{ |
| 3229 | + "foo": "`foo`", |
| 3230 | + "`bar`": "\"`bar`\"", |
| 3231 | + } |
| 3232 | + for k, v := range tests { |
| 3233 | + result := quote(k) |
| 3234 | + if result != v { |
| 3235 | + t.Errorf("got %v want %v", result, v) |
| 3236 | + } |
| 3237 | + } |
| 3238 | +} |
| 3239 | + |
| 3240 | +func TestMust(t *testing.T) { |
| 3241 | + r := regexp2.MustCompile("^\\/([^\\/]+)$", regexp2.None) |
| 3242 | + result := Must(r, nil) |
| 3243 | + if result != r { |
| 3244 | + t.Errorf("got %v want %v", result, r) |
| 3245 | + } |
| 3246 | + |
| 3247 | + t.Run("non nil error", func(t *testing.T) { |
| 3248 | + defer func() { |
| 3249 | + if err := recover(); err == nil { |
| 3250 | + t.Errorf("got nil want panic") |
| 3251 | + } |
| 3252 | + }() |
| 3253 | + result = Must(r, errors.New("error")) |
| 3254 | + }) |
| 3255 | +} |
| 3256 | + |
3182 | 3257 | func exec(r *regexp2.Regexp, str string) []string {
|
3183 | 3258 | m, _ := r.FindStringMatch(str)
|
3184 | 3259 | if m == nil {
|
|
0 commit comments