diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b7b928febaf0e5be6528a6428c3084474442b8e0..ef6cd0260ba1f0929607ac61f1abe46a55f57fef 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -36,6 +36,7 @@ add_executable (unit_tests test_INodeProcessor.cpp test_ItemType.cpp test_NameProcessor.cpp + test_OStreamProcessor.cpp test_PCG.cpp test_PugsAssert.cpp test_RevisionInfo.cpp diff --git a/tests/test_OStreamProcessor.cpp b/tests/test_OStreamProcessor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be958502a5b6e62d01c458405e4440eca792c20d --- /dev/null +++ b/tests/test_OStreamProcessor.cpp @@ -0,0 +1,79 @@ +#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 <node_processor/OStreamProcessor.hpp> + +#include <sstream> + +void +_replaceOStream(ASTNode& node, std::ostringstream& sout) +{ + if (node.is<language::cout_kw>() or node.is<language::cerr_kw>()) { + node.m_node_processor = std::make_unique<OStreamProcessor>(node, sout); + } else { + for (auto& child_node : node.children) { + _replaceOStream(*child_node, sout); + } + } +} + +#define CHECK_OSTREAM_EXPRESSION_RESULT(data, 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; \ + \ + std::ostringstream sout; \ + _replaceOStream(*ast, sout); \ + \ + ast->execute(exec_policy); \ + \ + REQUIRE(sout.str() == expected_value); \ + } + +TEST_CASE("OStreamProcessor", "[language]") +{ + SECTION("cout") + { + CHECK_OSTREAM_EXPRESSION_RESULT(R"(cout << 2;)", "2"); + CHECK_OSTREAM_EXPRESSION_RESULT(R"(cout << true;)", "true"); + CHECK_OSTREAM_EXPRESSION_RESULT(R"(cout << false;)", "false"); + CHECK_OSTREAM_EXPRESSION_RESULT(R"(cout << "x=" << 2 << "\n";)", "x=2\n"); + } + + SECTION("cerr") + { + CHECK_OSTREAM_EXPRESSION_RESULT(R"(cerr << 2;)", "2"); + CHECK_OSTREAM_EXPRESSION_RESULT(R"(cerr << true;)", "true"); + CHECK_OSTREAM_EXPRESSION_RESULT(R"(cerr << false;)", "false"); + CHECK_OSTREAM_EXPRESSION_RESULT(R"(cerr << "x=" << 2 << "\n";)", "x=2\n"); + } +}