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

@ -106,6 +106,7 @@ workspace "OpenAssetTools"
-- ========================
-- ThirdParty
-- ========================
include "thirdparty/catch2.lua"
include "thirdparty/libtomcrypt.lua"
include "thirdparty/libtommath.lua"
include "thirdparty/minilzo.lua"
@ -115,6 +116,7 @@ include "thirdparty/zlib.lua"
-- ThirdParty group: All projects that are external dependencies
group "ThirdParty"
catch2:project()
libtommath:project()
libtomcrypt:project()
minilzo:project()
@ -132,6 +134,7 @@ include "src/Unlinker.lua"
include "src/Utils.lua"
include "src/ZoneCode.lua"
include "src/ZoneCodeGenerator.lua"
include "src/ZoneCodeGeneratorLib.lua"
include "src/ZoneCodeGeneratorNew.lua"
include "src/ZoneCommon.lua"
include "src/ZoneLoading.lua"
@ -147,7 +150,7 @@ group "Components"
Utils:project()
ZoneCode:project()
ZoneCodeGenerator:project()
ZoneCodeGeneratorNew:project()
ZoneCodeGeneratorLib:project()
ZoneCommon:project()
ZoneLoading:project()
ZoneWriting:project()
@ -160,6 +163,7 @@ group ""
group "Tools"
Linker:project()
Unlinker:project()
ZoneCodeGeneratorNew:project()
group ""
-- ========================
@ -167,11 +171,13 @@ group ""
-- ========================
include "test/ObjCommonTests.lua"
include "test/ZoneCodeGeneratorTests.lua"
include "test/ZoneCodeGeneratorLibTests.lua"
include "test/ZoneCommonTests.lua"
-- Tests group: Unit test and other tests projects
group "Tests"
ObjCommonTests:project()
ZoneCodeGeneratorTests:project()
ZoneCodeGeneratorLibTests:project()
ZoneCommonTests:project()
group ""

View File

@ -0,0 +1,43 @@
ZoneCodeGeneratorLib = {}
function ZoneCodeGeneratorLib:include()
if References:include(self:name()) then
includedirs {
path.join(ProjectFolder(), "ZoneCodeGeneratorLib")
}
end
Utils:include()
end
function ZoneCodeGeneratorLib:link()
if References:link(self:name()) then
links(self:name())
Utils:link()
end
end
function ZoneCodeGeneratorLib:use()
dependson(self:name())
end
function ZoneCodeGeneratorLib:name()
return "ZoneCodeGeneratorLib"
end
function ZoneCodeGeneratorLib:project()
References:reset()
local folder = ProjectFolder();
project(self:name())
targetdir(TargetDirectoryLib)
location "%{wks.location}/src/%{prj.name}"
kind "StaticLib"
language "C++"
files {
path.join(folder, "ZoneCodeGeneratorLib/**.h"),
path.join(folder, "ZoneCodeGeneratorLib/**.cpp")
}
self:include()
end

View File

@ -2,7 +2,7 @@
#include <iostream>
#include "Parsing/Impl/CommentRemovingStreamProxy.h"
#include "Parsing/Impl/IncludingStreamProxy.h"
#include "Parsing/Impl/ParserFilesystemStream.h"
@ -23,8 +23,14 @@ bool HeaderFileReader::ReadHeaderFile(IDataRepository* repository) const
return false;
}
IncludingStreamProxy includeProxy(&stream);
IParserLineStream* lineStream = &includeProxy;
IParserLineStream* lineStream = &stream;
IncludingStreamProxy includeProxy(lineStream);
lineStream = &includeProxy;
CommentRemovingStreamProxy commentProxy(lineStream);
lineStream = &commentProxy;
while(true)
{

View File

@ -0,0 +1,74 @@
#include "CommentRemovingStreamProxy.h"
CommentRemovingStreamProxy::CommentRemovingStreamProxy(IParserLineStream* stream)
: m_stream(stream),
m_inside_multi_line_comment(false),
m_next_line_is_comment(false)
{
}
ParserLine CommentRemovingStreamProxy::NextLine()
{
auto line = m_stream->NextLine();
if (m_next_line_is_comment)
{
m_next_line_is_comment = !line.m_line.empty() && line.m_line[line.m_line.size() - 1] == '\\';
return ParserLine(line.m_filename, line.m_line_number, std::string());
}
unsigned multiLineCommentStart = 0;
for (auto i = 0u; i < line.m_line.size(); i++)
{
const auto c = line.m_line[i];
if (m_inside_multi_line_comment)
{
if (c == '*' && i + 1 < line.m_line.size() && line.m_line[i + 1] == '/')
{
line.m_line.erase(multiLineCommentStart, i + 2 - multiLineCommentStart);
multiLineCommentStart = 0;
m_inside_multi_line_comment = false;
}
}
else
{
if(c == '/' && i + 1 < line.m_line.size())
{
const auto c1 = line.m_line[i + 1];
if (c1 == '*')
{
multiLineCommentStart = i;
m_inside_multi_line_comment = true;
}
else if(c1 == '/')
{
m_next_line_is_comment = line.m_line[line.m_line.size() - 1] == '\\';
line.m_line.erase(i);
return line;
}
}
}
}
if(m_inside_multi_line_comment)
line.m_line.erase(multiLineCommentStart);
return line;
}
bool CommentRemovingStreamProxy::IncludeFile(const std::string& filename)
{
return m_stream->IncludeFile(filename);
}
bool CommentRemovingStreamProxy::IsOpen() const
{
return m_stream->IsOpen();
}
bool CommentRemovingStreamProxy::Eof() const
{
return m_stream->Eof();
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "Parsing/IParserLineStream.h"
class CommentRemovingStreamProxy final : public IParserLineStream
{
IParserLineStream* const m_stream;
bool m_inside_multi_line_comment;
bool m_next_line_is_comment;
public:
explicit CommentRemovingStreamProxy(IParserLineStream* stream);
ParserLine NextLine() override;
bool IncludeFile(const std::string& filename) override;
_NODISCARD bool IsOpen() const override;
_NODISCARD bool Eof() const override;
};

View File

@ -1,6 +1,5 @@
#pragma once
#include "Utils/ClassUtils.h"
#include "Parsing/IParserLineStream.h"
class IncludingStreamProxy final : public IParserLineStream
@ -10,7 +9,7 @@ class IncludingStreamProxy final : public IParserLineStream
static constexpr int INCLUDE_DIRECTIVE_LENGTH = std::char_traits<char>::length(INCLUDE_DIRECTIVE);
static constexpr int INCLUDE_DIRECTIVE_MINIMUM_TOTAL_LENGTH = INCLUDE_DIRECTIVE_LENGTH + 1 + 2; // #=+1 ""=+2
IParserLineStream* m_stream;
IParserLineStream* const m_stream;
_NODISCARD static bool FindIncludeDirective(const ParserLine& line, unsigned& includeDirectivePosition);
_NODISCARD static bool ExtractIncludeFilename(const ParserLine& line, unsigned includeDirectivePosition, unsigned& filenameStartPosition, unsigned& filenameEndPosition);

View File

@ -6,10 +6,13 @@ function ZoneCodeGeneratorNew:include()
path.join(ProjectFolder(), "ZoneCodeGeneratorNew")
}
end
Utils:include()
end
function ZoneCodeGeneratorNew:link()
if References:link(self:name()) then
links(self:name())
end
end
function ZoneCodeGeneratorNew:use()
@ -25,7 +28,7 @@ function ZoneCodeGeneratorNew:project()
local folder = ProjectFolder();
project(self:name())
targetdir(TargetDirectoryLib)
targetdir(TargetDirectoryBin)
location "%{wks.location}/src/%{prj.name}"
kind "ConsoleApp"
language "C++"
@ -34,9 +37,15 @@ function ZoneCodeGeneratorNew:project()
path.join(folder, "ZoneCodeGeneratorNew/**.h"),
path.join(folder, "ZoneCodeGeneratorNew/**.cpp")
}
vpaths {
["*"] = {
path.join(folder, "ZoneCodeGeneratorNew"),
}
}
self:include()
Utils:include()
ZoneCodeGeneratorLib:include()
Utils:link()
ZoneCodeGeneratorLib:link()
end

View File

@ -1,12 +0,0 @@
#pragma once
#include "Parsing/IParserLineStream.h"
class CommentRemovingStreamProxy final : IParserLineStream
{
public:
std::string NextLine() override;
bool IncludeFile(const std::string& filename) override;
bool IsOpen() const override;
_NODISCARD bool Eof() const override;
};

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

Some files were not shown because too many files have changed in this diff Show More