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 }
77
78 for _, q := range testCases {
79 t.Run("", func(t *testing.T) {
80 protoQ := QToProto(q)
81 q2, err := QFromProto(protoQ)
82 if err != nil {
83 t.Fatal(err)
84 }
85 if diff := cmp.Diff(q.String(), q2.String()); diff != "" {
86 t.Fatalf("unexpected diff: %s", diff)
87 }
88 })
89 }
90
91}
92
93func regexpMustParse(s string) *syntax.Regexp {
94 re, err := syntax.Parse(s, syntax.Perl)
95 if err != nil {
96 panic(err)
97 }
98 return re
99}