Skip to content
Snippets Groups Projects
Commit d890961f authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add tests for OStreamProcessor

parent 79acfd60
No related branches found
No related tags found
1 merge request!37Feature/language
...@@ -36,6 +36,7 @@ add_executable (unit_tests ...@@ -36,6 +36,7 @@ add_executable (unit_tests
test_INodeProcessor.cpp test_INodeProcessor.cpp
test_ItemType.cpp test_ItemType.cpp
test_NameProcessor.cpp test_NameProcessor.cpp
test_OStreamProcessor.cpp
test_PCG.cpp test_PCG.cpp
test_PugsAssert.cpp test_PugsAssert.cpp
test_RevisionInfo.cpp test_RevisionInfo.cpp
......
#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");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment