-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser_test.go
66 lines (61 loc) · 1.81 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2017 Razon Yang. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fastrouter
import (
"fmt"
"reflect"
"testing"
)
type testPattern struct {
reg string
params []string
hasTrailingSlashes bool
err error
}
func TestPatternParser_Parse(t *testing.T) {
emptyParams := []string{}
testPatterns := map[string]testPattern{
"": {"",
emptyParams,
false,
fmt.Errorf(`the pattern MUST begin with '/' in pattern %q`, ""),
},
"/": {"//?",
emptyParams,
false,
nil,
},
"users": {"",
emptyParams,
false,
fmt.Errorf(`the pattern MUST begin with '/' in pattern %q`, "users"),
},
`/users`: {"/users/?", emptyParams, false, nil},
`/users/`: {"/users/?", emptyParams, true, nil},
`/users/<id>`: {"/users/([^/]+)/?", []string{"id"}, false, nil},
`/users/<id:\d+>`: {`/users/(\d+)/?`, []string{"id"}, false, nil},
`/posts/<year:\d{4}>/<month:\d{2}>/<title>`: {
`/posts/(\d{4})/(\d{2})/([^/]+)/?`,
[]string{"year", "month", "title"},
false,
nil,
},
}
parser := NewParser()
for pattern, v := range testPatterns {
reg, params, hasTrailingSlashes, err := parser.Parse(pattern)
if v.reg != reg {
t.Errorf("expect the reg of pattern %q to be %q, but got %q", pattern, v.reg, reg)
}
if !compareSlice(v.params, params) {
t.Errorf("expect the params of pattern %q to be %v, but got %v", pattern, v.params, params)
}
if v.hasTrailingSlashes != hasTrailingSlashes {
t.Errorf("expect the hasTrailingSlashes of pattern %q to be %v, but got %v", pattern, v.hasTrailingSlashes, hasTrailingSlashes)
}
if !reflect.DeepEqual(v.err, err) {
t.Errorf("expect the err of pattern %q to be %v, but got %v", pattern, v.err, err)
}
}
}