mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
add unit tests for sequence architecture
This commit is contained in:
parent
5a7b184aa2
commit
e09793818f
@ -9,27 +9,22 @@ SequenceArchitecture::SequenceArchitecture()
|
|||||||
|
|
||||||
AddMatchers({
|
AddMatchers({
|
||||||
create.Keyword("architecture"),
|
create.Keyword("architecture"),
|
||||||
create.Or({
|
create.Identifier().Capture(CAPTURE_ARCHITECTURE),
|
||||||
create.Keyword("x86").Tag(TAG_X86),
|
|
||||||
create.Keyword("x64").Tag(TAG_X64)
|
|
||||||
}),
|
|
||||||
create.Char(';')
|
create.Char(';')
|
||||||
});
|
});
|
||||||
|
|
||||||
|
m_architecture_mapping["x86"] = Architecture::X86;
|
||||||
|
m_architecture_mapping["x64"] = Architecture::X64;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SequenceArchitecture::ProcessMatch(CommandsParserState* state, SequenceResult<CommandsParserValue>& result) const
|
void SequenceArchitecture::ProcessMatch(CommandsParserState* state, SequenceResult<CommandsParserValue>& result) const
|
||||||
{
|
{
|
||||||
switch (result.NextTag())
|
const auto& architectureToken = result.NextCapture(CAPTURE_ARCHITECTURE);
|
||||||
{
|
|
||||||
case TAG_X86:
|
|
||||||
state->SetArchitecture(Architecture::X86);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case TAG_X64:
|
const auto foundArchitecture = m_architecture_mapping.find(architectureToken.IdentifierValue());
|
||||||
state->SetArchitecture(Architecture::X64);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
if(foundArchitecture == m_architecture_mapping.end())
|
||||||
throw ParsingException(TokenPos(), "Unknown architecture!");
|
throw ParsingException(architectureToken.GetPos(), "Unknown architecture");
|
||||||
}
|
|
||||||
|
state->SetArchitecture(foundArchitecture->second);
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,9 @@
|
|||||||
|
|
||||||
class SequenceArchitecture final : public CommandsParser::sequence_t
|
class SequenceArchitecture final : public CommandsParser::sequence_t
|
||||||
{
|
{
|
||||||
static constexpr auto TAG_X86 = 1;
|
static constexpr auto CAPTURE_ARCHITECTURE = 1;
|
||||||
static constexpr auto TAG_X64 = 2;
|
|
||||||
|
std::unordered_map<std::string, Architecture> m_architecture_mapping;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void ProcessMatch(CommandsParserState* state, SequenceResult<CommandsParserValue>& result) const override;
|
void ProcessMatch(CommandsParserState* state, SequenceResult<CommandsParserValue>& result) const override;
|
||||||
|
@ -0,0 +1,89 @@
|
|||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#include "Utils/ClassUtils.h"
|
||||||
|
#include "Parsing/Commands/Sequence/SequenceArchitecture.h"
|
||||||
|
#include "Parsing/Mock/MockLexer.h"
|
||||||
|
#include "Persistence/InMemory/InMemoryRepository.h"
|
||||||
|
|
||||||
|
namespace test::parsing::commands::sequence::sequence_architecture
|
||||||
|
{
|
||||||
|
class CommandsSequenceTestsHelper
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::unique_ptr<IDataRepository> m_repository;
|
||||||
|
std::unique_ptr<CommandsParserState> m_state;
|
||||||
|
std::unique_ptr<ILexer<CommandsParserValue>> m_lexer;
|
||||||
|
|
||||||
|
unsigned m_consumed_token_count;
|
||||||
|
|
||||||
|
CommandsSequenceTestsHelper()
|
||||||
|
: m_repository(std::make_unique<InMemoryRepository>()),
|
||||||
|
m_state(std::make_unique<CommandsParserState>(m_repository.get())),
|
||||||
|
m_consumed_token_count(0u)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Tokens(std::initializer_list<Movable<CommandsParserValue>> tokens)
|
||||||
|
{
|
||||||
|
m_lexer = std::make_unique<MockLexer<CommandsParserValue>>(tokens, CommandsParserValue::EndOfFile(TokenPos()));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PerformTest()
|
||||||
|
{
|
||||||
|
REQUIRE(m_lexer);
|
||||||
|
const auto sequence = std::make_unique<SequenceArchitecture>();
|
||||||
|
return sequence->MatchSequence(m_lexer.get(), m_state.get(), m_consumed_token_count);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_CASE("SequenceAction: Ensure can set x86", "[parsing][parsingstream]")
|
||||||
|
{
|
||||||
|
CommandsSequenceTestsHelper helper;
|
||||||
|
const TokenPos pos;
|
||||||
|
helper.Tokens({
|
||||||
|
CommandsParserValue::Identifier(pos, new std::string("architecture")),
|
||||||
|
CommandsParserValue::Identifier(pos, new std::string("x86")),
|
||||||
|
CommandsParserValue::Character(pos, ';'),
|
||||||
|
CommandsParserValue::EndOfFile(pos)
|
||||||
|
});
|
||||||
|
|
||||||
|
auto result = helper.PerformTest();
|
||||||
|
|
||||||
|
REQUIRE(result);
|
||||||
|
REQUIRE(helper.m_consumed_token_count == 3);
|
||||||
|
REQUIRE(helper.m_repository->GetArchitecture() == Architecture::X86);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SequenceAction: Ensure can set x64", "[parsing][parsingstream]")
|
||||||
|
{
|
||||||
|
CommandsSequenceTestsHelper helper;
|
||||||
|
const TokenPos pos;
|
||||||
|
helper.Tokens({
|
||||||
|
CommandsParserValue::Identifier(pos, new std::string("architecture")),
|
||||||
|
CommandsParserValue::Identifier(pos, new std::string("x86")),
|
||||||
|
CommandsParserValue::Character(pos, ';'),
|
||||||
|
CommandsParserValue::EndOfFile(pos)
|
||||||
|
});
|
||||||
|
|
||||||
|
auto result = helper.PerformTest();
|
||||||
|
|
||||||
|
REQUIRE(result);
|
||||||
|
REQUIRE(helper.m_consumed_token_count == 3);
|
||||||
|
REQUIRE(helper.m_repository->GetArchitecture() == Architecture::X86);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SequenceAction: Ensure cannot match unknown value", "[parsing][parsingstream]")
|
||||||
|
{
|
||||||
|
CommandsSequenceTestsHelper helper;
|
||||||
|
const TokenPos pos;
|
||||||
|
helper.Tokens({
|
||||||
|
CommandsParserValue::Identifier(pos, new std::string("architecture")),
|
||||||
|
CommandsParserValue::Identifier(pos, new std::string("x1337")),
|
||||||
|
CommandsParserValue::Character(pos, ';'),
|
||||||
|
CommandsParserValue::EndOfFile(pos)
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE_THROWS_AS(helper.PerformTest(), ParsingException);
|
||||||
|
REQUIRE(helper.m_repository->GetArchitecture() == Architecture::UNKNOWN);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user