From ed3e65fc651a229d09e7c9fcc73a0764e5d0c975 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Wed, 4 Sep 2019 12:07:13 +0200 Subject: [PATCH] Add tests for ConcatExpressionProcessor --- .../ConcatExpressionProcessor.hpp | 2 +- tests/CMakeLists.txt | 1 + tests/test_ConcatExpressionProcessor.cpp | 80 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 tests/test_ConcatExpressionProcessor.cpp diff --git a/src/language/node_processor/ConcatExpressionProcessor.hpp b/src/language/node_processor/ConcatExpressionProcessor.hpp index 0b0d210d1..a82495607 100644 --- a/src/language/node_processor/ConcatExpressionProcessor.hpp +++ b/src/language/node_processor/ConcatExpressionProcessor.hpp @@ -9,7 +9,7 @@ class ConcatExpressionProcessor final : public INodeProcessor private: ASTNode& m_node; - PUGS_INLINE auto + PUGS_INLINE void _eval(const std::string& a, const ASTNodeDataVariant& b, ASTNodeDataVariant& value) { if constexpr (std::is_same_v<B_DataT, std::string>) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index baa29881a..2dd4c589d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,6 +29,7 @@ add_executable (unit_tests test_BreakProcessor.cpp test_BiCGStab.cpp test_ContinueProcessor.cpp + test_ConcatExpressionProcessor.cpp test_CRSMatrix.cpp test_DoWhileProcessor.cpp test_ExecUntilBreakOrContinue.cpp diff --git a/tests/test_ConcatExpressionProcessor.cpp b/tests/test_ConcatExpressionProcessor.cpp new file mode 100644 index 000000000..3b4430248 --- /dev/null +++ b/tests/test_ConcatExpressionProcessor.cpp @@ -0,0 +1,80 @@ +#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_CONCAT_EXPRESSION_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("ConcatExpressionProcessor", "[language]") +{ + SECTION("string + string") + { + CHECK_CONCAT_EXPRESSION_RESULT(R"(string s = "foo"; s = s+"bar";)", "s", std::string{"foobar"}); + } + + SECTION("string + N") + { + CHECK_CONCAT_EXPRESSION_RESULT(R"(N n = 1; string s = "foo_"; s = s+n;)", "s", std::string{"foo_1"}); + } + + SECTION("string + Z") + { + CHECK_CONCAT_EXPRESSION_RESULT(R"(string s = "foo_"; s = s+2;)", "s", std::string{"foo_2"}); + } + + SECTION("string + R") + { + CHECK_CONCAT_EXPRESSION_RESULT(R"(string s = "foo_"; s = s+2.4;)", "s", std::string{"foo_"} + std::to_string(2.4)); + } + + SECTION("string + B") + { + CHECK_CONCAT_EXPRESSION_RESULT(R"(string s = "foo_"; s = s+true;)", "s", std::string{"foo_1"}); + } +} -- GitLab