2017-12-09 10:45:13 +01:00
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
#include <gmock/gmock-matchers.h>
|
|
|
|
|
|
#include "SqlLexer.h"
|
2018-09-09 18:52:32 +02:00
|
|
|
|
#include "PrintTo_Qt.h"
|
2017-12-09 10:45:13 +01:00
|
|
|
|
|
|
|
|
|
|
using namespace testing;
|
|
|
|
|
|
|
2019-01-28 20:52:39 +01:00
|
|
|
|
TEST(SqlLexer, emptyInput)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString input;
|
|
|
|
|
|
SqlLexer lexer(input, LexerState::Null);
|
|
|
|
|
|
|
|
|
|
|
|
int startpos, length;
|
|
|
|
|
|
BasicTokenType tokentype;
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(0));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::End));
|
|
|
|
|
|
}
|
2017-12-09 10:45:13 +01:00
|
|
|
|
|
2023-01-07 07:41:58 +01:00
|
|
|
|
TEST(SqlLexer, emptyDollarQuote)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString input = "$$";
|
|
|
|
|
|
SqlLexer lexer(input, LexerState::Null);
|
|
|
|
|
|
|
|
|
|
|
|
int startpos = -1, length = -1;
|
|
|
|
|
|
BasicTokenType tokentype = BasicTokenType::None;
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(0));
|
|
|
|
|
|
ASSERT_THAT("$$", Eq(out));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::DollarQuote));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SqlLexer, filledDollarQuote)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString input = "$body$";
|
|
|
|
|
|
SqlLexer lexer(input, LexerState::Null);
|
|
|
|
|
|
|
|
|
|
|
|
int startpos = -1, length = -1;
|
|
|
|
|
|
BasicTokenType tokentype = BasicTokenType::None;
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(0));
|
|
|
|
|
|
ASSERT_THAT("$body$", Eq(out));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::DollarQuote));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-09 10:45:13 +01:00
|
|
|
|
TEST(SqlLexer, lexer)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString input = " SELECT ";
|
|
|
|
|
|
SqlLexer lexer(input, LexerState::Null);
|
|
|
|
|
|
|
|
|
|
|
|
int startpos, length;
|
|
|
|
|
|
BasicTokenType tokentype;
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(1));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(6));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::Symbol));
|
|
|
|
|
|
ASSERT_THAT( out, Eq(QString("SELECT")) );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-19 13:52:23 +02:00
|
|
|
|
TEST(SqlLexer, lexerWithWhiteSpace)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString input = " SELECT ";
|
|
|
|
|
|
SqlLexer lexer(input, LexerState::Null, true);
|
|
|
|
|
|
|
|
|
|
|
|
int startpos, length;
|
|
|
|
|
|
BasicTokenType tokentype;
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(0));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(1));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::WhiteSpace));
|
|
|
|
|
|
ASSERT_THAT(out, Eq(QString(" ")) );
|
|
|
|
|
|
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(1));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(6));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::Symbol));
|
|
|
|
|
|
ASSERT_THAT(out, Eq(QString("SELECT")) );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-09 10:45:13 +01:00
|
|
|
|
TEST(SqlLexer, lexer_quote_in_string)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString input = " 'abc''def' ";
|
|
|
|
|
|
SqlLexer lexer(input, LexerState::Null);
|
|
|
|
|
|
|
|
|
|
|
|
int startpos, length;
|
|
|
|
|
|
BasicTokenType tokentype;
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(1));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(10));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::QuotedString));
|
2019-08-19 13:52:23 +02:00
|
|
|
|
ASSERT_THAT(out, Eq(QString("'abc''def'")) );
|
2017-12-09 10:45:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-05 22:23:28 +01:00
|
|
|
|
TEST(SqlLexer, lexer_comma_handling)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString input = "abc,def";
|
|
|
|
|
|
SqlLexer lexer(input, LexerState::Null);
|
|
|
|
|
|
|
|
|
|
|
|
int startpos, length;
|
|
|
|
|
|
BasicTokenType tokentype;
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(0));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(3));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::Symbol));
|
|
|
|
|
|
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(3));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(1));
|
2018-09-09 16:20:30 +02:00
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::Comma));
|
2018-02-05 22:23:28 +01:00
|
|
|
|
ASSERT_THAT(out, Eq(QString(",")));
|
|
|
|
|
|
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(4));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(3));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::Symbol));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-06 21:18:28 +01:00
|
|
|
|
TEST(SqlLexer, lexer_cast)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString input = "'1'::integer";
|
|
|
|
|
|
SqlLexer lexer(input, LexerState::Null);
|
|
|
|
|
|
|
|
|
|
|
|
int startpos, length;
|
|
|
|
|
|
BasicTokenType tokentype;
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(0));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(3));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::QuotedString));
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(3));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(2));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::Cast));
|
|
|
|
|
|
lexer.nextBasicToken(startpos, length, tokentype, out);
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(startpos, Eq(5));
|
|
|
|
|
|
ASSERT_THAT(length, Eq(7));
|
|
|
|
|
|
ASSERT_THAT(tokentype, Eq(BasicTokenType::Symbol));
|
|
|
|
|
|
}
|