Skip to content
Snippets Groups Projects
Commit c4a78a8d authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add ASTSymbolInitializationChecker tests

parent 100859c4
No related branches found
No related tags found
1 merge request!37Feature/language
...@@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PUGS_BINARY_DIR}) ...@@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PUGS_BINARY_DIR})
add_executable (unit_tests add_executable (unit_tests
test_main.cpp test_main.cpp
test_ASTSymbolTableBuilder.cpp test_ASTSymbolTableBuilder.cpp
test_ASTSymbolInitializationChecker.cpp
test_ASTBuilder.cpp test_ASTBuilder.cpp
test_Array.cpp test_Array.cpp
test_ArrayUtils.cpp test_ArrayUtils.cpp
......
#include <catch2/catch.hpp>
#include <ASTBuilder.hpp>
#include <ASTSymbolTableBuilder.hpp>
#include <ASTSymbolInitializationChecker.hpp>
TEST_CASE("ASTSymbolInitializationChecker", "[language]")
{
SECTION("Declarative initialization")
{
std::string_view data = R"(
N m = 2;
N n = m;
N p;
)";
string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input);
ASTSymbolTableBuilder{*ast};
ASTSymbolInitializationChecker{*ast};
auto [attribute_m, found_m] = ast->m_symbol_table->find("m");
REQUIRE(found_m);
REQUIRE(attribute_m->second.isInitialized());
auto [attribute_n, found_n] = ast->m_symbol_table->find("n");
REQUIRE(found_n);
REQUIRE(attribute_n->second.isInitialized());
auto [attribute_p, found_p] = ast->m_symbol_table->find("p");
REQUIRE(found_p);
REQUIRE(not attribute_p->second.isInitialized());
}
SECTION("Declaration plus affectation")
{
std::string_view data = R"(
Z z;
N m;
N n;
n = 2;
m = n;
)";
string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input);
ASTSymbolTableBuilder{*ast};
ASTSymbolInitializationChecker{*ast};
auto [attribute_m, found_m] = ast->m_symbol_table->find("m");
REQUIRE(found_m);
REQUIRE(attribute_m->second.isInitialized());
auto [attribute_n, found_n] = ast->m_symbol_table->find("n");
REQUIRE(found_n);
REQUIRE(attribute_n->second.isInitialized());
auto [attribute_z, found_z] = ast->m_symbol_table->find("z");
REQUIRE(found_z);
REQUIRE(not attribute_z->second.isInitialized());
}
SECTION("used uninitialized")
{
std::string_view data = R"(
N n;
N m = n;
)";
string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input);
ASTSymbolTableBuilder{*ast};
REQUIRE_THROWS_AS(ASTSymbolInitializationChecker{*ast}, parse_error);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment