committed by
Thomas Pelletier
parent
0049ab3dc4
commit
0599275eb9
+48
-48
@@ -36,143 +36,143 @@ func testQLFlow(t *testing.T, input string, expectedFlow []token) {
|
||||
|
||||
func TestLexSpecialChars(t *testing.T) {
|
||||
testQLFlow(t, " .$[]..()?*", []token{
|
||||
token{Position{1, 2}, tokenDot, "."},
|
||||
token{Position{1, 3}, tokenDollar, "$"},
|
||||
token{Position{1, 4}, tokenLeftBracket, "["},
|
||||
token{Position{1, 5}, tokenRightBracket, "]"},
|
||||
token{Position{1, 6}, tokenDotDot, ".."},
|
||||
token{Position{1, 8}, tokenLeftParen, "("},
|
||||
token{Position{1, 9}, tokenRightParen, ")"},
|
||||
token{Position{1, 10}, tokenQuestion, "?"},
|
||||
token{Position{1, 11}, tokenStar, "*"},
|
||||
token{Position{1, 12}, tokenEOF, ""},
|
||||
{Position{1, 2}, tokenDot, "."},
|
||||
{Position{1, 3}, tokenDollar, "$"},
|
||||
{Position{1, 4}, tokenLeftBracket, "["},
|
||||
{Position{1, 5}, tokenRightBracket, "]"},
|
||||
{Position{1, 6}, tokenDotDot, ".."},
|
||||
{Position{1, 8}, tokenLeftParen, "("},
|
||||
{Position{1, 9}, tokenRightParen, ")"},
|
||||
{Position{1, 10}, tokenQuestion, "?"},
|
||||
{Position{1, 11}, tokenStar, "*"},
|
||||
{Position{1, 12}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexString(t *testing.T) {
|
||||
testQLFlow(t, "'foo\n'", []token{
|
||||
token{Position{1, 2}, tokenString, "foo\n"},
|
||||
token{Position{2, 2}, tokenEOF, ""},
|
||||
{Position{1, 2}, tokenString, "foo\n"},
|
||||
{Position{2, 2}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexDoubleString(t *testing.T) {
|
||||
testQLFlow(t, `"bar"`, []token{
|
||||
token{Position{1, 2}, tokenString, "bar"},
|
||||
token{Position{1, 6}, tokenEOF, ""},
|
||||
{Position{1, 2}, tokenString, "bar"},
|
||||
{Position{1, 6}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexStringEscapes(t *testing.T) {
|
||||
testQLFlow(t, `"foo \" \' \b \f \/ \t \r \\ \u03A9 \U00012345 \n bar"`, []token{
|
||||
token{Position{1, 2}, tokenString, "foo \" ' \b \f / \t \r \\ \u03A9 \U00012345 \n bar"},
|
||||
token{Position{1, 55}, tokenEOF, ""},
|
||||
{Position{1, 2}, tokenString, "foo \" ' \b \f / \t \r \\ \u03A9 \U00012345 \n bar"},
|
||||
{Position{1, 55}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexStringUnfinishedUnicode4(t *testing.T) {
|
||||
testQLFlow(t, `"\u000"`, []token{
|
||||
token{Position{1, 2}, tokenError, "unfinished unicode escape"},
|
||||
{Position{1, 2}, tokenError, "unfinished unicode escape"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexStringUnfinishedUnicode8(t *testing.T) {
|
||||
testQLFlow(t, `"\U0000"`, []token{
|
||||
token{Position{1, 2}, tokenError, "unfinished unicode escape"},
|
||||
{Position{1, 2}, tokenError, "unfinished unicode escape"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexStringInvalidEscape(t *testing.T) {
|
||||
testQLFlow(t, `"\x"`, []token{
|
||||
token{Position{1, 2}, tokenError, "invalid escape sequence: \\x"},
|
||||
{Position{1, 2}, tokenError, "invalid escape sequence: \\x"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexStringUnfinished(t *testing.T) {
|
||||
testQLFlow(t, `"bar`, []token{
|
||||
token{Position{1, 2}, tokenError, "unclosed string"},
|
||||
{Position{1, 2}, tokenError, "unclosed string"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexKey(t *testing.T) {
|
||||
testQLFlow(t, "foo", []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 4}, tokenEOF, ""},
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 4}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexRecurse(t *testing.T) {
|
||||
testQLFlow(t, "$..*", []token{
|
||||
token{Position{1, 1}, tokenDollar, "$"},
|
||||
token{Position{1, 2}, tokenDotDot, ".."},
|
||||
token{Position{1, 4}, tokenStar, "*"},
|
||||
token{Position{1, 5}, tokenEOF, ""},
|
||||
{Position{1, 1}, tokenDollar, "$"},
|
||||
{Position{1, 2}, tokenDotDot, ".."},
|
||||
{Position{1, 4}, tokenStar, "*"},
|
||||
{Position{1, 5}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexBracketKey(t *testing.T) {
|
||||
testQLFlow(t, "$[foo]", []token{
|
||||
token{Position{1, 1}, tokenDollar, "$"},
|
||||
token{Position{1, 2}, tokenLeftBracket, "["},
|
||||
token{Position{1, 3}, tokenKey, "foo"},
|
||||
token{Position{1, 6}, tokenRightBracket, "]"},
|
||||
token{Position{1, 7}, tokenEOF, ""},
|
||||
{Position{1, 1}, tokenDollar, "$"},
|
||||
{Position{1, 2}, tokenLeftBracket, "["},
|
||||
{Position{1, 3}, tokenKey, "foo"},
|
||||
{Position{1, 6}, tokenRightBracket, "]"},
|
||||
{Position{1, 7}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexSpace(t *testing.T) {
|
||||
testQLFlow(t, "foo bar baz", []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenKey, "bar"},
|
||||
token{Position{1, 9}, tokenKey, "baz"},
|
||||
token{Position{1, 12}, tokenEOF, ""},
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenKey, "bar"},
|
||||
{Position{1, 9}, tokenKey, "baz"},
|
||||
{Position{1, 12}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexInteger(t *testing.T) {
|
||||
testQLFlow(t, "100 +200 -300", []token{
|
||||
token{Position{1, 1}, tokenInteger, "100"},
|
||||
token{Position{1, 5}, tokenInteger, "+200"},
|
||||
token{Position{1, 10}, tokenInteger, "-300"},
|
||||
token{Position{1, 14}, tokenEOF, ""},
|
||||
{Position{1, 1}, tokenInteger, "100"},
|
||||
{Position{1, 5}, tokenInteger, "+200"},
|
||||
{Position{1, 10}, tokenInteger, "-300"},
|
||||
{Position{1, 14}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexFloat(t *testing.T) {
|
||||
testQLFlow(t, "100.0 +200.0 -300.0", []token{
|
||||
token{Position{1, 1}, tokenFloat, "100.0"},
|
||||
token{Position{1, 7}, tokenFloat, "+200.0"},
|
||||
token{Position{1, 14}, tokenFloat, "-300.0"},
|
||||
token{Position{1, 20}, tokenEOF, ""},
|
||||
{Position{1, 1}, tokenFloat, "100.0"},
|
||||
{Position{1, 7}, tokenFloat, "+200.0"},
|
||||
{Position{1, 14}, tokenFloat, "-300.0"},
|
||||
{Position{1, 20}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexFloatWithMultipleDots(t *testing.T) {
|
||||
testQLFlow(t, "4.2.", []token{
|
||||
token{Position{1, 1}, tokenError, "cannot have two dots in one float"},
|
||||
{Position{1, 1}, tokenError, "cannot have two dots in one float"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexFloatLeadingDot(t *testing.T) {
|
||||
testQLFlow(t, "+.1", []token{
|
||||
token{Position{1, 1}, tokenError, "cannot start float with a dot"},
|
||||
{Position{1, 1}, tokenError, "cannot start float with a dot"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexFloatWithTrailingDot(t *testing.T) {
|
||||
testQLFlow(t, "42.", []token{
|
||||
token{Position{1, 1}, tokenError, "float cannot end with a dot"},
|
||||
{Position{1, 1}, tokenError, "float cannot end with a dot"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexNumberWithoutDigit(t *testing.T) {
|
||||
testQLFlow(t, "+", []token{
|
||||
token{Position{1, 1}, tokenError, "no digit in that number"},
|
||||
{Position{1, 1}, tokenError, "no digit in that number"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexUnknown(t *testing.T) {
|
||||
testQLFlow(t, "^", []token{
|
||||
token{Position{1, 1}, tokenError, "unexpected char: '94'"},
|
||||
{Position{1, 1}, tokenError, "unexpected char: '94'"},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user