add unit tests for sequence architecture

This commit is contained in:
Jan 2021-02-19 23:17:53 +01:00
parent 5a7b184aa2
commit e09793818f
3 changed files with 102 additions and 17 deletions

View File

@ -9,27 +9,22 @@ SequenceArchitecture::SequenceArchitecture()
AddMatchers({
create.Keyword("architecture"),
create.Or({
create.Keyword("x86").Tag(TAG_X86),
create.Keyword("x64").Tag(TAG_X64)
}),
create.Identifier().Capture(CAPTURE_ARCHITECTURE),
create.Char(';')
});
m_architecture_mapping["x86"] = Architecture::X86;
m_architecture_mapping["x64"] = Architecture::X64;
}
void SequenceArchitecture::ProcessMatch(CommandsParserState* state, SequenceResult<CommandsParserValue>& result) const
{
switch (result.NextTag())
{
case TAG_X86:
state->SetArchitecture(Architecture::X86);
break;
const auto& architectureToken = result.NextCapture(CAPTURE_ARCHITECTURE);
case TAG_X64:
state->SetArchitecture(Architecture::X64);
break;
const auto foundArchitecture = m_architecture_mapping.find(architectureToken.IdentifierValue());
default:
throw ParsingException(TokenPos(), "Unknown architecture!");
}
if(foundArchitecture == m_architecture_mapping.end())
throw ParsingException(architectureToken.GetPos(), "Unknown architecture");
state->SetArchitecture(foundArchitecture->second);
}

View File

@ -4,8 +4,9 @@
class SequenceArchitecture final : public CommandsParser::sequence_t
{
static constexpr auto TAG_X86 = 1;
static constexpr auto TAG_X64 = 2;
static constexpr auto CAPTURE_ARCHITECTURE = 1;
std::unordered_map<std::string, Architecture> m_architecture_mapping;
protected:
void ProcessMatch(CommandsParserState* state, SequenceResult<CommandsParserValue>& result) const override;

View File

@ -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);
}
}