-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatch_test.go
95 lines (83 loc) · 2.51 KB
/
match_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) 2016 Kohei YOSHIDA. All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of The BSD 3-Clause License
// that can be found in the LICENSE file.
package uritemplate
import (
"fmt"
"strconv"
"strings"
"testing"
)
func ExampleTemplate_Match() {
tmpl := MustNew("https://example.com/dictionary/{term:1}/{term}")
match := tmpl.Match("https://example.com/dictionary/c/cat")
if match == nil {
fmt.Println("not matched")
return
}
fmt.Printf("term:1 is %q\n", match.Get("term:1").String())
fmt.Printf("term is %q\n", match.Get("term").String())
// Output:
// term:1 is "c"
// term is "cat"
}
func TestTemplate_Match(t *testing.T) {
for i, c := range testTemplateCases {
if c.failMatch {
continue
}
tmpl, err := New(c.raw)
if err != nil {
t.Errorf("unexpected error on %q: %#v", c.raw, err)
continue
}
match := tmpl.Match(c.expected)
if match == nil {
t.Errorf("%d: failed to match %q against %q", i, c.raw, c.expected)
t.Log(tmpl.prog.String())
continue
}
for name, actual := range match {
var expected Value
if semi := strings.Index(name, ":"); semi >= 0 {
maxlen, _ := strconv.Atoi(name[semi+1:])
name = name[:semi]
expected = testExpressionExpandVarMap[name]
if expected.T != ValueTypeString {
t.Errorf("%d: failed to match %q against %q", i, c.raw, c.expected)
t.Errorf("%d: expected %#v, but got %#v", i, expected, actual)
continue
}
if v := expected.V[0]; len(v) > maxlen {
expected.V = []string{v[:maxlen]}
}
} else {
expected = testExpressionExpandVarMap[name]
}
if actual.T != expected.T {
t.Errorf("%d: failed to match %q against %q", i, c.raw, c.expected)
t.Errorf("%d: expected %#v, but got %#v", i, expected, actual)
} else if le, la := len(expected.V), len(actual.V); le == la {
for i := range actual.V {
if actual.V[i] != expected.V[i] {
t.Errorf("%d: failed to match %q against %q", i, c.raw, c.expected)
t.Errorf("%d: expected %#v, but got %#v", i, expected, actual)
break
}
}
} else if !(le == 0 && la == 1 && actual.V[0] == "") { // not undef
t.Errorf("%d: failed to match %q against %q", i, c.raw, c.expected)
t.Errorf("%d: expected %#v, but got %#v", i, expected, actual)
}
}
}
}
func TestTemplate_NotMatch(t *testing.T) {
tmpl := MustNew("https://example.com/foo{?bar}")
match := tmpl.Match("https://example.com/foobaz")
if match != nil {
t.Errorf("must not match")
}
}