-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregex_test.go
57 lines (46 loc) · 1.89 KB
/
regex_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
package liquid
import (
"reflect"
"regexp"
"testing"
)
func testRegex(t *testing.T, r *regexp.Regexp, raw string, want []string) {
got := r.FindAllString(raw, -1)
if len(got) == 0 {
got = make([]string, 0)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Regex %v failed, want: %v, got: %v", r.String(), want, got)
}
}
func TestEmpty(t *testing.T) {
testRegex(t, quotedFragmentRegexp, ``, []string{})
}
func TestQuote(t *testing.T) {
testRegex(t, quotedFragmentRegexp, `"arg 1"`, []string{`"arg 1"`})
}
func TestWords(t *testing.T) {
testRegex(t, quotedFragmentRegexp, `arg1, arg2`, []string{`arg1`, `arg2`})
}
func TestTags(t *testing.T) {
testRegex(t, quotedFragmentRegexp, `<tr> </tr>`, []string{`<tr>`, `</tr>`})
testRegex(t, quotedFragmentRegexp, `<tr></tr>`, []string{`<tr></tr>`})
testRegex(t, quotedFragmentRegexp, `<style class="hello">' </style>`, []string{`<style`, `class="hello">`, `</style>`})
}
func TestDoubleQuotedWords(t *testing.T) {
testRegex(t, quotedFragmentRegexp, `'arg1 arg2 "arg 3"`, []string{`arg1`, `arg2`, `"arg 3"`})
}
func TestSingleQuotedWords(t *testing.T) {
testRegex(t, quotedFragmentRegexp, `arg1 arg2 'arg 3'`, []string{`arg1`, `arg2`, `'arg 3'`})
}
func TestQuotedWordsInTheMiddle(t *testing.T) {
testRegex(t, quotedFragmentRegexp, `arg1 arg2 "arg 3" arg4 `, []string{`arg1`, `arg2`, `"arg 3"`, `arg4`})
}
func TestVariableParserRegexp(t *testing.T) {
testRegex(t, variableParserRegexp, `var`, []string{`var`})
testRegex(t, variableParserRegexp, `var.method`, []string{`var`, `method`})
testRegex(t, variableParserRegexp, `var[method]`, []string{`var`, `[method]`})
testRegex(t, variableParserRegexp, `var[method][0]`, []string{`var`, `[method]`, `[0]`})
testRegex(t, variableParserRegexp, `var["method"][0]`, []string{`var`, `["method"]`, `[0]`})
testRegex(t, variableParserRegexp, `var[method][0].method`, []string{`var`, `[method]`, `[0]`, `method`})
}