Add base for simple expression unit tests

This commit is contained in:
Jan 2021-11-25 01:20:51 +01:00
parent c5475ce780
commit 7680f92ae1
5 changed files with 158 additions and 2 deletions

View File

@ -129,6 +129,7 @@ group ""
include "test/ObjCommonTests.lua"
include "test/ObjLoadingTests.lua"
include "test/ParserTestUtils.lua"
include "test/ParserTests.lua"
include "test/ZoneCodeGeneratorLibTests.lua"
include "test/ZoneCommonTests.lua"
@ -137,6 +138,7 @@ group "Tests"
ObjCommonTests:project()
ObjLoadingTests:project()
ParserTestUtils:project()
ParserTests:project()
ZoneCodeGeneratorLibTests:project()
ZoneCommonTests:project()
group ""

View File

@ -34,6 +34,7 @@ private:
public:
SimpleExpressionMatchers();
SimpleExpressionMatchers(bool enableStringOperands, bool enableIdentifierOperands, bool enableFloatingPointOperands, bool enableIntOperands);
virtual ~SimpleExpressionMatchers();
SimpleExpressionMatchers(const SimpleExpressionMatchers& other) = default;
SimpleExpressionMatchers(SimpleExpressionMatchers&& other) noexcept = default;
@ -41,8 +42,6 @@ public:
SimpleExpressionMatchers& operator=(SimpleExpressionMatchers&& other) noexcept = default;
protected:
SimpleExpressionMatchers(bool enableStringOperands, bool enableIdentifierOperands, bool enableFloatingPointOperands, bool enableIntOperands);
virtual std::unique_ptr<matcher_t> ParseOperandExtension(const supplier_t* labelSupplier) const;
virtual std::unique_ptr<ISimpleExpression> ProcessOperandExtension(SequenceResult<SimpleParserValue>& result) const;

53
test/ParserTests.lua Normal file
View File

@ -0,0 +1,53 @@
ParserTests = {}
function ParserTests:include(includes)
if includes:handle(self:name()) then
includedirs {
path.join(TestFolder(), "ParserTests")
}
end
end
function ParserTests:link(links)
end
function ParserTests:use()
end
function ParserTests:name()
return "ParserTests"
end
function ParserTests:project()
local folder = TestFolder()
local includes = Includes:create()
local links = Links:create()
project(self:name())
targetdir(TargetDirectoryTest)
location "%{wks.location}/test/%{prj.name}"
kind "ConsoleApp"
language "C++"
files {
path.join(folder, "ParserTests/**.h"),
path.join(folder, "ParserTests/**.cpp")
}
vpaths {
["*"] = {
path.join(folder, "ParserTests")
}
}
self:include(includes)
ParserTestUtils:include(includes)
Parser:include(includes)
catch2:include(includes)
links:linkto(ParserTestUtils)
links:linkto(Parser)
links:linkall()
end

View File

@ -0,0 +1,100 @@
#include <catch2/catch.hpp>
#include "Parsing/Impl/AbstractParser.h"
#include "Utils/ClassUtils.h"
#include "Parsing/Mock/MockLexer.h"
#include "Parsing/Simple/SimpleParserValue.h"
#include "Parsing/Simple/Expression/ISimpleExpression.h"
#include "Parsing/Simple/Expression/SimpleExpressionMatchers.h"
#include "Parsing/Simple/Matcher/SimpleMatcherFactory.h"
namespace test::parsing::simple::expression
{
class SimpleExpressionTestState
{
public:
std::unique_ptr<ISimpleExpression> m_expression;
};
class SimpleExpressionSequence final : public AbstractSequence<SimpleParserValue, SimpleExpressionTestState>
{
const SimpleExpressionMatchers m_expression_matchers;
public:
SimpleExpressionSequence()
: m_expression_matchers(true, true, true, true)
{
const SimpleMatcherFactory create(this);
AddLabeledMatchers(m_expression_matchers.Expression(this), SimpleExpressionMatchers::LABEL_EXPRESSION);
AddMatchers(create.Label(SimpleExpressionMatchers::LABEL_EXPRESSION));
}
protected:
void ProcessMatch(SimpleExpressionTestState* state, SequenceResult<SimpleParserValue>& result) const override
{
state->m_expression = m_expression_matchers.ProcessExpression(result);
}
};
class SimpleExpressionTestsHelper
{
public:
std::unique_ptr<SimpleExpressionTestState> m_state;
std::unique_ptr<ILexer<SimpleParserValue>> m_lexer;
std::unique_ptr<SimpleExpressionSequence> m_sequence;
unsigned m_consumed_token_count;
explicit SimpleExpressionTestsHelper()
: m_state(std::make_unique<SimpleExpressionTestState>()),
m_sequence(std::make_unique<SimpleExpressionSequence>()),
m_consumed_token_count(0u)
{
}
void Tokens(std::initializer_list<Movable<SimpleParserValue>> tokens)
{
m_lexer = std::make_unique<MockLexer<SimpleParserValue>>(tokens, SimpleParserValue::EndOfFile(TokenPos()));
}
void Tokens(std::vector<SimpleParserValue> tokens)
{
m_lexer = std::make_unique<MockLexer<SimpleParserValue>>(std::move(tokens), SimpleParserValue::EndOfFile(TokenPos()));
}
bool PerformTest()
{
REQUIRE(m_lexer);
m_consumed_token_count = 0;
return m_sequence->MatchSequence(m_lexer.get(), m_state.get(), m_consumed_token_count);
}
};
TEST_CASE("SimpleExpressions: Can parse expression with add operation", "[parsing][simple][expression]")
{
SimpleExpressionTestsHelper helper;
const TokenPos pos;
helper.Tokens({
SimpleParserValue::Integer(pos, 1336),
SimpleParserValue::Character(pos, '+'),
SimpleParserValue::Integer(pos, 1),
SimpleParserValue::EndOfFile(pos)
});
const auto result = helper.PerformTest();
REQUIRE(result);
REQUIRE(helper.m_consumed_token_count == 3);
const auto& expression = helper.m_state->m_expression;
REQUIRE(expression->IsStatic());
const auto value = expression->Evaluate();
REQUIRE(value.m_type == SimpleExpressionValue::Type::INT);
REQUIRE(value.m_int_value == 1337);
}
}

View File

@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>