From b35ad80894fe27197dd5f0a032d9b2565050366c Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Mon, 11 Nov 2024 01:34:38 +0100
Subject: [PATCH] Add tests for ASTCheckpointsInfo

---
 src/language/utils/ASTCheckpointsInfo.cpp |  4 +-
 tests/CMakeLists.txt                      |  1 +
 tests/test_ASTCheckpointsInfo.cpp         | 83 +++++++++++++++++++++++
 3 files changed, 86 insertions(+), 2 deletions(-)
 create mode 100644 tests/test_ASTCheckpointsInfo.cpp

diff --git a/src/language/utils/ASTCheckpointsInfo.cpp b/src/language/utils/ASTCheckpointsInfo.cpp
index 779290bba..fa1898e88 100644
--- a/src/language/utils/ASTCheckpointsInfo.cpp
+++ b/src/language/utils/ASTCheckpointsInfo.cpp
@@ -29,7 +29,7 @@ ASTCheckpointsInfo::_findASTCheckpoint(std::vector<size_t>& location, const ASTN
 
 ASTCheckpointsInfo::ASTCheckpointsInfo(const ASTNode& root_node)
 {
-  Assert(m_checkpoints_info_instance == nullptr, "Can only define one ASTCheckpointInfo");
+  Assert(m_checkpoints_info_instance == nullptr, "Can only define one ASTCheckpointsInfo");
   m_checkpoints_info_instance = this;
 
   Assert(root_node.is_root());
@@ -43,7 +43,7 @@ ASTCheckpointsInfo::ASTCheckpointsInfo(const ASTNode& root_node)
 const ASTCheckpointsInfo&
 ASTCheckpointsInfo::getInstance()
 {
-  Assert(m_checkpoints_info_instance != nullptr, "ASTCheckpointInfo is not defined!");
+  Assert(m_checkpoints_info_instance != nullptr, "ASTCheckpointsInfo is not defined!");
   return *m_checkpoints_info_instance;
 }
 
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 8fd49bde4..871bfbe45 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -19,6 +19,7 @@ add_executable (unit_tests
   test_ArraySubscriptProcessor.cpp
   test_ASTBuilder.cpp
   test_ASTCheckpoint.cpp
+  test_ASTCheckpointsInfo.cpp
   test_ASTDotPrinter.cpp
   test_ASTExecutionStack.cpp
   test_ASTModulesImporter.cpp
diff --git a/tests/test_ASTCheckpointsInfo.cpp b/tests/test_ASTCheckpointsInfo.cpp
new file mode 100644
index 000000000..61eb5c00f
--- /dev/null
+++ b/tests/test_ASTCheckpointsInfo.cpp
@@ -0,0 +1,83 @@
+#include <catch2/catch_approx.hpp>
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/matchers/catch_matchers_predicate.hpp>
+
+#include <utils/pugs_config.hpp>
+
+#include <dev/ParallelChecker.hpp>
+#include <language/ast/ASTBuilder.hpp>
+#include <language/ast/ASTExecutionStack.hpp>
+#include <language/ast/ASTModulesImporter.hpp>
+#include <language/ast/ASTNodeDataTypeBuilder.hpp>
+#include <language/ast/ASTNodeDeclarationToAffectationConverter.hpp>
+#include <language/ast/ASTNodeExpressionBuilder.hpp>
+#include <language/ast/ASTNodeTypeCleaner.hpp>
+#include <language/ast/ASTSymbolTableBuilder.hpp>
+#include <language/modules/MathModule.hpp>
+#include <language/utils/ASTCheckpointsInfo.hpp>
+#include <language/utils/CheckpointResumeRepository.hpp>
+#include <utils/ExecutionStatManager.hpp>
+
+class ASTCheckpointsInfoTester
+{
+ private:
+  ASTCheckpointsInfo m_ast_checkpoint_info;
+
+ public:
+  ASTCheckpointsInfoTester(const ASTNode& root_node) : m_ast_checkpoint_info(root_node) {}
+  ~ASTCheckpointsInfoTester() = default;
+};
+
+// clazy:excludeall=non-pod-global-static
+
+TEST_CASE("ASTCheckpointsInfo", "[utils/checkpointing]")
+{
+#ifndef NDEBUG
+  REQUIRE_THROWS_WITH(ASTCheckpointsInfo::getInstance(), "ASTCheckpointsInfo is not defined!");
+#endif   // NDEBUG
+
+  std::string data = R"(
+for(let i:N, i=0; i<7; ++i) {
+  checkpoint();
+
+  if (i == 2) {
+    checkpoint();
+  }
+
+  if (i == 5) {
+    checkpoint_and_exit();
+  }
+}
+)";
+
+  ExecutionStatManager::create();
+
+  TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"};
+  auto ast = ASTBuilder::build(input);
+
+  ASTModulesImporter{*ast};
+  ASTNodeTypeCleaner<language::import_instruction>{*ast};
+
+  ASTSymbolTableBuilder{*ast};
+  ASTNodeDataTypeBuilder{*ast};
+
+  ASTNodeDeclarationToAffectationConverter{*ast};
+  ASTNodeTypeCleaner<language::var_declaration>{*ast};
+  ASTNodeTypeCleaner<language::fct_declaration>{*ast};
+
+  ASTNodeExpressionBuilder{*ast};
+  ExecutionPolicy exec_policy;
+  ASTExecutionStack::create();
+  ASTCheckpointsInfoTester ast_cp_info_tester{*ast};
+  ASTExecutionStack::destroy();
+  ExecutionStatManager::destroy();
+  ast->m_symbol_table->clearValues();
+
+  REQUIRE(ASTCheckpointsInfo::getInstance().getASTCheckpoint(0).getASTLocation() == std::vector<size_t>{0, 3, 0});
+  REQUIRE(ASTCheckpointsInfo::getInstance().getASTCheckpoint(1).getASTLocation() == std::vector<size_t>{0, 3, 1, 1});
+  REQUIRE(ASTCheckpointsInfo::getInstance().getASTCheckpoint(2).getASTLocation() == std::vector<size_t>{0, 3, 2, 1});
+
+#ifndef NDEBUG
+  REQUIRE_THROWS_WITH(ASTCheckpointsInfoTester(*ast), "Can only define one ASTCheckpointsInfo");
+#endif   // NDEBUG
+}
-- 
GitLab