PropertySpecification.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 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 <doctest.h>
  34. #include <limits.h>
  35. using namespace Rml;
  36. TEST_CASE("PropertySpecification")
  37. {
  38. TestsSystemInterface system_interface;
  39. TestsRenderInterface render_interface;
  40. SetRenderInterface(&render_interface);
  41. SetSystemInterface(&system_interface);
  42. Rml::Initialise();
  43. PropertySpecification specification(1, 0);
  44. const PropertyId id = specification.RegisterProperty("name", "", false, false).AddParser("string").GetId();
  45. // Parse value into the <string> property.
  46. auto Parse = [&](const String& test_value, const String& expected) {
  47. PropertyDictionary properties;
  48. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  49. const auto num_properties = properties.GetProperties().size();
  50. CHECK(parse_success);
  51. CHECK(num_properties == 1);
  52. CHECK(properties.GetProperty(id));
  53. if (const Property* property = properties.GetProperty(id))
  54. {
  55. const String parsed_value = property->Get<String>();
  56. CHECK_MESSAGE(parsed_value == expected, "Test value: ", test_value);
  57. }
  58. };
  59. Parse("a", "a");
  60. Parse(" a ", "a");
  61. Parse("green", "green");
  62. Parse("image(ress:///.ress#/images/a.png)", "image(ress:///.ress#/images/a.png)");
  63. Parse(R"(image("ress:///.ress#/images/a.png"))", R"(image("ress:///.ress#/images/a.png"))");
  64. Parse(R"("ress:///.ress#/images/a.png")", R"(ress:///.ress#/images/a.png)");
  65. Parse(R"("escaped\"quotes")", R"(escaped"quotes)");
  66. Parse(R"("escaped\\backslash")", R"(escaped\backslash)");
  67. Parse(R"("bad_\escape")", R"(bad_\escape)");
  68. Parse(R"(C:\Windows\test.png)", R"(C:\Windows\test.png)");
  69. Parse(R"("C:\Windows\test.png")", R"(C:\Windows\test.png)");
  70. Parse(R"(C:\\Windows\\test.png)", R"(C:\\Windows\\test.png)");
  71. Parse(R"("C:\\Windows\\test.png")", R"(C:\Windows\test.png)");
  72. Parse(R"(\\host\test.png)", R"(\\host\test.png)");
  73. Parse(R"(\\\host\test.png)", R"(\\\host\test.png)");
  74. Parse(R"("\\host\\test.png")", R"(\host\test.png)");
  75. Parse("image(a)", "image(a)");
  76. Parse(R"(image(a))", R"(image(a))");
  77. Parse(R"(image(a, "b"))", R"(image(a, "b"))");
  78. Parse(R"V("image(a, \"b\")")V", R"V(image(a, "b"))V");
  79. Parse(R"(image( ))", R"(image( ))");
  80. Parse(R"(image( a\)b ))", R"(image( a)b ))");
  81. Parse(R"(image("a\)b"))", R"(image("a)b"))");
  82. Parse(R"(image( a\\b ))", R"(image( a\b ))");
  83. Parse(R"(image( a\\\b ))", R"(image( a\\b ))");
  84. Parse(R"(image( a\\\\b ))", R"(image( a\\b ))");
  85. Rml::Shutdown();
  86. }
  87. TEST_CASE("PropertyParser.Keyword")
  88. {
  89. TestsSystemInterface system_interface;
  90. TestsRenderInterface render_interface;
  91. SetRenderInterface(&render_interface);
  92. SetSystemInterface(&system_interface);
  93. Rml::Initialise();
  94. PropertySpecification specification(20, 0);
  95. auto Parse = [&](const PropertyId id, const String& test_value, int expected_value) {
  96. PropertyDictionary properties;
  97. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  98. if (expected_value == INT_MAX)
  99. {
  100. CHECK(!parse_success);
  101. }
  102. else
  103. {
  104. CHECK(parse_success);
  105. CHECK(properties.GetProperties().size() == 1);
  106. const int parsed_value = properties.GetProperty(id)->Get<int>();
  107. CHECK_MESSAGE(parsed_value == expected_value, "Test value: ", test_value);
  108. const String parsed_value_str = properties.GetProperty(id)->ToString();
  109. CHECK(parsed_value_str == test_value);
  110. }
  111. };
  112. const PropertyId simple = specification.RegisterProperty("simple", "", false, false).AddParser("keyword", "a, b, c").GetId();
  113. Parse(simple, "a", 0);
  114. Parse(simple, "b", 1);
  115. Parse(simple, "c", 2);
  116. Parse(simple, "d", INT_MAX);
  117. Parse(simple, "0", INT_MAX);
  118. Parse(simple, "2", INT_MAX);
  119. const PropertyId values = specification.RegisterProperty("values", "", false, false).AddParser("keyword", "a=50, b, c=-200").GetId();
  120. Parse(values, "a", 50);
  121. Parse(values, "b", 51);
  122. Parse(values, "c", -200);
  123. Parse(values, "d", INT_MAX);
  124. Parse(values, "0", INT_MAX);
  125. Parse(values, "2", INT_MAX);
  126. const PropertyId numbers =
  127. specification.RegisterProperty("numbers", "", false, false).AddParser("keyword", "a=10, b=20, c=30").AddParser("number").GetId();
  128. Parse(numbers, "a", 10);
  129. Parse(numbers, "b", 20);
  130. Parse(numbers, "c", 30);
  131. Parse(numbers, "d", INT_MAX);
  132. Parse(numbers, "0", 0);
  133. Parse(numbers, "2", 2);
  134. Parse(numbers, "20", 20);
  135. Rml::Shutdown();
  136. }