mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
Add unit tests for IncludingStreamProxy
This commit is contained in:
parent
e99e66cec5
commit
8b8f1d4f2a
@ -37,6 +37,13 @@ bool IncludingStreamProxy::ExtractIncludeFilename(const ParserLine& line, const
|
||||
{
|
||||
auto currentPos = includeDirectivePosition;
|
||||
bool isDoubleQuotes;
|
||||
|
||||
while(isspace(line.m_line[currentPos]))
|
||||
{
|
||||
if(currentPos++ >= line.m_line.size())
|
||||
throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR);
|
||||
}
|
||||
|
||||
if (line.m_line[currentPos] == '"')
|
||||
isDoubleQuotes = true;
|
||||
else if (line.m_line[currentPos] == '<')
|
||||
|
@ -0,0 +1,136 @@
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include "Parsing/Impl/IncludingStreamProxy.h"
|
||||
#include "Parsing/Mock/MockParserLineStream.h"
|
||||
|
||||
namespace test::parsing
|
||||
{
|
||||
TEST_CASE("IncludingStreamProxy: Ensure simple include is working", "[parsing][parsingstream]")
|
||||
{
|
||||
const std::vector<std::string> lines
|
||||
{
|
||||
"Hello world",
|
||||
"#include \"ASDF.txt\"",
|
||||
"and bye"
|
||||
};
|
||||
|
||||
const std::vector<std::string> asdf
|
||||
{
|
||||
"Hello galaxy"
|
||||
};
|
||||
|
||||
MockParserLineStream mockStream(lines);
|
||||
mockStream.AddIncludeLines("ASDF.txt", asdf);
|
||||
|
||||
IncludingStreamProxy proxy(&mockStream);
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 1);
|
||||
REQUIRE(line.m_filename == MockParserLineStream::MOCK_FILENAME);
|
||||
REQUIRE(line.m_line == "Hello world");
|
||||
}
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 1);
|
||||
REQUIRE(line.m_filename == "ASDF.txt");
|
||||
REQUIRE(line.m_line == "Hello galaxy");
|
||||
}
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 3);
|
||||
REQUIRE(line.m_filename == MockParserLineStream::MOCK_FILENAME);
|
||||
REQUIRE(line.m_line == "and bye");
|
||||
}
|
||||
|
||||
REQUIRE(proxy.Eof());
|
||||
}
|
||||
|
||||
TEST_CASE("IncludingStreamProxy: Ensure simple include with angle brackets is working", "[parsing][parsingstream]")
|
||||
{
|
||||
const std::vector<std::string> lines
|
||||
{
|
||||
"Hello world",
|
||||
"#include <ASDF.txt>",
|
||||
"and bye"
|
||||
};
|
||||
|
||||
const std::vector<std::string> asdf
|
||||
{
|
||||
"Hello galaxy"
|
||||
};
|
||||
|
||||
MockParserLineStream mockStream(lines);
|
||||
mockStream.AddIncludeLines("ASDF.txt", asdf);
|
||||
|
||||
IncludingStreamProxy proxy(&mockStream);
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 1);
|
||||
REQUIRE(line.m_filename == MockParserLineStream::MOCK_FILENAME);
|
||||
REQUIRE(line.m_line == "Hello world");
|
||||
}
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 1);
|
||||
REQUIRE(line.m_filename == "ASDF.txt");
|
||||
REQUIRE(line.m_line == "Hello galaxy");
|
||||
}
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 3);
|
||||
REQUIRE(line.m_filename == MockParserLineStream::MOCK_FILENAME);
|
||||
REQUIRE(line.m_line == "and bye");
|
||||
}
|
||||
|
||||
REQUIRE(proxy.Eof());
|
||||
}
|
||||
|
||||
TEST_CASE("IncludingStreamProxy: Ensure can have spaces before include directive", "[parsing][parsingstream]")
|
||||
{
|
||||
const std::vector<std::string> lines
|
||||
{
|
||||
"Hello world",
|
||||
" #include \"ASDF.txt\" ",
|
||||
"and bye"
|
||||
};
|
||||
|
||||
const std::vector<std::string> asdf
|
||||
{
|
||||
"Hello galaxy"
|
||||
};
|
||||
|
||||
MockParserLineStream mockStream(lines);
|
||||
mockStream.AddIncludeLines("ASDF.txt", asdf);
|
||||
|
||||
IncludingStreamProxy proxy(&mockStream);
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 1);
|
||||
REQUIRE(line.m_filename == MockParserLineStream::MOCK_FILENAME);
|
||||
REQUIRE(line.m_line == "Hello world");
|
||||
}
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 1);
|
||||
REQUIRE(line.m_filename == "ASDF.txt");
|
||||
REQUIRE(line.m_line == "Hello galaxy");
|
||||
}
|
||||
|
||||
{
|
||||
auto line = proxy.NextLine();
|
||||
REQUIRE(line.m_line_number == 3);
|
||||
REQUIRE(line.m_filename == MockParserLineStream::MOCK_FILENAME);
|
||||
REQUIRE(line.m_line == "and bye");
|
||||
}
|
||||
|
||||
REQUIRE(proxy.Eof());
|
||||
}
|
||||
}
|
@ -1,19 +1,40 @@
|
||||
#include "MockParserLineStream.h"
|
||||
|
||||
const std::string MockParserLineStream::MOCK_FILENAME;
|
||||
#include <algorithm>
|
||||
|
||||
const std::string MockParserLineStream::MOCK_FILENAME = "Mockfile";
|
||||
|
||||
MockParserLineStream::MockParserLineStream(const std::vector<std::string>& lines)
|
||||
: m_lines(lines),
|
||||
m_line(0)
|
||||
{
|
||||
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()
|
||||
{
|
||||
if(m_line < m_lines.size())
|
||||
while (!m_include_positions.empty())
|
||||
{
|
||||
const auto line = m_line++;
|
||||
return ParserLine(MOCK_FILENAME, line + 1, m_lines[line]);
|
||||
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(MOCK_FILENAME, 0, std::string());
|
||||
@ -21,7 +42,11 @@ ParserLine MockParserLineStream::NextLine()
|
||||
|
||||
bool MockParserLineStream::IncludeFile(const std::string& filename)
|
||||
{
|
||||
m_includes.push_back(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;
|
||||
}
|
||||
|
||||
@ -32,5 +57,8 @@ bool MockParserLineStream::IsOpen() const
|
||||
|
||||
bool MockParserLineStream::Eof() const
|
||||
{
|
||||
return m_line >= m_lines.size();
|
||||
return !std::any_of(m_include_positions.begin(), m_include_positions.end(), [](const IncludePos& pos)
|
||||
{
|
||||
return pos.m_pos < pos.m_lines.size();
|
||||
});
|
||||
}
|
||||
|
@ -1,20 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Parsing/IParserLineStream.h"
|
||||
|
||||
class MockParserLineStream final : public IParserLineStream
|
||||
{
|
||||
public:
|
||||
static const std::string MOCK_FILENAME;
|
||||
|
||||
const std::vector<std::string>& m_lines;
|
||||
unsigned m_line;
|
||||
std::vector<std::string> m_includes{};
|
||||
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;
|
||||
_NODISCARD bool IsOpen() const override;
|
||||
|
Loading…
x
Reference in New Issue
Block a user