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

Add tests for AffectationToStringProcessor

parent 44e84526
No related branches found
No related tags found
1 merge request!37Feature/language
......@@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PUGS_BINARY_DIR})
add_executable (unit_tests
test_main.cpp
test_AffectationProcessor.cpp
test_AffectationToStringProcessor.cpp
test_Array.cpp
test_ArrayUtils.cpp
test_ASTBuilder.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 <sstream>
#define CHECK_AFFECTATION_RESULT(data, variable_name, 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; \
ast->execute(exec_policy); \
\
auto symbol_table = ast->m_symbol_table; \
\
using namespace TAO_PEGTL_NAMESPACE; \
position use_position{internal::iterator{"fixture"}, "fixture"}; \
use_position.byte = 10000; \
auto [symbol, found] = symbol_table->find(variable_name, use_position); \
\
auto attribute = symbol->second; \
auto value = std::get<decltype(expected_value)>(attribute.value()); \
\
REQUIRE(value == expected_value); \
}
TEST_CASE("ASTAffectationToStringProcessor", "[language]")
{
SECTION("Affectations")
{
CHECK_AFFECTATION_RESULT(R"(string s; s = "foo";)", "s", std::string("foo"));
CHECK_AFFECTATION_RESULT(R"(N n = 2; string s; s = n;)", "s", std::to_string(2ul));
CHECK_AFFECTATION_RESULT(R"(string s; s = -1;)", "s", std::to_string(-1l));
CHECK_AFFECTATION_RESULT(R"(string s; s = true;)", "s", std::to_string(true));
CHECK_AFFECTATION_RESULT(R"(string s; s = 2.3;)", "s", std::to_string(2.3));
}
SECTION("+=")
{
CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += "bar";)", "s", std::string("foobar"));
CHECK_AFFECTATION_RESULT(R"(N n = 2; string s = "foo"; s += n;)", "s", (std::string("foo") + std::to_string(2ul)));
CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += -1;)", "s", (std::string("foo") + std::to_string(-1l)));
CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += true;)", "s", (std::string("foo") + std::to_string(true)));
CHECK_AFFECTATION_RESULT(R"(string s = "foo"; s += 2.3;)", "s", (std::string("foo") + std::to_string(2.3)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment