diff --git a/src/ZoneCodeGeneratorLib/Generating/CodeGenerator.cpp b/src/ZoneCodeGeneratorLib/Generating/CodeGenerator.cpp index 4dfbedb5..c5571b33 100644 --- a/src/ZoneCodeGeneratorLib/Generating/CodeGenerator.cpp +++ b/src/ZoneCodeGeneratorLib/Generating/CodeGenerator.cpp @@ -43,7 +43,7 @@ bool CodeGenerator::GenerateCodeForTemplate(RenderingContext* context, ICodeTemp return false; } - codeTemplate->RenderFile(stream, codeFile.m_tag); + codeTemplate->RenderFile(stream, codeFile.m_tag, context); stream.close(); } diff --git a/src/ZoneCodeGeneratorLib/Generating/ICodeTemplate.h b/src/ZoneCodeGeneratorLib/Generating/ICodeTemplate.h index 0059843b..153b5c97 100644 --- a/src/ZoneCodeGeneratorLib/Generating/ICodeTemplate.h +++ b/src/ZoneCodeGeneratorLib/Generating/ICodeTemplate.h @@ -30,5 +30,5 @@ public: ICodeTemplate& operator=(ICodeTemplate&& other) noexcept = default; virtual std::vector GetFilesToRender(RenderingContext* context) = 0; - virtual void RenderFile(std::ostream& stream, int fileTag) = 0; + virtual void RenderFile(std::ostream& stream, int fileTag, RenderingContext* context) = 0; }; diff --git a/src/ZoneCodeGeneratorLib/Generating/Templates/AssetStructTestsTemplate.cpp b/src/ZoneCodeGeneratorLib/Generating/Templates/AssetStructTestsTemplate.cpp index d1e456c4..32bfb09e 100644 --- a/src/ZoneCodeGeneratorLib/Generating/Templates/AssetStructTestsTemplate.cpp +++ b/src/ZoneCodeGeneratorLib/Generating/Templates/AssetStructTestsTemplate.cpp @@ -1,7 +1,85 @@ #include "AssetStructTestsTemplate.h" +#include #include +#include "Domain/Computations/StructureComputations.h" + +#define LINE(x) DoIntendation(); m_out << x << "\n" + +class AssetStructTestsTemplate::Internal +{ + static constexpr const char* INTENDATION = " "; + + std::ostream& m_out; + RenderingContext& m_env; + unsigned m_intendation; + + void DoIntendation() const + { + for (auto i = 0u; i < m_intendation; i++) + m_out << INTENDATION; + } + + void TestMethod(StructureInformation* structure) + { + if (structure->m_non_embedded_reference_exists) + { + LINE("TEST_METHOD(Test_"<m_definition->m_name<<")"); + LINE("{"); + m_intendation++; + LINE("Assert::AreEqual("<m_definition->GetSize()<<"u, sizeof("<m_definition->GetFullName()<<"));"); + LINE("Assert::AreEqual("<m_definition->GetAlignment()<<"u, alignof("<m_definition->GetFullName()<<"));"); + m_intendation--; + LINE("}"); + } + } + +public: + Internal(std::ostream& stream, RenderingContext* context) + : m_out(stream), + m_env(*context), + m_intendation(0u) + { + } + + void Source() + { + LINE("// ===================================================================="); + LINE("// This file has been generated by ZoneCodeGenerator."); + LINE("// Do not modify."); + LINE("// Any changes will be discarded when regenerating."); + LINE("// ===================================================================="); + LINE(""); + LINE("#include \"CppUnitTest.h\""); + LINE("#include \"Game/" << m_env.m_game << "/" << m_env.m_game << ".h\""); + LINE(""); + LINE("using namespace Microsoft::VisualStudio::CppUnitTestFramework;"); + LINE("using namespace " << m_env.m_game << ";"); + LINE(""); + LINE("namespace ZoneCommonTests"); + LINE("{"); + m_intendation++; + LINE("TEST_CLASS(AssetStructTest_"<m_definition->m_name<<")"); + LINE("{"); + LINE("public:"); + m_intendation++; + + TestMethod(m_env.m_asset); + for (auto* structure : m_env.m_used_structures) + { + StructureComputations computations(structure->m_info); + if (!structure->m_info->m_definition->m_anonymous && !computations.IsAsset()) + TestMethod(structure->m_info); + } + + m_intendation--; + LINE("};"); + m_intendation--; + LINE("}"); + } +}; + std::vector AssetStructTestsTemplate::GetFilesToRender(RenderingContext* context) { std::vector files; @@ -19,6 +97,12 @@ std::vector AssetStructTestsTemplate::GetFilesToRender(Renderi return files; } -void AssetStructTestsTemplate::RenderFile(std::ostream& stream, int fileTag) +void AssetStructTestsTemplate::RenderFile(std::ostream& stream, const int fileTag, RenderingContext* context) { + Internal internal(stream, context); + + if (fileTag == TAG_SOURCE) + internal.Source(); + else + std::cout << "Invalid tag in AssetStructTestsTemplate\n"; } diff --git a/src/ZoneCodeGeneratorLib/Generating/Templates/AssetStructTestsTemplate.h b/src/ZoneCodeGeneratorLib/Generating/Templates/AssetStructTestsTemplate.h index f371f0cd..d5d9ba84 100644 --- a/src/ZoneCodeGeneratorLib/Generating/Templates/AssetStructTestsTemplate.h +++ b/src/ZoneCodeGeneratorLib/Generating/Templates/AssetStructTestsTemplate.h @@ -5,7 +5,9 @@ class AssetStructTestsTemplate final : public ICodeTemplate { static constexpr int TAG_SOURCE = 1; + class Internal; + public: std::vector GetFilesToRender(RenderingContext* context) override; - void RenderFile(std::ostream& stream, int fileTag) override; + void RenderFile(std::ostream& stream, int fileTag, RenderingContext* context) override; }; diff --git a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp index d5c885c3..9ff88c6c 100644 --- a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp +++ b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.cpp @@ -25,7 +25,7 @@ std::vector ZoneLoadTemplate::GetFilesToRender(RenderingContex return files; } -void ZoneLoadTemplate::RenderFile(std::ostream& stream, const int fileTag) +void ZoneLoadTemplate::RenderFile(std::ostream& stream, const int fileTag, RenderingContext* context) { if (fileTag == TAG_HEADER) { diff --git a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.h b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.h index aebf2adb..5e9cafa9 100644 --- a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.h +++ b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneLoadTemplate.h @@ -8,5 +8,5 @@ class ZoneLoadTemplate final : public ICodeTemplate public: std::vector GetFilesToRender(RenderingContext* context) override; - void RenderFile(std::ostream& stream, int fileTag) override; + void RenderFile(std::ostream& stream, int fileTag, RenderingContext* context) override; }; diff --git a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneWriteTemplate.cpp b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneWriteTemplate.cpp index dad3e266..1c428493 100644 --- a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneWriteTemplate.cpp +++ b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneWriteTemplate.cpp @@ -25,6 +25,6 @@ std::vector ZoneWriteTemplate::GetFilesToRender(RenderingConte return files; } -void ZoneWriteTemplate::RenderFile(std::ostream& stream, const int fileTag) +void ZoneWriteTemplate::RenderFile(std::ostream& stream, const int fileTag, RenderingContext* context) { } diff --git a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneWriteTemplate.h b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneWriteTemplate.h index 4888f216..0aee63f4 100644 --- a/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneWriteTemplate.h +++ b/src/ZoneCodeGeneratorLib/Generating/Templates/ZoneWriteTemplate.h @@ -8,5 +8,5 @@ class ZoneWriteTemplate final : public ICodeTemplate public: std::vector GetFilesToRender(RenderingContext* context) override; - void RenderFile(std::ostream& stream, int fileTag) override; + void RenderFile(std::ostream& stream, int fileTag, RenderingContext* context) override; };