From c4a78a8d73330b8478436f64a00546778c4ec6ee Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Mon, 8 Jul 2019 18:33:33 +0200
Subject: [PATCH] Add ASTSymbolInitializationChecker tests

---
 tests/CMakeLists.txt                          |  1 +
 tests/test_ASTSymbolInitializationChecker.cpp | 79 +++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 tests/test_ASTSymbolInitializationChecker.cpp

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index fda652d92..77fccf2b2 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 000000000..8a9caed3f
--- /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);
+  }
+}
-- 
GitLab