Add Tests for ZCG cpp

This commit is contained in:
Jan
2021-02-10 18:03:50 +01:00
parent 31497d804c
commit f9ef7cc35b
102 changed files with 502 additions and 21 deletions

View File

@ -0,0 +1,51 @@
ZoneCodeGeneratorLibTests = {}
function ZoneCodeGeneratorLibTests:include()
if References:include(self:name()) then
includedirs {
path.join(TestFolder(), "ZoneCodeGeneratorLibTests")
}
end
end
function ZoneCodeGeneratorLibTests:link()
if References:link(self:name()) then
links(self:name())
end
end
function ZoneCodeGeneratorLibTests:use()
end
function ZoneCodeGeneratorLibTests:name()
return "ZoneCodeGeneratorLibTests"
end
function ZoneCodeGeneratorLibTests:project()
References:reset()
local folder = TestFolder();
project(self:name())
targetdir(TargetDirectoryTest)
location "%{wks.location}/test/%{prj.name}"
kind "ConsoleApp"
language "C++"
files {
path.join(folder, "ZoneCodeGeneratorLibTests/**.h"),
path.join(folder, "ZoneCodeGeneratorLibTests/**.cpp")
}
vpaths {
["*"] = {
path.join(folder, "ZoneCodeGeneratorLibTests"),
}
}
self:include()
ZoneCodeGeneratorLib:include()
catch2:include()
ZoneCodeGeneratorLib:link()
end

View File

@ -0,0 +1,227 @@
#include <catch2/catch.hpp>
#include "Parsing/Impl/CommentRemovingStreamProxy.h"
#include "Parsing/Mock/MockParserLineStream.h"
namespace test::parsing
{
TEST_CASE("CommentRemovingStreamProxy: Ensure simple single line comment is working", "[parsing][parsingstream]")
{
const std::vector<std::string> lines
{
"// hello",
"prefix // test",
"t//est"
};
MockParserLineStream mockStream(lines);
CommentRemovingStreamProxy proxy(&mockStream);
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 1);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 2);
REQUIRE(line.m_line == "prefix ");
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 3);
REQUIRE(line.m_line == "t");
}
REQUIRE(proxy.Eof());
}
TEST_CASE("CommentRemovingStreamProxy: Ensure single line comment expands to next line on backslash", "[parsing][parsingstream]")
{
const std::vector<std::string> lines
{
"// hello\\",
"this should still be a comment",
"this should not be a comment anymore"
};
MockParserLineStream mockStream(lines);
CommentRemovingStreamProxy proxy(&mockStream);
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 1);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 2);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 3);
REQUIRE(line.m_line == "this should not be a comment anymore");
}
REQUIRE(proxy.Eof());
}
TEST_CASE("CommentRemovingStreamProxy: Ensure single line comment expands to next line on backslash and is repeatable", "[parsing][parsingstream]")
{
const std::vector<std::string> lines
{
"// hello\\",
"this should still be a comment \\",
"this as well",
"this not anymore"
};
MockParserLineStream mockStream(lines);
CommentRemovingStreamProxy proxy(&mockStream);
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 1);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 2);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 3);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 4);
REQUIRE(line.m_line == "this not anymore");
}
REQUIRE(proxy.Eof());
}
TEST_CASE("CommentRemovingStreamProxy: Ensure backslash must be last character to expand single line comment to next line", "[parsing][parsingstream]")
{
const std::vector<std::string> lines
{
"// hello\\",
"this should still be a comment \\ ",
"this not anymore"
};
MockParserLineStream mockStream(lines);
CommentRemovingStreamProxy proxy(&mockStream);
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 1);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 2);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 3);
REQUIRE(line.m_line == "this not anymore");
}
REQUIRE(proxy.Eof());
}
TEST_CASE("CommentRemovingStreamProxy: Ensure simple multiline comment works on one line", "[parsing][parsingstream]")
{
const std::vector<std::string> lines
{
"hello/* hell*/ world",
"/*this should be a comment*/",
"Hello /*asdf*/",
"/*asdf*/World"
};
MockParserLineStream mockStream(lines);
CommentRemovingStreamProxy proxy(&mockStream);
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 1);
REQUIRE(line.m_line == "hello world");
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 2);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 3);
REQUIRE(line.m_line == "Hello ");
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 4);
REQUIRE(line.m_line == "World");
}
REQUIRE(proxy.Eof());
}
TEST_CASE("CommentRemovingStreamProxy: Ensure simple multiline comment works over multiple lines", "[parsing][parsingstream]")
{
const std::vector<std::string> lines
{
"hello/* hell",
" hell*/ world/*nope",
"notatall",
"hehe*/xd"
};
MockParserLineStream mockStream(lines);
CommentRemovingStreamProxy proxy(&mockStream);
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 1);
REQUIRE(line.m_line == "hello");
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 2);
REQUIRE(line.m_line == " world");
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 3);
REQUIRE(line.m_line.empty());
}
{
auto line = proxy.NextLine();
REQUIRE(line.m_line_number == 4);
REQUIRE(line.m_line == "xd");
}
REQUIRE(proxy.Eof());
}
}

View File

@ -0,0 +1,36 @@
#include "MockParserLineStream.h"
const std::string MockParserLineStream::MOCK_FILENAME;
MockParserLineStream::MockParserLineStream(const std::vector<std::string>& lines)
: m_lines(lines),
m_line(0)
{
}
ParserLine MockParserLineStream::NextLine()
{
if(m_line < m_lines.size())
{
const auto line = m_line++;
return ParserLine(MOCK_FILENAME, line + 1, m_lines[line]);
}
return ParserLine(MOCK_FILENAME, 0, std::string());
}
bool MockParserLineStream::IncludeFile(const std::string& filename)
{
m_includes.push_back(filename);
return true;
}
bool MockParserLineStream::IsOpen() const
{
return true;
}
bool MockParserLineStream::Eof() const
{
return m_line >= m_lines.size();
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <vector>
#include "Parsing/IParserLineStream.h"
class MockParserLineStream final : public IParserLineStream
{
static const std::string MOCK_FILENAME;
const std::vector<std::string>& m_lines;
unsigned m_line;
std::vector<std::string> m_includes{};
public:
explicit MockParserLineStream(const std::vector<std::string>& lines);
ParserLine NextLine() override;
bool IncludeFile(const std::string& filename) override;
_NODISCARD bool IsOpen() const override;
_NODISCARD bool Eof() const override;
};

View File

@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>