Browse Source

Unit tests: Allow adding expected geometry to the test render interface

Michael Ragazzon 1 year ago
parent
commit
727155c997

+ 43 - 1
Tests/Source/Common/TestsInterface.cpp

@@ -27,6 +27,7 @@
  */
 
 #include "TestsInterface.h"
+#include "TypesToString.h"
 #include <RmlUi/Core/Log.h>
 #include <RmlUi/Core/StringUtilities.h>
 #include <doctest.h>
@@ -90,9 +91,36 @@ void TestsSystemInterface::SetTime(double t)
 	elapsed_time = t;
 }
 
-Rml::CompiledGeometryHandle TestsRenderInterface::CompileGeometry(Rml::Span<const Rml::Vertex> /*vertices*/, Rml::Span<const int> /*indices*/)
+Rml::CompiledGeometryHandle TestsRenderInterface::CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices)
 {
 	counters.compile_geometry += 1;
+
+	if (meshes_set)
+	{
+		INFO("Got vertices:\n", vertices);
+		INFO("Got indices:\n", indices);
+		REQUIRE_MESSAGE(!meshes.empty(), "No CompileGeometry expected, but one was passed to us");
+
+		Rml::Mesh mesh = std::move(meshes.front());
+		meshes.erase(meshes.begin());
+		INFO("Expected mesh:\n", mesh);
+
+		CHECK(mesh.vertices.size() == vertices.size());
+		CHECK(mesh.indices.size() == indices.size());
+
+		for (size_t i = 0; i < mesh.vertices.size(); i++)
+		{
+			CHECK(mesh.vertices[i].position == vertices[i].position);
+			CHECK(mesh.vertices[i].colour == vertices[i].colour);
+			CHECK(mesh.vertices[i].tex_coord == vertices[i].tex_coord);
+		}
+
+		for (size_t i = 0; i < mesh.indices.size(); i++)
+		{
+			CHECK(mesh.indices[i] == indices[i]);
+		}
+	}
+
 	return Rml::CompiledGeometryHandle(counters.compile_geometry);
 }
 
@@ -182,7 +210,21 @@ void TestsRenderInterface::ResetCounters()
 {
 	counters_from_previous_reset = std::exchange(counters, Counters());
 }
+
+void TestsRenderInterface::ExpectCompileGeometry(Rml::Vector<Rml::Mesh> in_meshes)
+{
+	VerifyMeshes();
+	meshes = std::move(in_meshes);
+	meshes_set = true;
+}
+
 void TestsRenderInterface::Reset()
 {
+	VerifyMeshes();
+	meshes_set = false;
 	ResetCounters();
 }
+void TestsRenderInterface::VerifyMeshes()
+{
+	REQUIRE_MESSAGE(meshes.empty(), "CompileGeometry: Expected meshes not passed to us");
+}

+ 5 - 0
Tests/Source/Common/TestsInterface.h

@@ -29,6 +29,7 @@
 #ifndef RMLUI_TESTS_TESTSINTERFACE_H
 #define RMLUI_TESTS_TESTSINTERFACE_H
 
+#include <RmlUi/Core/Mesh.h>
 #include <RmlUi/Core/RenderInterface.h>
 #include <RmlUi/Core/SystemInterface.h>
 #include <Shell.h>
@@ -105,6 +106,8 @@ public:
 	void ResetCounters();
 	const Counters& GetCountersFromPreviousReset() const { return counters_from_previous_reset; }
 
+	void ExpectCompileGeometry(Rml::Vector<Rml::Mesh> meshes);
+
 	void Reset();
 
 private:
@@ -112,6 +115,8 @@ private:
 
 	Counters counters = {};
 	Counters counters_from_previous_reset = {};
+	Rml::Vector<Rml::Mesh> meshes;
+	bool meshes_set = false;
 };
 
 #endif

+ 38 - 0
Tests/Source/Common/TypesToString.h

@@ -44,6 +44,11 @@ inline std::ostream& operator<<(std::ostream& os, const Colourb& value)
 	os << ToString(value);
 	return os;
 }
+inline std::ostream& operator<<(std::ostream& os, const ColourbPremultiplied& value)
+{
+	os << (int)value.red << ", " << (int)value.green << ", " << (int)value.blue << ", " << (int)value.alpha;
+	return os;
+}
 inline std::ostream& operator<<(std::ostream& os, const Vector2f& value)
 {
 	os << ToString(value);
@@ -55,6 +60,39 @@ inline std::ostream& operator<<(std::ostream& os, const Vector2i& value)
 	return os;
 }
 
+inline std::ostream& operator<<(std::ostream& os, Span<const Vertex> vertices)
+{
+	for (const Vertex& vertex : vertices)
+	{
+		os << "{{" << vertex.position << "}, {" << vertex.colour << "}, {" << vertex.tex_coord << "}}," << std::endl;
+	}
+	return os;
+}
+inline std::ostream& operator<<(std::ostream& os, const Vector<Vertex>& vertices)
+{
+	os << Span<const Vertex>(vertices);
+	return os;
+}
+
+inline std::ostream& operator<<(std::ostream& os, Span<const int> indices)
+{
+	for (int index : indices)
+		os << index << ", ";
+	return os;
+}
+inline std::ostream& operator<<(std::ostream& os, const Vector<int>& indices)
+{
+	os << Span<const int>(indices);
+	return os;
+}
+
+inline std::ostream& operator<<(std::ostream& os, const Mesh& value)
+{
+	os << "vertices: {\n" << value.vertices << "}\n";
+	os << "indices: {\n" << value.indices << "\n}\n";
+	return os;
+}
+
 } // namespace Rml
 
 #endif