diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fda652d92696c9bca0ff39db5766b2a5fd63b0d1..77fccf2b2cdd20bee0076bdcb31a727a736e4877 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PUGS_BINARY_DIR}) add_executable (unit_tests test_main.cpp test_ASTSymbolTableBuilder.cpp + test_ASTSymbolInitializationChecker.cpp test_ASTBuilder.cpp test_Array.cpp test_ArrayUtils.cpp diff --git a/tests/test_ASTSymbolInitializationChecker.cpp b/tests/test_ASTSymbolInitializationChecker.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a9caed3f32aeed5f7816ea02955f7bf9b73656a --- /dev/null +++ b/tests/test_ASTSymbolInitializationChecker.cpp @@ -0,0 +1,79 @@ +#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); + } +}