DataExpression.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "../../../Source/Core/DataExpression.cpp"
  29. #include <RmlUi/Core/DataModelHandle.h>
  30. #include <RmlUi/Core/Types.h>
  31. #include <doctest.h>
  32. using namespace Rml;
  33. static DataTypeRegister type_register;
  34. static DataModel model(&type_register);
  35. static DataExpressionInterface interface(&model, nullptr);
  36. static String TestExpression(const String& expression)
  37. {
  38. String result;
  39. DataParser parser(expression, interface);
  40. if (parser.Parse(false))
  41. {
  42. Program program = parser.ReleaseProgram();
  43. AddressList addresses = parser.ReleaseAddresses();
  44. DataInterpreter interpreter(program, addresses, interface);
  45. if (interpreter.Run())
  46. result = interpreter.Result().Get<String>();
  47. else
  48. FAIL_CHECK("Could not execute expression: " << expression << "\n\n Parsed program: \n" << DumpProgram(program));
  49. }
  50. else
  51. {
  52. Program program = parser.ReleaseProgram();
  53. FAIL_CHECK("Could not parse expression: " << expression << "\n\n Parsed result: \n" << DumpProgram(program));
  54. }
  55. return result;
  56. }
  57. static bool TestAssignment(const String& expression)
  58. {
  59. bool result = false;
  60. DataParser parser(expression, interface);
  61. if (parser.Parse(true))
  62. {
  63. Program program = parser.ReleaseProgram();
  64. AddressList addresses = parser.ReleaseAddresses();
  65. DataInterpreter interpreter(program, addresses, interface);
  66. if (interpreter.Run())
  67. result = true;
  68. else
  69. FAIL_CHECK("Could not execute assignment expression: " << expression << "\n\n Parsed program: \n" << DumpProgram(program));
  70. }
  71. else
  72. {
  73. Program program = parser.ReleaseProgram();
  74. FAIL_CHECK("Could not parse assignment expression: " << expression << "\n\n Parsed result: \n" << DumpProgram(program));
  75. }
  76. return result;
  77. }
  78. TEST_CASE("Data expressions")
  79. {
  80. float radius = 8.7f;
  81. int num_trolls = 1;
  82. String color_name = "color";
  83. Colourb color_value = Colourb(180, 100, 255);
  84. std::vector<String> num_multi = {"left", "right"};
  85. DataModelConstructor constructor(&model);
  86. constructor.RegisterArray<std::vector<String>>();
  87. constructor.Bind("radius", &radius);
  88. constructor.Bind("color_name", &color_name);
  89. constructor.Bind("num_trolls", &num_trolls);
  90. constructor.Bind("num_multi", &num_multi);
  91. constructor.BindFunc("color_value", [&](Variant& variant) { variant = ToString(color_value); });
  92. constructor.RegisterTransformFunc("concatenate", [](const VariantList& arguments) -> Variant {
  93. StringList list;
  94. list.reserve(arguments.size());
  95. for (const Variant& argument : arguments)
  96. list.push_back(argument.Get<String>());
  97. String result;
  98. StringUtilities::JoinString(result, list);
  99. return Variant(std::move(result));
  100. });
  101. constructor.RegisterTransformFunc("number_suffix", [](const VariantList& arguments) -> Variant {
  102. if (arguments.size() != 3)
  103. return {};
  104. String suffix = (arguments[0].Get<double>() == 1.0 ? arguments[1] : arguments[2]).Get<String>();
  105. return Variant(arguments[0].Get<String>() + ' ' + suffix);
  106. });
  107. DataModelHandle handle = constructor.GetModelHandle();
  108. CHECK(TestExpression("3.62345 | round | format(2)") == "4.00");
  109. CHECK(TestExpression("'a' | to_upper") == "A");
  110. CHECK(TestExpression("!!10 - 1 ? 'hello' : 'world' | to_upper") == "WORLD");
  111. CHECK(TestExpression("(color_name) + (': ' + color_value)") == "color: #b464ff");
  112. CHECK(TestExpression("'hello world' | to_upper | concatenate(5 + 12 == 17 ? 'yes' : 'no', 9*2)") == "HELLO WORLD,yes,18");
  113. CHECK(TestExpression("true == false") == "0");
  114. CHECK(TestExpression("true != false") == "1");
  115. CHECK(TestExpression("true") == "1");
  116. CHECK(TestExpression("true || false ? true && 3==1+2 ? 'Absolutely!' : 'well..' : 'no'") == "Absolutely!");
  117. CHECK(TestExpression(R"('It\'s a fit')") == R"(It's a fit)");
  118. CHECK(TestExpression("2 * 2") == "4");
  119. CHECK(TestExpression("50000 / 1500") == "33.333");
  120. CHECK(TestExpression("5*1+2") == "7");
  121. CHECK(TestExpression("5*(1+2)") == "15");
  122. CHECK(TestExpression("2*(-2)/4") == "-1");
  123. CHECK(TestExpression("5.2 + 19 + 'px'") == "24.2px");
  124. CHECK(TestExpression("(radius | format(2)) + 'm'") == "8.70m");
  125. CHECK(TestExpression("radius < 10.5 ? 'smaller' : 'larger'") == "smaller");
  126. CHECK(TestAssignment("radius = 15"));
  127. CHECK(radius == doctest::Approx(15.f));
  128. CHECK(TestExpression("radius < 10.5 ? 'smaller' : 'larger'") == "larger");
  129. CHECK(TestAssignment("radius = 4; color_name = 'image-color'"));
  130. CHECK(radius == doctest::Approx(4.f));
  131. CHECK(color_name == "image-color");
  132. CHECK(TestExpression("radius == 4 && color_name == 'image-color'") == "1");
  133. CHECK(TestAssignment("color_name = 'a' | concatenate('b')"));
  134. CHECK(color_name == "a,b");
  135. CHECK(TestAssignment("color_name = concatenate('c','d')"));
  136. CHECK(color_name == "c,d");
  137. CHECK(TestExpression("5 == 1 + 2*2 || 8 == 1 + 4 ? 'yes' : 'no'") == "yes");
  138. CHECK(TestExpression("!!('fa' + 'lse')") == "0");
  139. CHECK(TestExpression("!!('tr' + 'ue')") == "1");
  140. CHECK(TestExpression("'fox' + 'dog' ? 'FoxyDog' : 'hot' + 'dog' | to_upper") == "HOTDOG");
  141. CHECK(TestExpression("3.62345 | round") == "4");
  142. CHECK(TestExpression("3.62345 | format(0)") == "4");
  143. CHECK(TestExpression("3.62345 | format(2)") == "3.62");
  144. CHECK(TestExpression("3.62345 | format(10)") == "3.6234500000");
  145. CHECK(TestExpression("3.62345 | format(10, true)") == "3.62345");
  146. CHECK(TestExpression("3.62345 | round | format(2)") == "4.00");
  147. CHECK(TestExpression("3.0001 | format(2, false)") == "3.00");
  148. CHECK(TestExpression("3.0001 | format(2, true)") == "3");
  149. CHECK(TestExpression("format(3.0001, 2, true)") == "3");
  150. CHECK(TestExpression("0.2 + 3.42345 | round") == "4");
  151. CHECK(TestExpression("(3.42345 | round) + 0.2") == "3.2");
  152. CHECK(TestExpression("(3.42345 | format(0)) + 0.2") == "30.2"); // Here, format(0) returns a string, so the + means string concatenation.
  153. CHECK(TestExpression("'Hi' | concatenate") == "Hi");
  154. CHECK(TestExpression("'Hi' | concatenate('there')") == "Hi,there");
  155. CHECK(TestExpression("'A' | concatenate('b','c', 'd','e')") == "A,b,c,d,e");
  156. CHECK(TestExpression("3.6 | concatenate") == "3.6");
  157. CHECK(TestExpression("concatenate()") == "");
  158. CHECK(TestExpression("concatenate('Hi')") == "Hi");
  159. CHECK(TestExpression("concatenate( 'Hi', 'there' )") == "Hi,there");
  160. CHECK(TestExpression("concatenate('A', 'b'+'c', 'd','e')") == "A,bc,d,e");
  161. CHECK(TestExpression("concatenate('r', 2*radius)") == "r,8");
  162. CHECK(TestExpression("concatenate(3.6)") == "3.6");
  163. CHECK(TestExpression("concatenate(3.6+1)") == "4.6");
  164. CHECK(TestExpression("concatenate(3.6+1) | round | format(3, false)") == "5.000");
  165. CHECK(TestExpression("concatenate(3.6+1 | round, 'x')") == "5,x");
  166. CHECK(TestExpression("num_trolls | number_suffix('troll','trolls')") == "1 troll");
  167. CHECK(TestExpression("concatenate('It takes', num_trolls*3 + ' goats', 'to outsmart', num_trolls | number_suffix('troll','trolls'))") ==
  168. "It takes,3 goats,to outsmart,1 troll");
  169. num_trolls = 3;
  170. handle.DirtyVariable("num_trolls");
  171. CHECK(TestExpression("concatenate('It takes', num_trolls*3 + ' goats', 'to outsmart', num_trolls | number_suffix('troll','trolls'))") ==
  172. "It takes,9 goats,to outsmart,3 trolls");
  173. // Test that only one side of ternary is evaluated
  174. CHECK(TestExpression("true ? num_multi[0] : num_multi[999]") == "left");
  175. CHECK(TestExpression("false ? num_multi[999] : num_multi[1]") == "right");
  176. }