Add base for menu parsing unit tests

This commit is contained in:
Jan 2021-11-14 12:46:13 +01:00
parent 13589fd43c
commit 0033ab2a2b
19 changed files with 288 additions and 9 deletions

View File

@ -127,12 +127,16 @@ group ""
-- Tests
-- ========================
include "test/ObjCommonTests.lua"
include "test/ObjLoadingTests.lua"
include "test/ParserTestUtils.lua"
include "test/ZoneCodeGeneratorLibTests.lua"
include "test/ZoneCommonTests.lua"
-- Tests group: Unit test and other tests projects
group "Tests"
ObjCommonTests:project()
ObjLoadingTests:project()
ParserTestUtils:project()
ZoneCodeGeneratorLibTests:project()
ZoneCommonTests:project()
group ""

View File

@ -13,7 +13,7 @@ CommonEventHandlerCondition::CommonEventHandlerCondition(std::unique_ptr<ICommon
{
}
CommonEventHandlerElementType CommonEventHandlerCondition::GetType()
CommonEventHandlerElementType CommonEventHandlerCondition::GetType() const
{
return CommonEventHandlerElementType::CONDITION;
}

View File

@ -16,9 +16,9 @@ namespace menu
std::unique_ptr<CommonEventHandlerSet> m_else_elements;
CommonEventHandlerCondition();
explicit CommonEventHandlerCondition(std::unique_ptr<ICommonExpression> condition, std::unique_ptr<CommonEventHandlerSet> conditionElements,
std::unique_ptr<CommonEventHandlerSet> elseElements);
CommonEventHandlerCondition(std::unique_ptr<ICommonExpression> condition, std::unique_ptr<CommonEventHandlerSet> conditionElements,
std::unique_ptr<CommonEventHandlerSet> elseElements);
CommonEventHandlerElementType GetType() override;
_NODISCARD CommonEventHandlerElementType GetType() const override;
};
}

View File

@ -10,7 +10,7 @@ CommonEventHandlerScript::CommonEventHandlerScript(std::string script)
{
}
CommonEventHandlerElementType CommonEventHandlerScript::GetType()
CommonEventHandlerElementType CommonEventHandlerScript::GetType() const
{
return CommonEventHandlerElementType::SCRIPT;
}

View File

@ -14,6 +14,6 @@ namespace menu
CommonEventHandlerScript();
explicit CommonEventHandlerScript(std::string script);
CommonEventHandlerElementType GetType() override;
_NODISCARD CommonEventHandlerElementType GetType() const override;
};
}

View File

@ -14,7 +14,7 @@ CommonEventHandlerSetLocalVar::CommonEventHandlerSetLocalVar(SetLocalVarType typ
{
}
CommonEventHandlerElementType CommonEventHandlerSetLocalVar::GetType()
CommonEventHandlerElementType CommonEventHandlerSetLocalVar::GetType() const
{
return CommonEventHandlerElementType::SET_LOCAL_VAR;
}

View File

@ -26,6 +26,6 @@ namespace menu
CommonEventHandlerSetLocalVar();
CommonEventHandlerSetLocalVar(SetLocalVarType type, std::string varName, std::unique_ptr<ICommonExpression> value);
CommonEventHandlerElementType GetType() override;
_NODISCARD CommonEventHandlerElementType GetType() const override;
};
}

View File

@ -1,5 +1,7 @@
#pragma once
#include "Utils/ClassUtils.h"
namespace menu
{
enum class CommonEventHandlerElementType
@ -21,6 +23,6 @@ namespace menu
ICommonEventHandlerElement& operator=(const ICommonEventHandlerElement& other) = default;
ICommonEventHandlerElement& operator=(ICommonEventHandlerElement&& other) noexcept = default;
virtual CommonEventHandlerElementType GetType() = 0;
_NODISCARD virtual CommonEventHandlerElementType GetType() const = 0;
};
}

53
test/ObjLoadingTests.lua Normal file
View File

@ -0,0 +1,53 @@
ObjLoadingTests = {}
function ObjLoadingTests:include(includes)
if includes:handle(self:name()) then
includedirs {
path.join(TestFolder(), "ObjLoadingTests")
}
end
end
function ObjLoadingTests:link(links)
end
function ObjLoadingTests:use()
end
function ObjLoadingTests:name()
return "ObjLoadingTests"
end
function ObjLoadingTests: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, "ObjLoadingTests/**.h"),
path.join(folder, "ObjLoadingTests/**.cpp")
}
vpaths {
["*"] = {
path.join(folder, "ObjLoadingTests")
}
}
self:include(includes)
ParserTestUtils:include(includes)
ObjLoading:include(includes)
catch2:include(includes)
links:linkto(ParserTestUtils)
links:linkto(ObjLoading)
links:linkall()
end

View File

@ -0,0 +1,164 @@
#include <catch2/catch.hpp>
#include "Parsing/Menu/Domain/EventHandler/CommonEventHandlerScript.h"
#include "Utils/ClassUtils.h"
#include "Parsing/Menu/Sequence/EventHandlerSetScopeSequences.h"
#include "Parsing/Mock/MockLexer.h"
using namespace menu;
namespace test::parsing::menu::sequence::event_handler_set
{
class EventHandlerSetSequenceTestsHelper
{
public:
std::vector<std::unique_ptr<MenuFileParser::sequence_t>> m_all_sequences;
std::vector<MenuFileParser::sequence_t*> m_scope_sequences;
std::unique_ptr<MenuFileParserState> m_state;
std::unique_ptr<ILexer<SimpleParserValue>> m_lexer;
std::unique_ptr<CommonEventHandlerSet> m_event_handler_set;
unsigned m_consumed_token_count;
explicit EventHandlerSetSequenceTestsHelper(FeatureLevel featureLevel)
: m_state(std::make_unique<MenuFileParserState>(featureLevel)),
m_event_handler_set(std::make_unique<CommonEventHandlerSet>()),
m_consumed_token_count(0u)
{
EventHandlerSetScopeSequences scopeSequences(m_all_sequences, m_scope_sequences);
scopeSequences.AddSequences(m_state->m_feature_level);
m_state->m_current_menu = m_state->m_menus.emplace_back(std::make_unique<CommonMenuDef>()).get();
m_state->m_current_event_handler_set = m_event_handler_set.get();
m_state->m_current_nested_event_handler_set = m_event_handler_set.get();
}
void Tokens(std::initializer_list<Movable<SimpleParserValue>> tokens)
{
m_lexer = std::make_unique<MockLexer<SimpleParserValue>>(tokens, SimpleParserValue::EndOfFile(TokenPos()));
}
bool PerformTest()
{
REQUIRE(m_lexer);
m_consumed_token_count = 0;
for (const auto* sequence : m_scope_sequences)
{
const auto couldMatch = sequence->MatchSequence(m_lexer.get(), m_state.get(), m_consumed_token_count);
if (couldMatch)
return couldMatch;
}
return false;
}
};
TEST_CASE("EventHandlerSetScopeSequences: Ensure can use fadeIn", "[parsing][sequence][menu]")
{
EventHandlerSetSequenceTestsHelper helper(FeatureLevel::IW4);
const TokenPos pos;
helper.Tokens({
SimpleParserValue::Identifier(pos, new std::string("fadeIn")),
SimpleParserValue::String(pos, new std::string("some_element")),
SimpleParserValue::Character(pos, ';'),
SimpleParserValue::EndOfFile(pos)
});
const auto result = helper.PerformTest();
REQUIRE(result);
REQUIRE(helper.m_consumed_token_count == 3);
const auto script = helper.m_state->m_current_script.str();
REQUIRE(script == R"("fadeIn" "some_element" ; )");
}
TEST_CASE("EventHandlerSetScopeSequences: Keyword casing doesnt matter", "[parsing][sequence][menu]")
{
EventHandlerSetSequenceTestsHelper helper(FeatureLevel::IW4);
const TokenPos pos;
helper.Tokens({
SimpleParserValue::Identifier(pos, new std::string("fadein")),
SimpleParserValue::String(pos, new std::string("some_element")),
SimpleParserValue::Character(pos, ';'),
SimpleParserValue::EndOfFile(pos)
});
const auto result = helper.PerformTest();
REQUIRE(result);
REQUIRE(helper.m_consumed_token_count == 3);
const auto script = helper.m_state->m_current_script.str();
REQUIRE(script == R"("fadein" "some_element" ; )");
}
TEST_CASE("EventHandlerSetScopeSequences: Invalid keywords are not recognized", "[parsing][sequence][menu]")
{
EventHandlerSetSequenceTestsHelper helper(FeatureLevel::IW4);
const TokenPos pos;
helper.Tokens({
SimpleParserValue::Identifier(pos, new std::string("noScriptCommand")),
SimpleParserValue::String(pos, new std::string("some_element")),
SimpleParserValue::Character(pos, ';'),
SimpleParserValue::EndOfFile(pos)
});
const auto result = helper.PerformTest();
REQUIRE(!result);
REQUIRE(helper.m_consumed_token_count == 0);
}
TEST_CASE("EventHandlerSetScopeSequences: Closing block terminates EventHandlerSet", "[parsing][sequence][menu]")
{
EventHandlerSetSequenceTestsHelper helper(FeatureLevel::IW4);
const TokenPos pos;
helper.Tokens({
SimpleParserValue::Character(pos, '}'),
SimpleParserValue::EndOfFile(pos)
});
const auto result = helper.PerformTest();
REQUIRE(result);
REQUIRE(helper.m_consumed_token_count == 1);
const auto script = helper.m_state->m_current_script.str();
REQUIRE(helper.m_state->m_current_event_handler_set == nullptr);
REQUIRE(helper.m_state->m_current_nested_event_handler_set == nullptr);
REQUIRE(helper.m_event_handler_set);
REQUIRE(helper.m_event_handler_set->m_elements.empty());
}
TEST_CASE("EventHandlerSetScopeSequences: Closing block finishes current script", "[parsing][sequence][menu]")
{
EventHandlerSetSequenceTestsHelper helper(FeatureLevel::IW4);
const TokenPos pos;
helper.Tokens({
SimpleParserValue::Character(pos, '}'),
SimpleParserValue::EndOfFile(pos)
});
helper.m_state->m_current_script << R"("fadeIn" "some_element" ; )";
const auto result = helper.PerformTest();
REQUIRE(result);
REQUIRE(helper.m_consumed_token_count == 1);
REQUIRE(helper.m_state->m_current_event_handler_set == nullptr);
REQUIRE(helper.m_state->m_current_nested_event_handler_set == nullptr);
REQUIRE(helper.m_event_handler_set);
REQUIRE(helper.m_event_handler_set->m_elements.size() == 1);
const auto* firstElement = helper.m_event_handler_set->m_elements[0].get();
REQUIRE(firstElement->GetType() == CommonEventHandlerElementType::SCRIPT);
const auto* scriptElement = dynamic_cast<const CommonEventHandlerScript*>(firstElement);
REQUIRE(scriptElement != nullptr);
REQUIRE(scriptElement->m_script == R"("fadeIn" "some_element" ; )");
}
}

View File

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

52
test/ParserTestUtils.lua Normal file
View File

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

View File

@ -44,8 +44,10 @@ function ZoneCodeGeneratorLibTests:project()
self:include(includes)
ZoneCodeGeneratorLib:include(includes)
ParserTestUtils:include(includes)
catch2:include(includes)
links:linkto(ZoneCodeGeneratorLib)
links:linkto(ParserTestUtils)
links:linkall()
end