mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 08:05:45 +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;
|
auto currentPos = includeDirectivePosition;
|
||||||
bool isDoubleQuotes;
|
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] == '"')
|
if (line.m_line[currentPos] == '"')
|
||||||
isDoubleQuotes = true;
|
isDoubleQuotes = true;
|
||||||
else if (line.m_line[currentPos] == '<')
|
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"
|
#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)
|
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()
|
ParserLine MockParserLineStream::NextLine()
|
||||||
{
|
{
|
||||||
if(m_line < m_lines.size())
|
while (!m_include_positions.empty())
|
||||||
{
|
{
|
||||||
const auto line = m_line++;
|
auto& currentInclude = m_include_positions.back();
|
||||||
return ParserLine(MOCK_FILENAME, line + 1, m_lines[line]);
|
|
||||||
|
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());
|
return ParserLine(MOCK_FILENAME, 0, std::string());
|
||||||
@ -21,7 +42,11 @@ ParserLine MockParserLineStream::NextLine()
|
|||||||
|
|
||||||
bool MockParserLineStream::IncludeFile(const std::string& filename)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,5 +57,8 @@ bool MockParserLineStream::IsOpen() const
|
|||||||
|
|
||||||
bool MockParserLineStream::Eof() 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
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "Parsing/IParserLineStream.h"
|
#include "Parsing/IParserLineStream.h"
|
||||||
|
|
||||||
class MockParserLineStream final : public IParserLineStream
|
class MockParserLineStream final : public IParserLineStream
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
static const std::string MOCK_FILENAME;
|
static const std::string MOCK_FILENAME;
|
||||||
|
|
||||||
|
private:
|
||||||
|
class IncludePos
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string m_filename;
|
||||||
const std::vector<std::string>& m_lines;
|
const std::vector<std::string>& m_lines;
|
||||||
unsigned m_line;
|
unsigned m_pos;
|
||||||
std::vector<std::string> m_includes{};
|
|
||||||
|
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:
|
public:
|
||||||
explicit MockParserLineStream(const std::vector<std::string>& lines);
|
explicit MockParserLineStream(const std::vector<std::string>& lines);
|
||||||
|
|
||||||
|
void AddIncludeLines(const std::string& filename, const std::vector<std::string>& lines);
|
||||||
ParserLine NextLine() override;
|
ParserLine NextLine() override;
|
||||||
bool IncludeFile(const std::string& filename) override;
|
bool IncludeFile(const std::string& filename) override;
|
||||||
_NODISCARD bool IsOpen() const override;
|
_NODISCARD bool IsOpen() const override;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user