From d890961fd9f9a4ff0be541517893f87deb6a3b64 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Tue, 3 Sep 2019 14:40:16 +0200 Subject: [PATCH] Add tests for OStreamProcessor --- tests/CMakeLists.txt | 1 + tests/test_OStreamProcessor.cpp | 79 +++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/test_OStreamProcessor.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b7b928feb..ef6cd0260 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 000000000..be958502a --- /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"); + } +} -- GitLab