DataExpression.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "../../../Source/Core/DataExpression.cpp"
  29. #include <doctest.h>
  30. #include <nanobench.h>
  31. using namespace ankerl;
  32. using namespace Rml;
  33. static DataTypeRegister type_register;
  34. static DataModel model(type_register.GetTransformFuncRegister());
  35. static DataExpressionInterface interface(&model, nullptr);
  36. String TestExpression(const String& expression, const char* benchmark_name = nullptr)
  37. {
  38. String result;
  39. DataParser parser(expression, interface);
  40. nanobench::Bench bench;
  41. if (benchmark_name)
  42. {
  43. bench.title(benchmark_name);
  44. bench.run("Parse", [&] {
  45. parser.Parse(false);
  46. });
  47. }
  48. if (parser.Parse(false))
  49. {
  50. Program program = parser.ReleaseProgram();
  51. AddressList addresses = parser.ReleaseAddresses();
  52. DataInterpreter interpreter(program, addresses, interface);
  53. if (interpreter.Run())
  54. {
  55. result = interpreter.Result().Get<String>();
  56. if (benchmark_name)
  57. {
  58. bench.run("Execute", [&] {
  59. interpreter.Run();
  60. });
  61. }
  62. }
  63. else
  64. {
  65. FAIL_CHECK("Could not execute expression: " << expression << "\n\n Parsed program: \n" << interpreter.DumpProgram());
  66. }
  67. }
  68. else
  69. {
  70. FAIL_CHECK("Could not parse expression: " << expression);
  71. }
  72. return result;
  73. };
  74. bool TestAssignment(const String& expression)
  75. {
  76. bool result = false;
  77. DataParser parser(expression, interface);
  78. if (parser.Parse(true))
  79. {
  80. Program program = parser.ReleaseProgram();
  81. AddressList addresses = parser.ReleaseAddresses();
  82. DataInterpreter interpreter(program, addresses, interface);
  83. if (interpreter.Run())
  84. {
  85. result = true;
  86. }
  87. else
  88. {
  89. FAIL_CHECK("Could not execute assignment expression: " << expression << "\n\n Parsed program: \n" << interpreter.DumpProgram());
  90. }
  91. }
  92. else
  93. {
  94. FAIL_CHECK("Could not parse assignment expression: " << expression);
  95. }
  96. return result;
  97. };
  98. TEST_CASE("Data expressions")
  99. {
  100. float radius = 8.7f;
  101. String color_name = "color";
  102. Colourb color_value = Colourb(180, 100, 255);
  103. DataModelConstructor handle(&model, &type_register);
  104. handle.Bind("radius", &radius);
  105. handle.Bind("color_name", &color_name);
  106. handle.BindFunc("color_value", [&](Variant& variant) {
  107. variant = ToString(color_value);
  108. });
  109. CHECK(TestExpression("!!10 - 1 ? 'hello' : 'world' | to_upper") == "WORLD");
  110. CHECK(TestExpression("(color_name) + (': rgba(' + color_value + ')')") == "color: rgba(180, 100, 255, 255)");
  111. CHECK(TestExpression("'hello world' | to_upper(5 + 12 == 17 ? 'yes' : 'no', 9*2)") == "HELLO WORLD");
  112. CHECK(TestExpression("true == false") == "0");
  113. CHECK(TestExpression("true != false") == "1");
  114. CHECK(TestExpression("true") == "1");
  115. CHECK(TestExpression("true || false ? true && 3==1+2 ? 'Absolutely!' : 'well..' : 'no'") == "Absolutely!");
  116. CHECK(TestExpression(R"('It\'s a fit')") == R"(It's a fit)");
  117. CHECK(TestExpression("2 * 2") == "4");
  118. CHECK(TestExpression("50000 / 1500") == "33.333");
  119. CHECK(TestExpression("5*1+2") == "7");
  120. CHECK(TestExpression("5*(1+2)") == "15");
  121. CHECK(TestExpression("2*(-2)/4") == "-1");
  122. CHECK(TestExpression("5.2 + 19 + 'px'") == "24.2px");
  123. CHECK(TestExpression("(radius | format(2)) + 'm'") == "8.70m");
  124. CHECK(TestExpression("radius < 10.5 ? 'smaller' : 'larger'") == "smaller");
  125. CHECK(TestAssignment("radius = 15"));
  126. CHECK(radius == doctest::Approx(15.f));
  127. CHECK(TestExpression("radius < 10.5 ? 'smaller' : 'larger'") == "larger");
  128. CHECK(TestAssignment("radius = 4; color_name = 'image-color'"));
  129. CHECK(radius == doctest::Approx(4.f));
  130. CHECK(color_name == "image-color");
  131. CHECK(TestExpression("radius == 4 && color_name == 'image-color'") == "1");
  132. CHECK(TestExpression("5 == 1 + 2*2 || 8 == 1 + 4 ? 'yes' : 'no'") == "yes");
  133. CHECK(TestExpression("!!('fa' + 'lse')") == "0");
  134. CHECK(TestExpression("!!('tr' + 'ue')") == "1");
  135. CHECK(TestExpression("'fox' + 'dog' ? 'FoxyDog' : 'hot' + 'dog' | to_upper") == "HOTDOG");
  136. CHECK(TestExpression("3.62345 | round") == "4");
  137. CHECK(TestExpression("3.62345 | format(0)") == "4");
  138. CHECK(TestExpression("3.62345 | format(2)") == "3.62");
  139. CHECK(TestExpression("3.62345 | format(10)") == "3.6234500000");
  140. CHECK(TestExpression("3.62345 | format(10, true)") == "3.62345");
  141. CHECK(TestExpression("3.62345 | round | format(2)") == "4.00");
  142. CHECK(TestExpression("3.0001 | format(2, false)") == "3.00");
  143. CHECK(TestExpression("3.0001 | format(2, true)") == "3");
  144. CHECK(TestExpression("0.2 + 3.42345 | round") == "4");
  145. CHECK(TestExpression("(3.42345 | round) + 0.2") == "3.2");
  146. CHECK(TestExpression("(3.42345 | format(0)) + 0.2") == "30.2"); // Here, format(0) returns a string, so the + means string concatenation.
  147. // Benchmark
  148. #ifdef RMLUI_ENABLE_BENCHMARKS
  149. TestExpression("2 * 2", "Data expression simple");
  150. TestExpression("true || false ? true && 3==1+2 ? 'Absolutely!' : 'well..' : 'no'", "Data expression complex");
  151. #endif
  152. }