Browse Source

Add unit tests for Element::Closest

Michael Ragazzon 4 years ago
parent
commit
63899af0c1
2 changed files with 43 additions and 6 deletions
  1. 41 5
      Tests/Source/UnitTests/Selectors.cpp
  2. 2 1
      changelog.md

+ 41 - 5
Tests/Source/UnitTests/Selectors.cpp

@@ -70,11 +70,11 @@ static const String doc_end = R"(
 </rml>
 )";
 
-struct Selector {
+struct QuerySelector {
 	String selector;
 	String expected_ids;
 };
-static const Vector<Selector> selectors =
+static const Vector<QuerySelector> query_selectors =
 {
 	{ "span",                        "Y D0 D1 F0" },
 	{ ".hello",                      "X Z H" },
@@ -105,6 +105,24 @@ static const Vector<Selector> selectors =
 	{ ".hello.world, #P span, #I",   "Z D0 D1 F0 I" },
 	{ "body * span",                 "D0 D1 F0" },
 };
+struct ClosestSelector {
+	String start_id;
+	String selector;
+	String expected_id;
+};
+static const Vector<ClosestSelector> closest_selectors =
+{
+	{ "D1",  "#P",               "P" },
+	{ "D1",  "#P, body",         "P" },
+	{ "D1",  "#P, #D",           "D" },
+	{ "D1",  "#Z",               "" },
+	{ "D1",  "#D1",              "" },
+	{ "D1",  "#D0",              "" },
+	{ "D1",  "div",              "P" },
+	{ "D1",  "p",                "D" },
+	{ "D1",  ":nth-child(4)",    "D" },
+	{ "D1",  "div:nth-child(4)", "P" },
+};
 
 
 // Recursively iterate through 'element' and all of its descendants to find all
@@ -156,7 +174,7 @@ TEST_CASE("Selectors")
 
 	SUBCASE("RCSS document selectors")
 	{
-		for (const Selector& selector : selectors)
+		for (const QuerySelector& selector : query_selectors)
 		{
 			const String selector_css = selector.selector + " { drag: drag; } ";
 			const String document_string = doc_begin + selector_css + doc_end;
@@ -180,7 +198,7 @@ TEST_CASE("Selectors")
 		ElementDocument* document = context->LoadDocumentFromMemory(document_string);
 		REQUIRE(document);
 
-		for (const Selector& selector : selectors)
+		for (const QuerySelector& selector : query_selectors)
 		{
 			ElementList elements;
 			document->QuerySelectorAll(elements, selector.selector);
@@ -197,8 +215,26 @@ TEST_CASE("Selectors")
 			}
 
 			CHECK_MESSAGE(matching_ids == selector.expected_ids, "QuerySelector: " << selector.selector);
-			context->UnloadDocument(document);
 		}
+		context->UnloadDocument(document);
+	}
+
+	SUBCASE("Closest")
+	{
+		const String document_string = doc_begin + doc_end;
+		ElementDocument* document = context->LoadDocumentFromMemory(document_string);
+		REQUIRE(document);
+
+		for (const ClosestSelector& selector : closest_selectors)
+		{
+			Element* start = document->GetElementById(selector.start_id);
+			REQUIRE(start);
+
+			Element* match = start->Closest(selector.selector);
+			const String match_id = match ? match->GetId() : "";
+			CHECK_MESSAGE(match_id == selector.expected_id, "Closest() selector '" << selector.selector << "' from " << selector.start_id);
+		}
+		context->UnloadDocument(document);
 	}
 
 	Rml::Shutdown();

+ 2 - 1
changelog.md

@@ -115,7 +115,7 @@ Use the RCSS `display` property to enable table formatting. See the style sheet
 
 ### Element improvements
 
-- Implemented `Element::QuerySelector` and `Element::QuerySelectorAll`.
+- Implemented `Element::QuerySelector`, `Element::QuerySelectorAll`, and `Element::Closest`.
 - The `tab-index: auto` property can now be set on the `<body>` element to enable tabbing back to the document.
 - `<select>` elements now react to changes in the `value` attribute.
 - Element attributes can now use escaped RML characters, eg. `<p example="&quot;Quoted text&quot;"/>`. [#154](https://github.com/mikke89/RmlUi/pull/154) (thanks @actboy168).
@@ -162,6 +162,7 @@ Improved Lua plugin in several aspects.
 - Fix a bug where font textures were leaked on `Rml::Shutdown()`. [#133](https://github.com/mikke89/RmlUi/issues/133)
 - Fixed building with MinGW, and added it to the CI to avoid future breaks. [#108](https://github.com/mikke89/RmlUi/pull/108) (thanks @cloudwu).
 - Fixed several compilation issues and warnings. [#118](https://github.com/mikke89/RmlUi/issues/118) [#97](https://github.com/mikke89/RmlUi/pull/97) [#157](https://github.com/mikke89/RmlUi/pull/157) (thanks @SpaceCat-Chan and @LWSS).
+- Fixed a bug where the `transition: all ...;` property would not parse correctly.
 - Fix \<textarea\> getting an unnecessary horizontal scrollbar. [#122](https://github.com/mikke89/RmlUi/issues/122)
 - Fix text position changing in input fields when selecting text and font has kerning.
 - Fix text-decoration not always being regenerated. [#119](https://github.com/mikke89/RmlUi/issues/119)