lexer_test.go

 1package query
 2
 3import (
 4	"testing"
 5
 6	"github.com/stretchr/testify/assert"
 7)
 8
 9func TestTokenize(t *testing.T) {
10	var tests = []struct {
11		input  string
12		tokens []token
13	}{
14		{"gibberish", nil},
15		{"status:", nil},
16		{":value", nil},
17
18		{"status:open", []token{{"status", "open"}}},
19		{"status:closed", []token{{"status", "closed"}}},
20
21		{"author:rene", []token{{"author", "rene"}}},
22		{`author:"René Descartes"`, []token{{"author", "René Descartes"}}},
23
24		{
25			`status:open status:closed author:rene author:"René Descartes"`,
26			[]token{
27				{"status", "open"},
28				{"status", "closed"},
29				{"author", "rene"},
30				{"author", "René Descartes"},
31			},
32		},
33	}
34
35	for _, tc := range tests {
36		tokens, err := tokenize(tc.input)
37		if tc.tokens == nil {
38			assert.Error(t, err)
39			assert.Nil(t, tokens)
40		} else {
41			assert.NoError(t, err)
42			assert.Equal(t, tc.tokens, tokens)
43		}
44	}
45}