-
Notifications
You must be signed in to change notification settings - Fork 4
/
lex_test.go
93 lines (81 loc) · 2.33 KB
/
lex_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
package qs
import (
// "fmt"
"reflect"
// "strings"
"testing"
)
/*
func ExampleLex() {
queries := []string{
``,
`wibble`,
`wibble pibble`,
`"a phrase"`,
`tag: citrus`,
`tag:(lemon mango)`,
`-tag:("lemon mango")`,
`tag:(citrus -banana)`,
`date:[2014-01-01 TO 2014-01-02]`,
`1234wibble 12`,
}
for _, q := range queries {
toks := lex(q)
out := []string{}
for _, tok := range toks {
out = append(out, tok.typ.String())
}
fmt.Printf("%s %s\n", q, strings.Join(out, ","))
}
// Output:
//
}
*/
func TestLexer(t *testing.T) {
/*
tokName := func(tok int) string {
// Cheesy and britle - must be a better way...
i := tok - yyPrivate - 1
if i >= 0 && i < len(yyToknames) {
return yyToknames[i]
} else {
return fmt.Sprintf("tok-%d", tok)
}
}
*/
data := []struct {
input string
expected []tokType
}{
{`wibble 123 : < > { } [ ] - + ( ) TO AND OR NOT "this is a phrase"`, []tokType{tLITERAL, tLITERAL, tCOLON, tLESS, tGREATER, tLBRACE, tRBRACE, tLSQUARE, tRSQUARE, tMINUS, tPLUS, tLPAREN, tRPAREN, tTO, tAND, tOR, tNOT, tQUOTED, tEOF}},
{`1.234`, []tokType{tLITERAL, tEOF}},
{` blah `, []tokType{tLITERAL, tEOF}},
{`foo:[ 1 TO 5 ]`, []tokType{tLITERAL, tCOLON, tLSQUARE, tLITERAL, tTO, tLITERAL, tRSQUARE, tEOF}},
{`foo:[1 TO 5]`, []tokType{tLITERAL, tCOLON, tLSQUARE, tLITERAL, tTO, tLITERAL, tRSQUARE, tEOF}},
{`t<est`, []tokType{tLITERAL, tEOF}},
{`t>est`, []tokType{tLITERAL, tEOF}},
{`t=est`, []tokType{tLITERAL, tEOF}},
{`t+est`, []tokType{tLITERAL, tEOF}},
{`TOe`, []tokType{tLITERAL, tEOF}},
{`NOTe`, []tokType{tLITERAL, tEOF}},
{`sAND`, []tokType{tLITERAL, tEOF}},
{`g AND t`, []tokType{tLITERAL, tAND, tLITERAL, tEOF}},
{`shoesize:>10`, []tokType{tLITERAL, tCOLON, tGREATER, tLITERAL, tEOF}},
{`123four`, []tokType{tLITERAL, tEOF}},
{`four321`, []tokType{tLITERAL, tEOF}},
{`123 four`, []tokType{tLITERAL, tLITERAL, tEOF}},
{`wibble~`, []tokType{tLITERAL, tFUZZY, tEOF}},
{`wibble~0.1`, []tokType{tLITERAL, tFUZZY, tEOF}},
{`wibble^5`, []tokType{tLITERAL, tBOOST, tEOF}},
}
for _, dat := range data {
toks := lex(dat.input)
got := make([]tokType, len(toks))
for i, tok := range toks {
got[i] = tok.typ
}
if !reflect.DeepEqual(dat.expected, got) {
t.Errorf("Expected %#v, got %#v: for `%s`", dat.expected, got, dat.input)
}
}
}