2019-05-19 12:26:21 +00:00
|
|
|
#ifdef TESTS_BUILD
|
2019-05-19 12:42:45 +00:00
|
|
|
#include <catch.hpp>
|
2019-06-15 15:20:27 +00:00
|
|
|
#include <codecvt>
|
|
|
|
#include <locale>
|
2019-05-21 15:23:56 +00:00
|
|
|
#include "../../src/Parser/Lexer.hpp"
|
2019-06-17 16:35:12 +00:00
|
|
|
using namespace Porygon::Parser;
|
2019-05-19 12:26:21 +00:00
|
|
|
|
|
|
|
TEST_CASE( "When at end of script return terminator", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.Peek() == '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Peek doesn't advance", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto script = new u16string(u"5 + 5"); // Create as reference to ensure the compiler plays nice with it in release builds
|
2019-06-14 15:20:33 +00:00
|
|
|
Lexer lexer = Lexer(*script, nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.Peek() == '5');
|
|
|
|
REQUIRE(lexer.Peek() == '5');
|
|
|
|
REQUIRE(lexer.Peek() == '5');
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Next does advance", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto script = new u16string(u"5 + 5"); // Create as reference to ensure the compiler plays nice with it in release builds
|
2019-06-14 15:20:33 +00:00
|
|
|
Lexer lexer = Lexer(*script, nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.Next() == '5');
|
|
|
|
REQUIRE(lexer.Next() == ' ');
|
|
|
|
REQUIRE(lexer.Next() == '+');
|
|
|
|
REQUIRE(lexer.Next() == ' ');
|
|
|
|
REQUIRE(lexer.Next() == '5');
|
|
|
|
REQUIRE(lexer.Next() == '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex Null Terminator as EOF", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.LexNext('\0') -> GetKind() == TokenKind::EndOfFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex Plus Token", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.LexNext('+') -> GetKind() == TokenKind::PlusToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex Minus Token", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.LexNext('-') -> GetKind() == TokenKind::MinusToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex Slash Token", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.LexNext('/') -> GetKind() == TokenKind::SlashToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex Star Token", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.LexNext('*') -> GetKind() == TokenKind::StarToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex Assignment Token", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(lexer.LexNext('=') -> GetKind() == TokenKind::AssignmentToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex Equality Token", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"==", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::EqualityToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex Whitespace", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
CHECK(lexer.LexNext(' ') -> GetKind() == TokenKind::WhiteSpace);
|
|
|
|
CHECK(lexer.LexNext('\t') -> GetKind() == TokenKind::WhiteSpace);
|
|
|
|
CHECK(lexer.LexNext('\n') -> GetKind() == TokenKind::WhiteSpace);
|
|
|
|
CHECK(lexer.LexNext('\r') -> GetKind() == TokenKind::WhiteSpace);
|
2019-05-19 13:28:45 +00:00
|
|
|
CHECK(lexer.LexNext('\v') -> GetKind() == TokenKind::WhiteSpace);
|
|
|
|
CHECK(lexer.LexNext('\f') -> GetKind() == TokenKind::WhiteSpace);
|
2019-05-19 12:26:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 14:11:16 +00:00
|
|
|
TEST_CASE( "Lex Basic Digits", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
CHECK(lexer.LexNext('0') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('1') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('2') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('3') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('4') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('5') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('6') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('7') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('8') -> GetKind() == TokenKind::Integer);
|
|
|
|
CHECK(lexer.LexNext('9') -> GetKind() == TokenKind::Integer);
|
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
std::u16string to_u16string(long const &i) {
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::little_endian>, char16_t> conv;
|
|
|
|
return conv.from_bytes(std::to_string(i));
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:26:21 +00:00
|
|
|
TEST_CASE( "Lex Longer Integers", "[lexer]" ) {
|
|
|
|
long integers[] {0,1,5,9,10,50,100,1000,99999,6484,62163,48862};
|
|
|
|
for (long integer : integers){
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = to_u16string(integer);
|
|
|
|
Lexer lexer = Lexer(s, nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::Integer);
|
|
|
|
auto* integerToken = (IntegerToken *)firstToken;
|
2019-06-13 16:49:38 +00:00
|
|
|
CHECK(integerToken -> GetValue() == integer);
|
2019-05-19 12:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
std::u16string to_u16string(double const &i) {
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::little_endian>, char16_t> conv;
|
|
|
|
return conv.from_bytes(std::to_string(i));}
|
|
|
|
|
2019-05-19 12:26:21 +00:00
|
|
|
TEST_CASE( "Lex Floats", "[lexer]" ) {
|
|
|
|
double floats[] {0.5, 0.8, 100.7, 52.3548, 8461354.1324886};
|
|
|
|
for (double f : floats){
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = to_u16string(f);
|
|
|
|
Lexer lexer = Lexer(s, nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::Float);
|
|
|
|
auto* floatToken = (FloatToken *)firstToken;
|
2019-06-13 16:49:38 +00:00
|
|
|
CHECK(floatToken -> GetValue() == Approx(f));
|
2019-05-19 12:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex And Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"and", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::AndKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex Break Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"break", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::BreakKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex Do Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"do", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::DoKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex else Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"else", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::ElseKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex else if Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"elseif", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::ElseIfKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex end Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"end", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::EndKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex false Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"false", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::FalseKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex for Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"for", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::ForKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex function Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = new u16string(u"function");
|
|
|
|
Lexer lexer = Lexer(*s, nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::FunctionKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex if Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"if", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::IfKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex in Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"in", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::InKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex local Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"local", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::LocalKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex nil Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"nil", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::NilKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex not Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"not", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::NotKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex or Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"or", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::OrKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex return Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"return", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::ReturnKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex then Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"then", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::ThenKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex true Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"true", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::TrueKeyword);
|
|
|
|
}
|
|
|
|
TEST_CASE( "Lex while Keyword", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"while", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::WhileKeyword);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex identifier", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
Lexer lexer = Lexer(u"foo", nullptr);
|
2019-05-19 12:26:21 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-19 12:26:21 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::Identifier);
|
2019-06-13 16:49:38 +00:00
|
|
|
REQUIRE(((IdentifierToken*)firstToken) -> GetValue() == HashedString("foo"));
|
2019-05-19 12:26:21 +00:00
|
|
|
}
|
2019-05-19 14:11:16 +00:00
|
|
|
|
|
|
|
TEST_CASE( "Lex Start Position", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = new u16string(u"+ - bar 1234");
|
|
|
|
Lexer lexer = Lexer(*s, nullptr);
|
2019-05-19 14:11:16 +00:00
|
|
|
auto tokens = lexer.Lex();
|
2019-05-21 13:11:00 +00:00
|
|
|
REQUIRE(tokens.size() == 5);
|
2019-05-19 14:11:16 +00:00
|
|
|
CHECK(((IdentifierToken*)tokens[0]) -> GetStartPosition() == 0);
|
2019-05-21 13:11:00 +00:00
|
|
|
CHECK(((IdentifierToken*)tokens[1]) -> GetStartPosition() == 2);
|
|
|
|
CHECK(((IdentifierToken*)tokens[2]) -> GetStartPosition() == 4);
|
|
|
|
CHECK(((IdentifierToken*)tokens[3]) -> GetStartPosition() == 8);
|
|
|
|
CHECK(((IdentifierToken*)tokens[4]) -> GetStartPosition() == 12);
|
2019-05-19 14:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Lex End Position", "[lexer]" ) {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = new u16string(u"+ - bar 1234");
|
|
|
|
Lexer lexer = Lexer(*s, nullptr);
|
2019-05-19 14:11:16 +00:00
|
|
|
auto tokens = lexer.Lex();
|
2019-05-21 13:11:00 +00:00
|
|
|
REQUIRE(tokens.size() == 5);
|
2019-05-19 14:11:16 +00:00
|
|
|
CHECK(((IdentifierToken*)tokens[0]) -> GetEndPosition() == 0);
|
2019-05-21 13:11:00 +00:00
|
|
|
CHECK(((IdentifierToken*)tokens[1]) -> GetEndPosition() == 2);
|
|
|
|
CHECK(((IdentifierToken*)tokens[2]) -> GetEndPosition() == 6);
|
|
|
|
CHECK(((IdentifierToken*)tokens[3]) -> GetEndPosition() == 11);
|
|
|
|
CHECK(((IdentifierToken*)tokens[4]) -> GetEndPosition() == 12);
|
2019-05-19 14:11:16 +00:00
|
|
|
}
|
2019-05-22 11:24:28 +00:00
|
|
|
|
|
|
|
TEST_CASE("Lex Double Quote String", "[lexer]") {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = new u16string(u"\"foo bar\"");
|
|
|
|
Lexer lexer = Lexer(*s, nullptr);
|
2019-05-22 11:24:28 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-22 11:24:28 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::String);
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE(((StringToken*)firstToken) -> GetValue() == u"foo bar");
|
2019-05-22 11:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Lex Single Quote String", "[lexer]") {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = new u16string(u"'foo bar'");
|
|
|
|
Lexer lexer = Lexer(*s, nullptr);
|
2019-05-22 11:24:28 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-22 11:24:28 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::String);
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE(((StringToken*)firstToken) -> GetValue() == u"foo bar");
|
2019-05-22 11:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Lex Double Quote String, Escape Quote", "[lexer]") {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = new u16string(u"'foo\\\"bar'");
|
|
|
|
Lexer lexer = Lexer(*s, nullptr);
|
2019-05-22 11:24:28 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-22 11:24:28 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::String);
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE(((StringToken*)firstToken) -> GetValue() == u"foo\"bar");
|
2019-05-22 11:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Lex String with newline", "[lexer]") {
|
2019-06-15 15:20:27 +00:00
|
|
|
auto s = new u16string(u"'foo\\nbar'");
|
|
|
|
Lexer lexer = Lexer(*s, nullptr);
|
2019-05-22 11:24:28 +00:00
|
|
|
auto tokens = lexer.Lex();
|
|
|
|
REQUIRE(tokens.size() == 2);
|
2019-06-13 16:49:38 +00:00
|
|
|
const IToken* firstToken = tokens[0];
|
2019-05-22 11:24:28 +00:00
|
|
|
REQUIRE(firstToken -> GetKind() == TokenKind::String);
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE(((StringToken*)firstToken) -> GetValue() == u"foo\nbar");
|
2019-05-22 11:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-19 12:26:21 +00:00
|
|
|
#endif
|