Skip to content

Commit

Permalink
Start work on testing expression parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 6, 2024
1 parent c94f4fe commit b15ce4b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/Parser/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,33 @@ TEST(ParserTest, ParseStruct) {
}
}

TEST(ParserTest, Expressions) {
std::string structDecl =
R"(
float a = 3.14;
float b = 1.0 + 2.0 * 3.0 / a;
)";

auto unit = parse(structDecl);

EXPECT_TRUE(unit);
auto& externalDecl = unit->getExternalDeclarations();
EXPECT_EQ(externalDecl.size(), 2);
auto parsedA = dynamic_cast<VariableDeclaration*>(externalDecl.at(0).get());
auto parsedB = dynamic_cast<VariableDeclaration*>(externalDecl.at(1).get());

// Check var names
EXPECT_EQ(parsedA->getIdentifierName(), "a");
EXPECT_EQ(parsedB->getIdentifierName(), "b");

// Check types
EXPECT_EQ(parsedA->getType()->toString(), "float");
EXPECT_EQ(parsedB->getType()->toString(), "float");

EXPECT_EQ(dynamic_cast<FloatConstantExpression*>(parsedA->getInitialzerExpression())->getVal(), 3.14f);
EXPECT_TRUE(dynamic_cast<BinaryExpression*>(parsedB->getInitialzerExpression()));
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down

0 comments on commit b15ce4b

Please sign in to comment.