diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 21f7c62236482c6cb033dbfe717f86b486df5f45..084dd96eeb1fecbdac00abe7117602d801da8329 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable (unit_tests test_ASTNodeDataTypeBuilder.cpp test_ASTNodeDataTypeChecker.cpp test_ASTNodeDeclarationToAffectationConverter.cpp + test_ASTNodeEmptyBlockCleaner.cpp test_ASTNodeExpressionBuilder.cpp test_ASTNodeFunctionEvaluationExpressionBuilder.cpp test_ASTNodeIncDecExpressionBuilder.cpp diff --git a/tests/test_ASTNodeEmptyBlockCleaner.cpp b/tests/test_ASTNodeEmptyBlockCleaner.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1076d45e8ba60f85a79af24377d05f336ac0240 --- /dev/null +++ b/tests/test_ASTNodeEmptyBlockCleaner.cpp @@ -0,0 +1,149 @@ +#include <catch2/catch.hpp> + +#include <ASTNodeValueBuilder.hpp> + +#include <ASTBuilder.hpp> +#include <ASTNodeDataTypeBuilder.hpp> + +#include <ASTNodeDeclarationToAffectationConverter.hpp> +#include <ASTNodeTypeCleaner.hpp> + +#include <ASTNodeEmptyBlockCleaner.hpp> + +#include <ASTNodeExpressionBuilder.hpp> + +#include <ASTSymbolTableBuilder.hpp> + +#include <ASTPrinter.hpp> + +#include <PEGGrammar.hpp> + +#include <Demangle.hpp> + +#define CHECK_AST(data, expected_output) \ + { \ + static_assert(std::is_same_v<std::decay_t<decltype(data)>, std::string_view>); \ + static_assert((std::is_same_v<std::decay_t<decltype(expected_output)>, std::string_view>) or \ + (std::is_same_v<std::decay_t<decltype(expected_output)>, std::string>)); \ + \ + string_input input{data, "test.pgs"}; \ + auto ast = ASTBuilder::build(input); \ + \ + ASTSymbolTableBuilder{*ast}; \ + ASTNodeDataTypeBuilder{*ast}; \ + ASTNodeValueBuilder{*ast}; \ + \ + ASTNodeDeclarationToAffectationConverter{*ast}; \ + ASTNodeTypeCleaner<language::declaration>{*ast}; \ + \ + ASTNodeEmptyBlockCleaner{*ast}; \ + \ + ASTNodeExpressionBuilder{*ast}; \ + std::stringstream ast_output; \ + ast_output << '\n' << ASTPrinter{*ast, ASTPrinter::Format::raw, {ASTPrinter::Info::exec_type}}; \ + \ + REQUIRE(ast_output.str() == expected_output); \ + } + +TEST_CASE("ASTNodeEmptyBlockCleaner", "[language]") +{ + SECTION("empty file") + { + std::string_view data = R"( +)"; + + std::string_view result = R"( +(root:ASTNodeListProcessor) +)"; + + CHECK_AST(data, result); + } + + SECTION("keep non-empty block") + { + std::string_view data = R"( +{ + R x = 3; +} +)"; + + std::string_view result = R"( +(root:ASTNodeListProcessor) + `-(language::block:ASTNodeListProcessor) + `-(language::eq_op:AffectationProcessor<language::eq_op, double, long>) + +-(language::name:x:NameProcessor) + `-(language::integer:3:FakeProcessor) +)"; + + CHECK_AST(data, result); + } + + SECTION("remove empty block") + { + std::string_view data = R"( +{ + R x; +} +)"; + + std::string_view result = R"( +(root:ASTNodeListProcessor) +)"; + + CHECK_AST(data, result); + } + + SECTION("remove nested empty blocks") + { + std::string_view data = R"( +{ + R x; + { + R y; + } + { + R z; + { + R w; + } + } +} +)"; + + std::string_view result = R"( +(root:ASTNodeListProcessor) +)"; + + CHECK_AST(data, result); + } + + SECTION("remove nested empty blocks") + { + std::string_view data = R"( +{ + R x; + { + R y; + } + { + R z; + { + R w = 4; + } + } +} +)"; + + std::string_view result = R"( +(root:ASTNodeListProcessor) + `-(language::block:ASTNodeListProcessor) + `-(language::block:ASTNodeListProcessor) + `-(language::block:ASTNodeListProcessor) + `-(language::eq_op:AffectationProcessor<language::eq_op, double, long>) + +-(language::name:w:NameProcessor) + `-(language::integer:4:FakeProcessor) +)"; + + CHECK_AST(data, result); + } +}