From 547eb0688a3508be73414e39b016f6f3e4f5abad Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Tue, 3 Sep 2019 19:02:46 +0200
Subject: [PATCH] Add tests for ForProcessor

---
 src/language/node_processor/ForProcessor.hpp |   2 +-
 tests/CMakeLists.txt                         |   1 +
 tests/test_ForProcessor.cpp                  | 116 +++++++++++++++++++
 3 files changed, 118 insertions(+), 1 deletion(-)
 create mode 100644 tests/test_ForProcessor.cpp

diff --git a/src/language/node_processor/ForProcessor.hpp b/src/language/node_processor/ForProcessor.hpp
index 990640db1..e06db01d0 100644
--- a/src/language/node_processor/ForProcessor.hpp
+++ b/src/language/node_processor/ForProcessor.hpp
@@ -22,7 +22,7 @@ class ForProcessor final : public INodeProcessor
           if constexpr (std::is_arithmetic_v<T>) {
             return value;
           } else {
-            return false;
+            return false;   // LCOV_EXCL_LINE (unreachable: only there for compilation purpose)
           }
         },
         m_node.children[1]->m_value));
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 5b8af5949..1d81a0716 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -32,6 +32,7 @@ add_executable (unit_tests
   test_CRSMatrix.cpp
   test_ExecUntilBreakOrContinue.cpp
   test_FakeProcessor.cpp
+  test_ForProcessor.cpp
   test_IfProcessor.cpp
   test_IncDecExpressionProcessor.cpp
   test_INodeProcessor.cpp
diff --git a/tests/test_ForProcessor.cpp b/tests/test_ForProcessor.cpp
new file mode 100644
index 000000000..bd833f7f5
--- /dev/null
+++ b/tests/test_ForProcessor.cpp
@@ -0,0 +1,116 @@
+#include <catch2/catch.hpp>
+
+#include <ASTNodeValueBuilder.hpp>
+
+#include <ASTBuilder.hpp>
+#include <ASTNodeDataTypeBuilder.hpp>
+
+#include <ASTNodeDeclarationCleaner.hpp>
+#include <ASTNodeDeclarationToAffectationConverter.hpp>
+
+#include <ASTNodeExpressionBuilder.hpp>
+
+#include <ASTNodeAffectationExpressionBuilder.hpp>
+
+#include <ASTSymbolTableBuilder.hpp>
+
+#include <ASTPrinter.hpp>
+
+#include <Demangle.hpp>
+
+#include <PEGGrammar.hpp>
+
+#include <sstream>
+
+#define CHECK_FOR_PROCESSOR_RESULT(data, variable_name, expected_value)     \
+  {                                                                         \
+    string_input input{data, "test.pgs"};                                   \
+    auto ast = ASTBuilder::build(input);                                    \
+                                                                            \
+    ASTSymbolTableBuilder{*ast};                                            \
+    ASTNodeDataTypeBuilder{*ast};                                           \
+    ASTNodeValueBuilder{*ast};                                              \
+                                                                            \
+    ASTNodeDeclarationToAffectationConverter{*ast};                         \
+    ASTNodeDeclarationCleaner{*ast};                                        \
+                                                                            \
+    ASTNodeExpressionBuilder{*ast};                                         \
+    ExecUntilBreakOrContinue exec_policy;                                   \
+    ast->execute(exec_policy);                                              \
+                                                                            \
+    auto symbol_table = ast->m_symbol_table;                                \
+                                                                            \
+    using namespace TAO_PEGTL_NAMESPACE;                                    \
+    position use_position{internal::iterator{"fixture"}, "fixture"};        \
+    use_position.byte    = 10000;                                           \
+    auto [symbol, found] = symbol_table->find(variable_name, use_position); \
+                                                                            \
+    auto attribute = symbol->second;                                        \
+    auto value     = std::get<decltype(expected_value)>(attribute.value()); \
+                                                                            \
+    REQUIRE(value == expected_value);                                       \
+  }
+
+TEST_CASE("ForProcessor", "[language]")
+{
+  SECTION("simple for")
+  {
+    std::string_view data = R"(
+N i = 0;
+for(N l=0; l<10; ++l) {
+  i += l;
+}
+)";
+    CHECK_FOR_PROCESSOR_RESULT(data, "i", 45ul);
+  }
+
+  SECTION("for with break")
+  {
+    std::string_view data = R"(
+N i = 0;
+for(N l=0; l<10; ++l) {
+  i += l;
+  if (i > 30) break;
+}
+)";
+    CHECK_FOR_PROCESSOR_RESULT(data, "i", 36ul);
+  }
+
+  SECTION("for with continue")
+  {
+    std::string_view data = R"(
+N i = 0;
+for(N l=0; l<10; ++l) {
+  if (l<3) continue;
+  i += l;
+}
+)";
+    CHECK_FOR_PROCESSOR_RESULT(data, "i", 42ul);
+  }
+
+  //   SECTION("simple if(true)else")
+  //   {
+  //     std::string_view data = R"(
+  // N i = 0;
+  // if(true) {
+  //   i = 1;
+  // } else {
+  //   i = 2;
+  // }
+  // )";
+  //     CHECK_FOR_PROCESSOR_RESULT(data, "i", 1ul);
+  //   }
+
+  //   SECTION("simple if(false)")
+  //   {
+  //     std::string_view data = R"(
+  // N i = 0;
+  // if(false) {
+  //   i = 1;
+  // } else {
+  //   i = 2;
+  // }
+  // )";
+  //     CHECK_FOR_PROCESSOR_RESULT(data, "i", 2ul);
+  //   }
+}
-- 
GitLab