fork of https://github.com/sourcegraph/zoekt
1package query
2
3import (
4 "regexp/syntax"
5 "testing"
6
7 "github.com/RoaringBitmap/roaring"
8 "github.com/google/go-cmp/cmp"
9 "github.com/grafana/regexp"
10)
11
12func TestQueryRoundtrip(t *testing.T) {
13 testCases := []Q{
14 &Regexp{
15 Regexp: regexpMustParse("foo"),
16 FileName: true,
17 Content: true,
18 CaseSensitive: true,
19 },
20 &Symbol{
21 Expr: &Language{
22 Language: "go",
23 },
24 },
25 &Language{
26 Language: "typescript",
27 },
28 &Const{
29 Value: true,
30 },
31 &Repo{
32 Regexp: regexp.MustCompile("github.com/foo/bar"),
33 },
34 &RepoRegexp{
35 Regexp: regexp.MustCompile("github.com/foo.*"),
36 },
37 &BranchesRepos{
38 List: []BranchRepos{{
39 Branch: "test",
40 Repos: func() *roaring.Bitmap {
41 bm := roaring.New()
42 bm.Add(3)
43 bm.Add(34)
44 return bm
45 }(),
46 }},
47 },
48 NewRepoIDs(3, 4, 5),
49 &Branch{
50 Pattern: "master",
51 Exact: true,
52 },
53 NewRepoSet("test1", "test2"),
54 NewFileNameSet("test3", "test4"),
55 &And{
56 Children: []Q{
57 &Language{Language: "go"},
58 &Type{
59 Child: &Substring{Pattern: "interface"},
60 Type: TypeFileMatch,
61 },
62 },
63 },
64 &Or{
65 Children: []Q{
66 &Language{Language: "go"},
67 &Type{
68 Child: &Substring{Pattern: "interface"},
69 Type: TypeFileMatch,
70 },
71 },
72 },
73 &Not{
74 Child: &Language{Language: "go"},
75 },
76 &Boost{
77 Child: &Or{
78 Children: []Q{
79 &And{
80 Children: []Q{
81 &Substring{Pattern: "foo"},
82 &Substring{Pattern: "bar"},
83 },
84 },
85 &Substring{Pattern: "foo bar"},
86 },
87 },
88 Boost: 20,
89 },
90 }
91
92 for _, q := range testCases {
93 t.Run("", func(t *testing.T) {
94 protoQ := QToProto(q)
95 q2, err := QFromProto(protoQ)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if diff := cmp.Diff(q.String(), q2.String()); diff != "" {
100 t.Fatalf("unexpected diff: %s", diff)
101 }
102 })
103 }
104}
105
106func TestRegexpProtoUsesRegexpString(t *testing.T) {
107 q := &Regexp{Regexp: regexpMustParse(`a.*b`)}
108 protoQ := q.ToProto()
109
110 if got, want := protoQ.GetRegexp(), q.RegexpString(); got != want {
111 t.Fatalf("ToProto().Regexp = %q, want %q", got, want)
112 }
113}
114
115func regexpMustParse(s string) *syntax.Regexp {
116 re, err := syntax.Parse(s, syntax.Perl)
117 if err != nil {
118 panic(err)
119 }
120 return re
121}