From b29173e40dfad687a9bfaf39a3d22c758fbddf55 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Tue, 3 Sep 2019 18:52:39 +0200 Subject: [PATCH] Add tests for IfProcessor --- src/language/node_processor/IfProcessor.hpp | 4 +- tests/CMakeLists.txt | 1 + tests/test_IfProcessor.cpp | 103 ++++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tests/test_IfProcessor.cpp diff --git a/src/language/node_processor/IfProcessor.hpp b/src/language/node_processor/IfProcessor.hpp index 4d28cce58..0f0444a27 100644 --- a/src/language/node_processor/IfProcessor.hpp +++ b/src/language/node_processor/IfProcessor.hpp @@ -13,13 +13,13 @@ class IfProcessor final : public INodeProcessor execute(ExecUntilBreakOrContinue& exec_policy) { m_node.children[0]->execute(exec_policy); - const bool is_true = static_cast<bool>(std::visit( + const bool is_true = static_cast<bool>(std::visit( // LCOV_EXCL_LINE (false negative) [](const auto& value) -> bool { using T = std::decay_t<decltype(value)>; if constexpr (std::is_arithmetic_v<T>) { return value; } else { - return false; + return false; // LCOV_EXCL_LINE (only there for compilation purpose unreachable) } }, m_node.children[0]->m_value)); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 456f6f987..5b8af5949 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_IfProcessor.cpp test_IncDecExpressionProcessor.cpp test_INodeProcessor.cpp test_ItemType.cpp diff --git a/tests/test_IfProcessor.cpp b/tests/test_IfProcessor.cpp new file mode 100644 index 000000000..a316f2d31 --- /dev/null +++ b/tests/test_IfProcessor.cpp @@ -0,0 +1,103 @@ +#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_IF_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("IfProcessor", "[language]") +{ + SECTION("simple if(true)") + { + std::string_view data = R"( +N i = 0; +if(true) { + i = 1; +} +)"; + CHECK_IF_PROCESSOR_RESULT(data, "i", 1ul); + } + + SECTION("simple if(false)") + { + std::string_view data = R"( +N i = 0; +if(false) { + i = 1; +} +)"; + CHECK_IF_PROCESSOR_RESULT(data, "i", 0ul); + } + + SECTION("simple if(true)else") + { + std::string_view data = R"( +N i = 0; +if(true) { + i = 1; +} else { + i = 2; +} +)"; + CHECK_IF_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_IF_PROCESSOR_RESULT(data, "i", 2ul); + } +} -- GitLab