fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

1// Copyright 2016 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package query 16 17import ( 18 "log" 19 "reflect" 20 "regexp/syntax" 21 "testing" 22 23 "github.com/grafana/regexp" 24) 25 26var _ = log.Println 27 28func TestQueryString(t *testing.T) { 29 q := &Or{[]Q{ 30 &And{[]Q{ 31 &Substring{Pattern: "hoi"}, 32 &Not{&Substring{Pattern: "hai"}}, 33 }}, 34 }} 35 got := q.String() 36 want := `(or (and substr:"hoi" (not substr:"hai")))` 37 38 if got != want { 39 t.Errorf("got %s, want %s", got, want) 40 } 41} 42 43func TestSimplify(t *testing.T) { 44 type testcase struct { 45 in Q 46 want Q 47 } 48 49 cases := []testcase{ 50 { 51 in: NewOr( 52 NewOr( 53 NewAnd(&Substring{Pattern: "hoi"}, 54 &Not{&Substring{Pattern: "hai"}}), 55 NewOr( 56 &Substring{Pattern: "zip"}, 57 &Substring{Pattern: "zap"}, 58 ))), 59 want: NewOr( 60 NewAnd( 61 &Substring{Pattern: "hoi"}, 62 &Not{&Substring{Pattern: "hai"}}), 63 &Substring{Pattern: "zip"}, 64 &Substring{Pattern: "zap"}), 65 }, 66 {in: &And{}, want: &Const{true}}, 67 {in: &Or{}, want: &Const{false}}, 68 {in: NewAnd(&Const{true}, &Const{false}), want: &Const{false}}, 69 {in: NewOr(&Const{false}, &Const{true}), want: &Const{true}}, 70 {in: &Not{&Const{true}}, want: &Const{false}}, 71 { 72 in: NewAnd( 73 &Substring{Pattern: "byte"}, 74 &Not{NewAnd(&Substring{Pattern: "byte"})}), 75 want: NewAnd( 76 &Substring{Pattern: "byte"}, 77 &Not{&Substring{Pattern: "byte"}}), 78 }, 79 } 80 81 for _, c := range cases { 82 got := Simplify(c.in) 83 if !reflect.DeepEqual(got, c.want) { 84 t.Errorf("got %s, want %s", got, c.want) 85 } 86 } 87} 88 89func TestMap(t *testing.T) { 90 in := NewAnd(&Substring{Pattern: "bla"}, &Not{&Repo{Regexp: regexp.MustCompile("foo")}}) 91 out := NewAnd(&Substring{Pattern: "bla"}, &Not{&Const{false}}) 92 93 f := func(q Q) Q { 94 if _, ok := q.(*Repo); ok { 95 return &Const{false} 96 } 97 return q 98 } 99 got := Map(in, f) 100 if !reflect.DeepEqual(got, out) { 101 t.Errorf("got %v, want %v", got, out) 102 } 103} 104 105func TestVisitAtoms(t *testing.T) { 106 in := NewAnd(&Substring{}, &Repo{}, &Not{&Const{}}) 107 count := 0 108 VisitAtoms(in, func(q Q) { 109 count++ 110 }) 111 if count != 3 { 112 t.Errorf("got %d, want 3", count) 113 } 114} 115 116func TestExpandFileContent(t *testing.T) { 117 re, _ := syntax.Parse("foo", syntax.Perl) 118 119 cases := []struct { 120 q Q 121 want string 122 }{ 123 { 124 q: &Substring{FileName: true, Content: true}, 125 want: "(or file_substr:\"\" content_substr:\"\")", 126 }, 127 { 128 q: &Substring{FileName: false, Content: false}, 129 want: "(or file_substr:\"\" content_substr:\"\")", 130 }, 131 132 { 133 q: &Substring{FileName: true, Content: false}, 134 want: "file_substr:\"\"", 135 }, 136 { 137 q: &Substring{FileName: false, Content: true}, 138 want: "content_substr:\"\"", 139 }, 140 { 141 q: &Regexp{Regexp: re, FileName: true, Content: true}, 142 want: "(or file_regex:\"foo\" regex:\"foo\")", 143 }, 144 { 145 q: &Regexp{Regexp: re, FileName: false, Content: false}, 146 want: "(or file_regex:\"foo\" regex:\"foo\")", 147 }, 148 149 { 150 q: &Regexp{Regexp: re, FileName: true, Content: false}, 151 want: "file_regex:\"foo\"", 152 }, 153 { 154 q: &Regexp{Regexp: re, FileName: false, Content: true}, 155 want: "regex:\"foo\"", 156 }, 157 } 158 159 for _, tt := range cases { 160 t.Run("", func(t *testing.T) { 161 if got := ExpandFileContent(tt.q); got.String() != tt.want { 162 t.Fatalf("got %s, want %s\n", got.String(), tt.want) 163 } 164 }) 165 } 166}