mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 14:58:10 -05:00
Add base for menu parsing unit tests
This commit is contained in:
65
test/ParserTestUtils/Parsing/Mock/MockLexer.h
Normal file
65
test/ParserTestUtils/Parsing/Mock/MockLexer.h
Normal file
@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "Parsing/ILexer.h"
|
||||
|
||||
template <typename TokenType>
|
||||
class MockLexer final : public ILexer<TokenType>
|
||||
{
|
||||
// TokenType must inherit IParserValue
|
||||
static_assert(std::is_base_of<IParserValue, TokenType>::value);
|
||||
|
||||
std::vector<TokenType> m_tokens;
|
||||
TokenType m_eof;
|
||||
int m_pop_count;
|
||||
|
||||
public:
|
||||
MockLexer(std::initializer_list<Movable<TokenType>> tokens, TokenType eof)
|
||||
: m_tokens(std::make_move_iterator(tokens.begin()), std::make_move_iterator(tokens.end())),
|
||||
m_eof(std::move(eof)),
|
||||
m_pop_count(0)
|
||||
{
|
||||
}
|
||||
|
||||
~MockLexer() override = default;
|
||||
MockLexer(const MockLexer& other) = delete;
|
||||
MockLexer(MockLexer&& other) noexcept = default;
|
||||
MockLexer& operator=(const MockLexer& other) = delete;
|
||||
MockLexer& operator=(MockLexer&& other) noexcept = default;
|
||||
|
||||
const TokenType& GetToken(const unsigned index) override
|
||||
{
|
||||
if (index < m_tokens.size())
|
||||
return m_tokens[index];
|
||||
|
||||
return m_eof;
|
||||
}
|
||||
|
||||
void PopTokens(const int amount) override
|
||||
{
|
||||
m_pop_count += amount;
|
||||
}
|
||||
|
||||
bool IsEof() override
|
||||
{
|
||||
return !m_tokens.empty();
|
||||
}
|
||||
|
||||
const TokenPos& GetPos() override
|
||||
{
|
||||
return !m_tokens.empty() ? m_tokens[0].GetPos() : m_eof.GetPos();
|
||||
}
|
||||
|
||||
_NODISCARD int GetPopCount() const
|
||||
{
|
||||
return m_pop_count;
|
||||
}
|
||||
|
||||
_NODISCARD ParserLine GetLineForPos(const TokenPos& pos) const override
|
||||
{
|
||||
return ParserLine();
|
||||
}
|
||||
};
|
@ -0,0 +1,8 @@
|
||||
#include "MockPackValueSupplier.h"
|
||||
|
||||
#include "Parsing/Impl/PackDefinitionStreamProxy.h"
|
||||
|
||||
int MockPackValueSupplier::GetCurrentPack() const
|
||||
{
|
||||
return PackDefinitionStreamProxy::DEFAULT_PACK;
|
||||
}
|
10
test/ParserTestUtils/Parsing/Mock/MockPackValueSupplier.h
Normal file
10
test/ParserTestUtils/Parsing/Mock/MockPackValueSupplier.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "Parsing/IPackValueSupplier.h"
|
||||
|
||||
class MockPackValueSupplier final : public IPackValueSupplier
|
||||
{
|
||||
public:
|
||||
_NODISCARD int GetCurrentPack() const override;
|
||||
};
|
70
test/ParserTestUtils/Parsing/Mock/MockParserLineStream.cpp
Normal file
70
test/ParserTestUtils/Parsing/Mock/MockParserLineStream.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
#include "MockParserLineStream.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
const std::string MockParserLineStream::MOCK_FILENAME = "Mockfile";
|
||||
|
||||
MockParserLineStream::MockParserLineStream(const std::vector<std::string>& lines)
|
||||
{
|
||||
AddIncludeLines(MOCK_FILENAME, lines);
|
||||
m_include_positions.emplace_back(MOCK_FILENAME, lines);
|
||||
}
|
||||
|
||||
MockParserLineStream::IncludePos::IncludePos(std::string filename, const std::vector<std::string>& lines)
|
||||
: m_filename(std::move(filename)),
|
||||
m_lines(lines),
|
||||
m_pos(0)
|
||||
{
|
||||
}
|
||||
|
||||
void MockParserLineStream::AddIncludeLines(const std::string& filename, const std::vector<std::string>& lines)
|
||||
{
|
||||
m_include_lines[filename] = lines;
|
||||
}
|
||||
|
||||
ParserLine MockParserLineStream::NextLine()
|
||||
{
|
||||
while (!m_include_positions.empty())
|
||||
{
|
||||
auto& currentInclude = m_include_positions.back();
|
||||
|
||||
if (currentInclude.m_pos < currentInclude.m_lines.size())
|
||||
{
|
||||
const auto line = currentInclude.m_pos++;
|
||||
return ParserLine(currentInclude.m_filename, static_cast<int>(line + 1), currentInclude.m_lines[line]);
|
||||
}
|
||||
|
||||
m_include_positions.pop_back();
|
||||
}
|
||||
|
||||
return ParserLine();
|
||||
}
|
||||
|
||||
bool MockParserLineStream::IncludeFile(const std::string& filename)
|
||||
{
|
||||
const auto foundInclude = m_include_lines.find(filename);
|
||||
if (foundInclude == m_include_lines.end())
|
||||
return false;
|
||||
|
||||
m_include_positions.emplace_back(foundInclude->first, foundInclude->second);
|
||||
return true;
|
||||
}
|
||||
|
||||
void MockParserLineStream::PopCurrentFile()
|
||||
{
|
||||
if (!m_include_positions.empty())
|
||||
m_include_positions.pop_back();
|
||||
}
|
||||
|
||||
bool MockParserLineStream::IsOpen() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MockParserLineStream::Eof() const
|
||||
{
|
||||
return !std::any_of(m_include_positions.begin(), m_include_positions.end(), [](const IncludePos& pos)
|
||||
{
|
||||
return pos.m_pos < pos.m_lines.size();
|
||||
});
|
||||
}
|
36
test/ParserTestUtils/Parsing/Mock/MockParserLineStream.h
Normal file
36
test/ParserTestUtils/Parsing/Mock/MockParserLineStream.h
Normal file
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Parsing/IParserLineStream.h"
|
||||
|
||||
class MockParserLineStream final : public IParserLineStream
|
||||
{
|
||||
public:
|
||||
static const std::string MOCK_FILENAME;
|
||||
|
||||
private:
|
||||
class IncludePos
|
||||
{
|
||||
public:
|
||||
std::string m_filename;
|
||||
const std::vector<std::string>& m_lines;
|
||||
unsigned m_pos;
|
||||
|
||||
IncludePos(std::string filename, const std::vector<std::string>& lines);
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, std::vector<std::string>> m_include_lines;
|
||||
std::vector<IncludePos> m_include_positions;
|
||||
|
||||
public:
|
||||
explicit MockParserLineStream(const std::vector<std::string>& lines);
|
||||
|
||||
void AddIncludeLines(const std::string& filename, const std::vector<std::string>& lines);
|
||||
ParserLine NextLine() override;
|
||||
bool IncludeFile(const std::string& filename) override;
|
||||
void PopCurrentFile() override;
|
||||
_NODISCARD bool IsOpen() const override;
|
||||
_NODISCARD bool Eof() const override;
|
||||
};
|
49
test/ParserTestUtils/Parsing/Mock/MockSequence.h
Normal file
49
test/ParserTestUtils/Parsing/Mock/MockSequence.h
Normal file
@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include "Parsing/Sequence/AbstractSequence.h"
|
||||
|
||||
struct MockSequenceState
|
||||
{
|
||||
char m_dummy;
|
||||
};
|
||||
|
||||
template<typename TokenType>
|
||||
class MockSequence final : public AbstractSequence<TokenType, MockSequenceState>
|
||||
{
|
||||
public:
|
||||
typedef AbstractSequence<TokenType, MockSequenceState> parent_t;
|
||||
|
||||
private:
|
||||
using parent_t::AddMatchers;
|
||||
using parent_t::AddLabeledMatchers;
|
||||
|
||||
std::function<void(SequenceResult<TokenType>&)> m_handler;
|
||||
|
||||
protected:
|
||||
void ProcessMatch(MockSequenceState* state, SequenceResult<TokenType>& result) const override
|
||||
{
|
||||
if (m_handler)
|
||||
m_handler(result);
|
||||
}
|
||||
|
||||
public:
|
||||
void AddMockMatchers(std::initializer_list<Movable<std::unique_ptr<typename parent_t::matcher_t>>> matchers)
|
||||
{
|
||||
AddMatchers(matchers);
|
||||
}
|
||||
|
||||
void AddMockLabeledMatchers(std::initializer_list<Movable<std::unique_ptr<typename parent_t::matcher_t>>> matchers, const int label)
|
||||
{
|
||||
AddLabeledMatchers(matchers, label);
|
||||
}
|
||||
|
||||
void Handle(std::function<void(SequenceResult<TokenType>&)> handler)
|
||||
{
|
||||
m_handler = std::move(handler);
|
||||
}
|
||||
|
||||
IMatcherForLabelSupplier<TokenType>* GetLabelSupplier()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user