diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 469eeabbeef895a0d0a0b56edcbbda4939917793..9c9404535ffd08d971fa59ae32cc07607cc52b8d 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_AffectationProcessor.cpp + test_AffectationToStringProcessor.cpp test_Array.cpp test_ArrayUtils.cpp test_ASTBuilder.cpp diff --git a/tests/test_AffectationToStringProcessor.cpp b/tests/test_AffectationToStringProcessor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb2ba582482b1fb0b7a9c68a0813e066947ad5ca --- /dev/null +++ b/tests/test_AffectationToStringProcessor.cpp @@ -0,0 +1,73 @@ +#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_AFFECTATION_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("ASTAffectationToStringProcessor", "[language]") +{ + SECTION("Affectations") + { + CHECK_AFFECTATION_RESULT(R"(string s; s = "foo";)", "s", std::string("foo")); + CHECK_AFFECTATION_RESULT(R"(N n = 2; string s; s = n;)", "s", std::to_string(2ul)); + CHECK_AFFECTATION_RESULT(R"(string s; s = -1;)", "s", std::to_string(-1l)); + CHECK_AFFECTATION_RESULT(R"(string s; s = true;)", "s", std::to_string(true)); + CHECK_AFFECTATION_RESULT(R"(string s; s = 2.3;)", "s", std::to_string(2.3)); + } + + SECTION("+=") + { + CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += "bar";)", "s", std::string("foobar")); + CHECK_AFFECTATION_RESULT(R"(N n = 2; string s = "foo"; s += n;)", "s", (std::string("foo") + std::to_string(2ul))); + CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += -1;)", "s", (std::string("foo") + std::to_string(-1l))); + CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += true;)", "s", (std::string("foo") + std::to_string(true))); + CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += 2.3;)", "s", (std::string("foo") + std::to_string(2.3))); + } +}