Repository navigation
Expand file tree
/
Copy pathissue175_test.go
More file actions
71 lines (61 loc) · 2.04 KB
/
Copy pathissue175_test.go
File metadata and controls
71 lines (61 loc) · 2.04 KB
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
package coregex
import (
"fmt"
"reflect"
"regexp"
"testing"
)
// TestIssue175_LazyQuantifier verifies that lazy quantifiers are honored by the
// char class and composite strategies, which used to match greedily.
// See https://github.com/coregx/coregex/issues/175
func TestIssue175_LazyQuantifier(t *testing.T) {
tests := []struct {
pattern string
input string
}{
// Reported cases
{`\d+?`, "11"},
{`\w+?`, "ab"},
{`(?i)[a-c]+?`, "abc"},
{`\w+?\s??\w?`, "1A\nc"},
{`a*?b`, "aab"},
// (?U) swaps the meaning of the quantifiers
{`(?U)\d+`, "11"},
// Composite char class patterns
{`[a-c]+?[a-c]+?`, "abcabc"},
{`[a-z]{1,3}?[a-c]*?`, "abcabc"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%q/%q", tt.pattern, tt.input), func(t *testing.T) {
re := MustCompile(tt.pattern)
reStd := regexp.MustCompile(tt.pattern)
got := re.FindAllStringIndex(tt.input, -1)
want := reStd.FindAllStringIndex(tt.input, -1)
if !reflect.DeepEqual(got, want) {
t.Errorf("FindAllStringIndex(%q, %q): coregex=%v, stdlib=%v",
tt.pattern, tt.input, got, want)
}
gotIdx := re.FindStringIndex(tt.input)
wantIdx := reStd.FindStringIndex(tt.input)
if !reflect.DeepEqual(gotIdx, wantIdx) {
t.Errorf("FindStringIndex(%q, %q): coregex=%v, stdlib=%v",
tt.pattern, tt.input, gotIdx, wantIdx)
}
if got, want := re.MatchString(tt.input), reStd.MatchString(tt.input); got != want {
t.Errorf("MatchString(%q, %q): coregex=%v, stdlib=%v", tt.pattern, tt.input, got, want)
}
gotRepl := re.ReplaceAllString(tt.input, "<$0>")
wantRepl := reStd.ReplaceAllString(tt.input, "<$0>")
if gotRepl != wantRepl {
t.Errorf("ReplaceAllString(%q, %q): coregex=%q, stdlib=%q",
tt.pattern, tt.input, gotRepl, wantRepl)
}
gotSub := re.FindAllStringSubmatchIndex(tt.input, -1)
wantSub := reStd.FindAllStringSubmatchIndex(tt.input, -1)
if !reflect.DeepEqual(gotSub, wantSub) {
t.Errorf("FindAllStringSubmatchIndex(%q, %q): coregex=%v, stdlib=%v",
tt.pattern, tt.input, gotSub, wantSub)
}
})
}
}