Kaynağa Gözat

Allow unit tests to expect warnings and errors logged by RmlUi

Michael Ragazzon 4 yıl önce
ebeveyn
işleme
52edfde013

+ 36 - 3
Tests/Source/Common/TestsInterface.cpp

@@ -27,17 +27,50 @@
  */
 
 #include "TestsInterface.h"
+#include <RmlUi/Core/StringUtilities.h>
 #include <doctest.h>
 
-
 bool TestsSystemInterface::LogMessage(Rml::Log::Type type, const Rml::String& message)
 {
 	static const char* message_type_str[Rml::Log::Type::LT_MAX] = { "Always", "Error", "Assert", "Warning", "Info", "Debug" };
-	bool result = Rml::SystemInterface::LogMessage(type, message);
-	CHECK_MESSAGE(type >= Rml::Log::Type::LT_INFO, "RmlUi " << message_type_str[type] << ": " << message);
+	const bool result = Rml::SystemInterface::LogMessage(type, message);
+
+	if (type <= Rml::Log::Type::LT_WARNING)
+	{
+		const Rml::String warning = "RmlUi " + Rml::String(message_type_str[type]) + ": " + message;
+
+		if (num_expected_warnings > 0)
+		{
+			num_logged_warnings += 1;
+			warnings.push_back(warning);
+		}
+		else
+		{
+			FAIL_CHECK(warning);
+		}
+	}
+
 	return result;
 }
 
+void TestsSystemInterface::SetNumExpectedWarnings(int in_num_expected_warnings)
+{
+	if (num_expected_warnings > 0)
+	{
+		// Check and clear previous warnings
+		if (num_logged_warnings != num_expected_warnings)
+		{
+			Rml::String str = "Got unexpected number of warnings: \n";
+			Rml::StringUtilities::JoinString(str, warnings, '\n');
+			CHECK_MESSAGE(num_logged_warnings == num_expected_warnings, str);
+		}
+
+		num_expected_warnings = 0;
+		num_logged_warnings = 0;
+		warnings.clear();
+	}
+	num_expected_warnings = in_num_expected_warnings;
+}
 
 void TestsRenderInterface::RenderGeometry(Rml::Vertex* /*vertices*/, int /*num_vertices*/, int* /*indices*/, int /*num_indices*/, const Rml::TextureHandle /*texture*/, const Rml::Vector2f& /*translation*/)
 {

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

@@ -37,6 +37,16 @@ class TestsSystemInterface : public ShellSystemInterface
 {
 public:
 	bool LogMessage(Rml::Log::Type type, const Rml::String& message) override;
+
+	// Checks and clears previously logged messages, then sets the number of expected
+	// warnings and errors until the next call.
+	void SetNumExpectedWarnings(int num_expected_warnings);
+
+private:
+	int num_logged_warnings = 0;
+	int num_expected_warnings = 0;
+
+	Rml::StringList warnings;
 };
 
 

+ 7 - 0
Tests/Source/Common/TestsShell.cpp

@@ -154,6 +154,8 @@ void TestsShell::ShutdownShell()
 		RMLUI_ASSERTMSG(shell_context->GetNumDocuments() == num_documents_begin, "Make sure all previously opened documents have been closed.");
 		(void)num_documents_begin;
 
+		tests_system_interface.SetNumExpectedWarnings(0);
+
 		Rml::Shutdown();
 
 #ifdef RMLUI_TESTS_USE_SHELL
@@ -166,6 +168,11 @@ void TestsShell::ShutdownShell()
 	}
 }
 
+void TestsShell::SetNumExpectedWarnings(int num_warnings)
+{
+	tests_system_interface.SetNumExpectedWarnings(num_warnings);
+}
+
 Rml::String TestsShell::GetRenderStats()
 {
 	Rml::String result;

+ 4 - 0
Tests/Source/Common/TestsShell.h

@@ -47,6 +47,10 @@ namespace TestsShell {
 
 	void ShutdownShell();
 
+	// Set the number of expected warnings and errors logged by RmlUi until the next call to this function
+	// or until 'ShutdownShell()'.
+	void SetNumExpectedWarnings(int num_warnings);
+
 	// Stats only available for the dummy renderer.
 	Rml::String GetRenderStats();
 }