forked from google/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_test.go
128 lines (119 loc) · 3.57 KB
/
eval_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2020 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zoekt
import (
"reflect"
"regexp/syntax"
"strings"
"testing"
"github.com/google/zoekt/query"
)
var opnames = map[syntax.Op]string{
syntax.OpNoMatch: "OpNoMatch",
syntax.OpEmptyMatch: "OpEmptyMatch",
syntax.OpLiteral: "OpLiteral",
syntax.OpCharClass: "OpCharClass",
syntax.OpAnyCharNotNL: "OpAnyCharNotNL",
syntax.OpAnyChar: "OpAnyChar",
syntax.OpBeginLine: "OpBeginLine",
syntax.OpEndLine: "OpEndLine",
syntax.OpBeginText: "OpBeginText",
syntax.OpEndText: "OpEndText",
syntax.OpWordBoundary: "OpWordBoundary",
syntax.OpNoWordBoundary: "OpNoWordBoundary",
syntax.OpCapture: "OpCapture",
syntax.OpStar: "OpStar",
syntax.OpPlus: "OpPlus",
syntax.OpQuest: "OpQuest",
syntax.OpRepeat: "OpRepeat",
syntax.OpConcat: "OpConcat",
syntax.OpAlternate: "OpAlternate",
}
func printRegexp(t *testing.T, r *syntax.Regexp, lvl int) {
t.Logf("%s%s ch: %d", strings.Repeat(" ", lvl), opnames[r.Op], len(r.Sub))
for _, s := range r.Sub {
printRegexp(t, s, lvl+1)
}
}
func substrMT(pattern string) matchTree {
d := &indexData{}
mt, _ := d.newSubstringMatchTree(&query.Substring{
Pattern: pattern,
})
return mt
}
func TestRegexpParse(t *testing.T) {
type testcase struct {
in string
query matchTree
isEquivalent bool
}
cases := []testcase{
{"(foo|)bar", substrMT("bar"), false},
{"(foo|)", &bruteForceMatchTree{}, false},
{"(foo|bar)baz.*bla", &andMatchTree{[]matchTree{
&orMatchTree{[]matchTree{
substrMT("foo"),
substrMT("bar"),
}},
substrMT("baz"),
substrMT("bla"),
}}, false},
{
"^[a-z](People)+barrabas$",
&andMatchTree{[]matchTree{
substrMT("People"),
substrMT("barrabas"),
}}, false,
},
{"foo", substrMT("foo"), true},
{"^foo", substrMT("foo"), false},
{"(foo) (bar)", &andMatchTree{[]matchTree{substrMT("foo"), substrMT("bar")}}, false},
{"(thread|needle|haystack)", &orMatchTree{[]matchTree{
substrMT("thread"),
substrMT("needle"),
substrMT("haystack"),
}}, true},
{"(foo)(?-s:.)*?(bar)", &andLineMatchTree{andMatchTree{[]matchTree{
substrMT("foo"),
substrMT("bar"),
}}}, false},
{"(foo)(?-s:.)*?[[:space:]](?-s:.)*?(bar)", &andMatchTree{[]matchTree{
substrMT("foo"),
substrMT("bar"),
}}, false},
{"(foo){2,}", substrMT("foo"), false},
{"(...)(...)", &bruteForceMatchTree{}, false},
}
for _, c := range cases {
r, err := syntax.Parse(c.in, syntax.Perl)
if err != nil {
t.Errorf("Parse(%q): %v", c.in, err)
continue
}
d := indexData{}
q := query.Regexp{
Regexp: r,
}
gotQuery, isEq, _, _ := d.regexpToMatchTreeRecursive(q.Regexp, 3, q.FileName, q.CaseSensitive)
if !reflect.DeepEqual(c.query, gotQuery) {
printRegexp(t, r, 0)
t.Errorf("regexpToQuery(%q): got %v, want %v", c.in, gotQuery, c.query)
}
if isEq != c.isEquivalent {
printRegexp(t, r, 0)
t.Errorf("regexpToQuery(%q): got %v, want %v", c.in, isEq, c.isEquivalent)
}
}
}