diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5608c5d57f46833a60fcec2ee7a5ee94cd59bedd..de029d999cb5a70fed90b34ec3d9be02f17f836a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,12 +3,13 @@ set(EXECUTABLE_OUTPUT_PATH ${PUGS_BINARY_DIR}) add_executable (unit_tests test_main.cpp - test_ASTSymbolTableBuilder.cpp - test_ASTSymbolInitializationChecker.cpp - test_ASTBuilder.cpp - test_ASTNodeDataType.cpp test_Array.cpp test_ArrayUtils.cpp + test_ASTDotPrinter.cpp + test_ASTBuilder.cpp + test_ASTNodeDataType.cpp + test_ASTSymbolTableBuilder.cpp + test_ASTSymbolInitializationChecker.cpp test_ItemType.cpp test_PugsAssert.cpp test_RevisionInfo.cpp diff --git a/tests/test_ASTDotPrinter.cpp b/tests/test_ASTDotPrinter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..486db0b4a2399123ef3affa0c57d2231fb828154 --- /dev/null +++ b/tests/test_ASTDotPrinter.cpp @@ -0,0 +1,45 @@ +#include <catch2/catch.hpp> + +#include <ASTBuilder.hpp> +#include <ASTDotPrinter.hpp> +#include <sstream> + +#define CHECK_DOT(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>); \ + \ + string_input input{data, "test.pgs"}; \ + auto ast = ASTBuilder::build(input); \ + \ + std::stringstream ast_output; \ + ast_output << '\n' << ASTDotPrinter{*ast}; \ + \ + REQUIRE(ast_output.str() == expected_output); \ + } + +TEST_CASE("ASTDotPrinter", "[language]") +{ + rang::setControlMode(rang::control::Off); + + std::string_view data = R"( +N n = 2 + 3; +)"; + + std::string_view result = R"( +digraph parse_tree +{ + x0 [ label="root \nundefined" ] + x0 -> { x1 } + x1 [ label="language::declaration\nN n = 2 + 3\nundefined" ] + x1 -> { x2, x3, x4 } + x2 [ label="language::N_set\nN\nundefined" ] + x3 [ label="language::name\nn\nundefined" ] + x4 [ label="language::plus_op\nundefined" ] + x4 -> { x5, x6 } + x5 [ label="language::integer\n2\nundefined" ] + x6 [ label="language::integer\n3\nundefined" ] +} +)"; + CHECK_DOT(data, result); +}