git-bug/query/lexer_test.go

53 lines
1.1 KiB
Go
Raw Normal View History

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"},
},
},
2020-03-28 21:22:27 +03:00
// quotes
{`key:"value value"`, []token{{"key", "value value"}}},
{`key:'value value'`, []token{{"key", "value value"}}},
// unmatched quotes
{`key:'value value`, nil},
{`key:value value'`, nil},
}
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)
}
}
}