aboutsummaryrefslogtreecommitdiffstats
path: root/query/lexer_test.go
blob: 922e3fc937b389d228252db09e7052946c9ad2f6 (plain) (blame)
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
package query

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestTokenize(t *testing.T) {
	var tests = []struct {
		input  string
		tokens []token
	}{
		{"gibberish", nil},
		{"status:", nil},
		{":value", nil},

		{"status:open", []token{{"status", "open"}}},
		{"status:closed", []token{{"status", "closed"}}},

		{"author:rene", []token{{"author", "rene"}}},
		{`author:"René Descartes"`, []token{{"author", "René Descartes"}}},

		{
			`status:open status:closed author:rene author:"René Descartes"`,
			[]token{
				{"status", "open"},
				{"status", "closed"},
				{"author", "rene"},
				{"author", "René Descartes"},
			},
		},
	}

	for _, tc := range tests {
		tokens, err := tokenize(tc.input)
		if tc.tokens == nil {
			assert.Error(t, err)
			assert.Nil(t, tokens)
		} else {
			assert.NoError(t, err)
			assert.Equal(t, tc.tokens, tokens)
		}
	}
}