fix server-rendered results by generating result pages into a buffer and reading...
[debiancodesearch.git] / index / regexp_test.go
blobae0dd807c99528bc1645bcd47928e2956a026ec2
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package index
7 import (
8 "regexp/syntax"
9 "testing"
12 var queryTests = []struct {
13 re string
14 q string
16 {`Abcdef`, `"Abc" "bcd" "cde" "def"`},
17 {`(abc)(def)`, `"abc" "bcd" "cde" "def"`},
18 {`abc.*(def|ghi)`, `"abc" ("def"|"ghi")`},
19 {`abc(def|ghi)`, `"abc" ("bcd" "cde" "def")|("bcg" "cgh" "ghi")`},
20 {`a+hello`, `"ahe" "ell" "hel" "llo"`},
21 {`(a+hello|b+world)`, `("ahe" "ell" "hel" "llo")|("bwo" "orl" "rld" "wor")`},
22 {`a*bbb`, `"bbb"`},
23 {`a?bbb`, `"bbb"`},
24 {`(bbb)a?`, `"bbb"`},
25 {`(bbb)a*`, `"bbb"`},
26 {`^abc`, `"abc"`},
27 {`abc$`, `"abc"`},
28 {`ab[cde]f`, `("abc" "bcf")|("abd" "bdf")|("abe" "bef")`},
29 {`(abc|bac)de`, `"cde" ("abc" "bcd")|("acd" "bac")`},
31 // These don't have enough letters for a trigram, so they return the
32 // always matching query "+".
33 {`ab[^cde]f`, `+`},
34 {`ab.f`, `+`},
35 {`.`, `+`},
36 {`()`, `+`},
38 // No matches.
39 {`[^\s\S]`, `-`},
41 // Factoring works.
42 {`(abc|abc)`, `"abc"`},
43 {`(ab|ab)c`, `"abc"`},
44 {`ab(cab|cat)`, `"abc" "bca" ("cab"|"cat")`},
45 {`(z*(abc|def)z*)(z*(abc|def)z*)`, `("abc"|"def")`},
46 {`(z*abcz*defz*)|(z*abcz*defz*)`, `"abc" "def"`},
47 {`(z*abcz*defz*(ghi|jkl)z*)|(z*abcz*defz*(mno|prs)z*)`,
48 `"abc" "def" ("ghi"|"jkl"|"mno"|"prs")`},
49 {`(z*(abcz*def)|(ghiz*jkl)z*)|(z*(mnoz*prs)|(tuvz*wxy)z*)`,
50 `("abc" "def")|("ghi" "jkl")|("mno" "prs")|("tuv" "wxy")`},
51 {`(z*abcz*defz*)(z*(ghi|jkl)z*)`, `"abc" "def" ("ghi"|"jkl")`},
52 {`(z*abcz*defz*)|(z*(ghi|jkl)z*)`, `("ghi"|"jkl")|("abc" "def")`},
54 // analyze keeps track of multiple possible prefix/suffixes.
55 {`[ab][cd][ef]`, `("ace"|"acf"|"ade"|"adf"|"bce"|"bcf"|"bde"|"bdf")`},
56 {`ab[cd]e`, `("abc" "bce")|("abd" "bde")`},
58 // Different sized suffixes.
59 {`(a|ab)cde`, `"cde" ("abc" "bcd")|("acd")`},
60 {`(a|b|c|d)(ef|g|hi|j)`, `+`},
62 {`(?s).`, `+`},
64 // Expanding case.
65 {`(?i)a~~`, `("A~~"|"a~~")`},
66 {`(?i)ab~`, `("AB~"|"Ab~"|"aB~"|"ab~")`},
67 {`(?i)abc`, `("ABC"|"ABc"|"AbC"|"Abc"|"aBC"|"aBc"|"abC"|"abc")`},
68 {`(?i)abc|def`, `("ABC"|"ABc"|"AbC"|"Abc"|"DEF"|"DEf"|"DeF"|"Def"|"aBC"|"aBc"|"abC"|"abc"|"dEF"|"dEf"|"deF"|"def")`},
69 {`(?i)abcd`, `("ABC"|"ABc"|"AbC"|"Abc"|"aBC"|"aBc"|"abC"|"abc") ("BCD"|"BCd"|"BcD"|"Bcd"|"bCD"|"bCd"|"bcD"|"bcd")`},
70 {`(?i)abc|abc`, `("ABC"|"ABc"|"AbC"|"Abc"|"aBC"|"aBc"|"abC"|"abc")`},
72 // Word boundary.
73 {`\b`, `+`},
74 {`\B`, `+`},
75 {`\babc`, `"abc"`},
76 {`\Babc`, `"abc"`},
77 {`abc\b`, `"abc"`},
78 {`abc\B`, `"abc"`},
79 {`ab\bc`, `"abc"`},
80 {`ab\Bc`, `"abc"`},
83 func TestQuery(t *testing.T) {
84 for _, tt := range queryTests {
85 re, err := syntax.Parse(tt.re, syntax.Perl)
86 if err != nil {
87 t.Fatal(err)
89 q := RegexpQuery(re).String()
90 if q != tt.q {
91 t.Errorf("RegexpQuery(%#q) = %#q, want %#q", tt.re, q, tt.q)