Implemented parser-context tests

This commit is contained in:
Pasha Bibko
2025-08-21 20:27:02 +01:00
parent a18e652915
commit 18f5a3954d
2 changed files with 114 additions and 10 deletions

View File

@@ -1,6 +0,0 @@
{
"files.associations":
{
"type_traits": "cpp"
}
}

View File

@@ -8,21 +8,131 @@ namespace PashaBibko::LXC::Parser
{
TEST(ParserTests, ParserContextAtAndInBounds)
{
EXPECT_TRUE(true);
{
std::string src = "576 hello return { null }";
Util::ReturnVal tokens = Lexer::TokenizeFile(src);
ASSERT_FALSE(tokens.Failed());
ParserContext ctx(tokens.Result());
ASSERT_FALSE(ctx.At() == nullptr);
EXPECT_TRUE(ctx.At()->type == Lexer::Token::NumLiteral);
EXPECT_STREQ(ctx.At()->Str(), "576");
EXPECT_TRUE(ctx.InBounds());
}
{
std::string src = " ";
Util::ReturnVal tokens = Lexer::TokenizeFile(src);
ASSERT_FALSE(tokens.Failed());
ParserContext ctx(tokens.Result());
EXPECT_TRUE(ctx.At() == nullptr);
EXPECT_FALSE(ctx.InBounds());
}
}
TEST(ParserTests, ParserContextPeek)
{
EXPECT_TRUE(true);
{
std::string src = "int hello";
Util::ReturnVal tokens = Lexer::TokenizeFile(src);
ASSERT_FALSE(tokens.Failed());
ParserContext ctx(tokens.Result());
ASSERT_FALSE(ctx.Peek() == nullptr);
EXPECT_TRUE(ctx.Peek()->type == Lexer::Token::Identifier);
EXPECT_STREQ(ctx.Peek()->Str(), "hello");
}
{
std::string src = "null";
Util::ReturnVal tokens = Lexer::TokenizeFile(src);
ASSERT_FALSE(tokens.Failed());
}
}
TEST(ParserTests, ParserContextAdvance)
{
EXPECT_TRUE(true);
std::string src = "func<int> main() { return 23 }";
Util::ReturnVal tokens = Lexer::TokenizeFile(src);
ASSERT_FALSE(tokens.Failed());
ParserContext ctx(tokens.Result());
for (size_t idx = 0; idx < 10; idx++)
{
const Lexer::Token* current = ctx.Advance();
ASSERT_FALSE(current == nullptr);
const Lexer::Token::TokenType cType = current->type;
switch (idx)
{
case 0:
EXPECT_TRUE(cType == Lexer::Token::OpenCrocodile);
break;
case 1:
EXPECT_TRUE(cType == Lexer::Token::Identifier);
break;
case 2:
EXPECT_TRUE(cType == Lexer::Token::CloseCrocodile);
break;
case 3:
EXPECT_TRUE(cType == Lexer::Token::Identifier);
break;
case 4:
EXPECT_TRUE(cType == Lexer::Token::OpenParen);
break;
case 5:
EXPECT_TRUE(cType == Lexer::Token::CloseParen);
break;
case 6:
EXPECT_TRUE(cType == Lexer::Token::OpenBrace);
break;
case 7:
EXPECT_TRUE(cType == Lexer::Token::Return);
break;
case 8:
EXPECT_TRUE(cType == Lexer::Token::NumLiteral);
break;
case 9:
EXPECT_TRUE(cType == Lexer::Token::CloseBrace);
break;
default:
ASSERT_FALSE(false); // There were two many tokens generated by the lexer
}
}
}
TEST(ParserTests, ParserContextExpect)
{
EXPECT_TRUE(true);
std::string src = "func<int> main() { return 42 }";
Util::ReturnVal tokens = Lexer::TokenizeFile(src);
ASSERT_FALSE(tokens.Failed());
ParserContext ctx(tokens.Result());
ASSERT_TRUE(ctx.Expect(std::array
{
Lexer::Token::FunctionDef,
Lexer::Token::OpenCrocodile,
Lexer::Token::Identifier,
Lexer::Token::CloseCrocodile,
Lexer::Token::Identifier,
Lexer::Token::OpenParen,
Lexer::Token::CloseParen,
Lexer::Token::OpenBrace,
Lexer::Token::Return,
Lexer::Token::NumLiteral,
Lexer::Token::CloseBrace
}));
}
}