PropertySpecification.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../Common/TestsInterface.h"
  29. #include <RmlUi/Core/Core.h>
  30. #include <RmlUi/Core/PropertyDefinition.h>
  31. #include <RmlUi/Core/PropertyDictionary.h>
  32. #include <RmlUi/Core/PropertySpecification.h>
  33. #include <RmlUi/Core/StyleSheetSpecification.h>
  34. #include <doctest.h>
  35. #include <limits.h>
  36. namespace Rml {
  37. class TestPropertySpecification {
  38. public:
  39. using SplitOption = PropertySpecification::SplitOption;
  40. TestPropertySpecification(const PropertySpecification& specification) : specification(specification) {}
  41. bool ParsePropertyValues(StringList& values_list, const String& values, SplitOption split_option) const
  42. {
  43. return specification.ParsePropertyValues(values_list, values, split_option);
  44. }
  45. private:
  46. const PropertySpecification& specification;
  47. };
  48. } // namespace Rml
  49. using namespace Rml;
  50. static String Stringify(const StringList& list)
  51. {
  52. String result = "[";
  53. for (int i = 0; i < (int)list.size(); i++)
  54. {
  55. if (i != 0)
  56. result += "; ";
  57. result += list[i];
  58. }
  59. result += ']';
  60. return result;
  61. }
  62. TEST_CASE("PropertySpecification.ParsePropertyValues")
  63. {
  64. TestsSystemInterface system_interface;
  65. TestsRenderInterface render_interface;
  66. SetRenderInterface(&render_interface);
  67. SetSystemInterface(&system_interface);
  68. Rml::Initialise();
  69. using SplitOption = TestPropertySpecification::SplitOption;
  70. const TestPropertySpecification& specification = TestPropertySpecification(StyleSheetSpecification::GetPropertySpecification());
  71. struct Expected {
  72. Expected(const char* value) : values{String(value)} {}
  73. Expected(std::initializer_list<String> list) : values(list) {}
  74. StringList values;
  75. };
  76. auto Parse = [&](const String& test_value, const Expected& expected, SplitOption split = SplitOption::Whitespace) {
  77. StringList parsed_values;
  78. bool success = specification.ParsePropertyValues(parsed_values, test_value, split);
  79. const String split_str[] = {"none", "whitespace", "comma"};
  80. INFO("\n\tSplit: ", split_str[(int)split], "\n\tInput: ", test_value, "\n\tExpected: ", Stringify(expected.values),
  81. "\n\tResult: ", Stringify(parsed_values));
  82. CHECK(success);
  83. CHECK(parsed_values == expected.values);
  84. };
  85. Parse("red", "red");
  86. Parse(" red ", "red");
  87. Parse("inline-block", "inline-block");
  88. Parse("none red", {"none", "red"});
  89. Parse("none red", {"none", "red"});
  90. Parse("none\t \r \nred", {"none", "red"});
  91. Parse("none red", "none red", SplitOption::None);
  92. Parse(" none red ", "none red", SplitOption::None);
  93. Parse("none red", "none red", SplitOption::None);
  94. Parse("none\t \r \nred", "none\t \r \nred", SplitOption::None);
  95. Parse("none,red", "none,red", SplitOption::None);
  96. Parse(" \"none,red\" ", "none,red", SplitOption::None);
  97. Parse("none,red", {"none,red"});
  98. Parse("none, red", {"none,", "red"});
  99. Parse("none , red", {"none", ",", "red"});
  100. Parse("none , red", {"none", ",", "red"});
  101. Parse("none,,red", "none,,red");
  102. Parse("none,,,red", "none,,,red");
  103. Parse("none,red", {"none", "red"}, SplitOption::Comma);
  104. Parse("none, red", {"none", "red"}, SplitOption::Comma);
  105. Parse("none , red", {"none", "red"}, SplitOption::Comma);
  106. Parse("none , red", {"none", "red"}, SplitOption::Comma);
  107. Parse("none,,red", {"none", "red"}, SplitOption::Comma);
  108. Parse("none,,,red", {"none", "red"}, SplitOption::Comma);
  109. Parse("none, , ,red", {"none", "red"}, SplitOption::Comma);
  110. Parse("\"string with spaces\"", "string with spaces");
  111. Parse("\"string with spaces\" two", {"string with spaces", "two"});
  112. Parse("\"string with spaces\"two", {"string with spaces", "two"});
  113. Parse("\"string with spaces\"two", "string with spaces two", SplitOption::None);
  114. Parse("\"string (with) ((parenthesis\" two", {"string (with) ((parenthesis", "two"});
  115. Parse("\"none,,red\" two", {"none,,red", "two"});
  116. Parse("aa(bb( cc ) dd) ee", {"aa(bb( cc ) dd)", "ee"});
  117. Parse("aa(\"bb cc ) dd\") ee", {"aa(\"bb cc ) dd\")", "ee"});
  118. Parse("aa(\"bb cc \\) dd\") ee", {"aa(\"bb cc \\) dd\")", "ee"});
  119. Parse("aa(\"bb cc \\) dd\") ee", "aa(\"bb cc \\) dd\") ee", SplitOption::Comma);
  120. Parse("none(\"long string\"), aa, \"bb() cc\"", {"none(\"long string\"),", "aa,", "bb() cc"});
  121. Parse("none(\"long string\"), aa, \"bb() cc\"", {"none(\"long string\")", "aa", "\"bb() cc\""}, SplitOption::Comma);
  122. Parse("none(\"long string\"), aa, bb() cc", {"none(\"long string\")", "aa", "bb() cc"}, SplitOption::Comma);
  123. Parse("tiled-horizontal( title-bar-l, title-bar-c, title-bar-r )", "tiled-horizontal( title-bar-l, title-bar-c, title-bar-r )");
  124. Parse("tiled-horizontal( title-bar-l, title-bar-c,\n\ttitle-bar-r )", "tiled-horizontal( title-bar-l, title-bar-c,\n\ttitle-bar-r )");
  125. Parse("tiled-horizontal( title-bar-l, title-bar-c )", "tiled-horizontal( title-bar-l, title-bar-c )", SplitOption::Comma);
  126. Parse("linear-gradient(110deg, #fff, #000 10%) border-box, image(invader.png)",
  127. {"linear-gradient(110deg, #fff, #000 10%)", "border-box,", "image(invader.png)"});
  128. Parse("linear-gradient(110deg, #fff, #000 10%) border-box, image(invader.png)",
  129. {"linear-gradient(110deg, #fff, #000 10%) border-box", "image(invader.png)"}, SplitOption::Comma);
  130. Parse(R"(image( a\) b ))", {R"(image( a\))", "b", ")"});
  131. Parse(R"(image( a\) b ))", R"(image( a\) b ))", SplitOption::Comma);
  132. Parse(R"(image( ))", R"(image( ))");
  133. Parse(R"(image( a\\b ))", R"(image( a\\b ))");
  134. Parse(R"(image( a\\\b ))", R"(image( a\\\b ))");
  135. Parse(R"(image( a\\\\b ))", R"(image( a\\\\b ))");
  136. Parse(R"(image("a\)b"))", R"(image("a\)b"))");
  137. Parse(R"(image("a\\)b"))", R"(image("a\)b"))");
  138. Parse(R"(image("a\\b"))", R"(image("a\b"))");
  139. Parse(R"(image("a\\\b"))", R"(image("a\\b"))");
  140. Parse(R"(image("a\\\\b"))", R"(image("a\\b"))");
  141. Rml::Shutdown();
  142. }
  143. TEST_CASE("PropertySpecification.string")
  144. {
  145. TestsSystemInterface system_interface;
  146. TestsRenderInterface render_interface;
  147. SetRenderInterface(&render_interface);
  148. SetSystemInterface(&system_interface);
  149. Rml::Initialise();
  150. PropertySpecification specification(1, 0);
  151. const PropertyId id = specification.RegisterProperty("name", "", false, false).AddParser("string").GetId();
  152. // Parse value into the <string> property.
  153. auto Parse = [&](const String& test_value, const String& expected) {
  154. PropertyDictionary properties;
  155. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  156. const auto num_properties = properties.GetProperties().size();
  157. CHECK(parse_success);
  158. CHECK(num_properties == 1);
  159. CHECK(properties.GetProperty(id));
  160. if (const Property* property = properties.GetProperty(id))
  161. {
  162. const String parsed_value = property->Get<String>();
  163. CHECK_MESSAGE(parsed_value == expected, "Test value: ", test_value);
  164. }
  165. };
  166. Parse("a", "a");
  167. Parse(" a ", "a");
  168. Parse("green", "green");
  169. Parse("image(ress:///.ress#/images/a.png)", "image(ress:///.ress#/images/a.png)");
  170. Parse(R"(image("ress:///.ress#/images/a.png"))", R"(image("ress:///.ress#/images/a.png"))");
  171. Parse(R"("ress:///.ress#/images/a.png")", R"(ress:///.ress#/images/a.png)");
  172. Parse(R"("escaped\"quotes")", R"(escaped"quotes)");
  173. Parse(R"("escaped\\backslash")", R"(escaped\backslash)");
  174. Parse(R"("bad_\escape")", R"(bad_\escape)");
  175. Parse(R"(C:\Windows\test.png)", R"(C:\Windows\test.png)");
  176. Parse(R"("C:\Windows\test.png")", R"(C:\Windows\test.png)");
  177. Parse(R"(C:\\Windows\\test.png)", R"(C:\\Windows\\test.png)");
  178. Parse(R"("C:\\Windows\\test.png")", R"(C:\Windows\test.png)");
  179. Parse(R"(\\host\test.png)", R"(\\host\test.png)");
  180. Parse(R"(\\\host\test.png)", R"(\\\host\test.png)");
  181. Parse(R"("\\host\\test.png")", R"(\host\test.png)");
  182. Parse("image(a)", "image(a)");
  183. Parse(R"(image(a))", R"(image(a))");
  184. Parse(R"(image(a, "b"))", R"(image(a, "b"))");
  185. Parse(R"V("image(a, \"b\")")V", R"V(image(a, "b"))V");
  186. Rml::Shutdown();
  187. }
  188. TEST_CASE("PropertyParser.Keyword")
  189. {
  190. TestsSystemInterface system_interface;
  191. TestsRenderInterface render_interface;
  192. SetRenderInterface(&render_interface);
  193. SetSystemInterface(&system_interface);
  194. Rml::Initialise();
  195. // Test keyword parser. Ensure that the keyword values are correct.
  196. PropertySpecification specification(20, 0);
  197. auto Parse = [&](const PropertyId id, const String& test_value, int expected_value) {
  198. PropertyDictionary properties;
  199. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  200. if (expected_value == INT_MAX)
  201. {
  202. CHECK(!parse_success);
  203. }
  204. else
  205. {
  206. CHECK(parse_success);
  207. CHECK(properties.GetProperties().size() == 1);
  208. const int parsed_value = properties.GetProperty(id)->Get<int>();
  209. CHECK_MESSAGE(parsed_value == expected_value, "Test value: ", test_value);
  210. const String parsed_value_str = properties.GetProperty(id)->ToString();
  211. CHECK(parsed_value_str == test_value);
  212. }
  213. };
  214. const PropertyId simple = specification.RegisterProperty("simple", "", false, false).AddParser("keyword", "a, b, c").GetId();
  215. Parse(simple, "a", 0);
  216. Parse(simple, "b", 1);
  217. Parse(simple, "c", 2);
  218. Parse(simple, "d", INT_MAX);
  219. Parse(simple, "0", INT_MAX);
  220. Parse(simple, "2", INT_MAX);
  221. const PropertyId values = specification.RegisterProperty("values", "", false, false).AddParser("keyword", "a=50, b, c=-200").GetId();
  222. Parse(values, "a", 50);
  223. Parse(values, "b", 51);
  224. Parse(values, "c", -200);
  225. Parse(values, "d", INT_MAX);
  226. Parse(values, "0", INT_MAX);
  227. Parse(values, "2", INT_MAX);
  228. const PropertyId numbers =
  229. specification.RegisterProperty("numbers", "", false, false).AddParser("keyword", "a=10, b=20, c=30").AddParser("number").GetId();
  230. Parse(numbers, "a", 10);
  231. Parse(numbers, "b", 20);
  232. Parse(numbers, "c", 30);
  233. Parse(numbers, "d", INT_MAX);
  234. Parse(numbers, "0", 0);
  235. Parse(numbers, "2", 2);
  236. Parse(numbers, "20", 20);
  237. Rml::Shutdown();
  238. }