DataExpression.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <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.GetTransformFuncRegister());
  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" << interpreter.DumpProgram());
  49. }
  50. else
  51. {
  52. FAIL_CHECK("Could not parse expression: " << expression);
  53. }
  54. return result;
  55. }
  56. static bool TestAssignment(const String& expression)
  57. {
  58. bool result = false;
  59. DataParser parser(expression, interface);
  60. if (parser.Parse(true))
  61. {
  62. Program program = parser.ReleaseProgram();
  63. AddressList addresses = parser.ReleaseAddresses();
  64. DataInterpreter interpreter(program, addresses, interface);
  65. if (interpreter.Run())
  66. result = true;
  67. else
  68. FAIL_CHECK("Could not execute assignment expression: " << expression << "\n\n Parsed program: \n" << interpreter.DumpProgram());
  69. }
  70. else
  71. {
  72. FAIL_CHECK("Could not parse assignment expression: " << expression);
  73. }
  74. return result;
  75. }
  76. TEST_CASE("Data expressions")
  77. {
  78. float radius = 8.7f;
  79. String color_name = "color";
  80. Colourb color_value = Colourb(180, 100, 255);
  81. DataModelConstructor handle(&model, &type_register);
  82. handle.Bind("radius", &radius);
  83. handle.Bind("color_name", &color_name);
  84. handle.BindFunc("color_value", [&](Variant& variant) {
  85. variant = ToString(color_value);
  86. });
  87. CHECK(TestExpression("!!10 - 1 ? 'hello' : 'world' | to_upper") == "WORLD");
  88. CHECK(TestExpression("(color_name) + (': rgba(' + color_value + ')')") == "color: rgba(180, 100, 255, 255)");
  89. CHECK(TestExpression("'hello world' | to_upper(5 + 12 == 17 ? 'yes' : 'no', 9*2)") == "HELLO WORLD");
  90. CHECK(TestExpression("true == false") == "0");
  91. CHECK(TestExpression("true != false") == "1");
  92. CHECK(TestExpression("true") == "1");
  93. CHECK(TestExpression("true || false ? true && 3==1+2 ? 'Absolutely!' : 'well..' : 'no'") == "Absolutely!");
  94. CHECK(TestExpression(R"('It\'s a fit')") == R"(It's a fit)");
  95. CHECK(TestExpression("2 * 2") == "4");
  96. CHECK(TestExpression("50000 / 1500") == "33.333");
  97. CHECK(TestExpression("5*1+2") == "7");
  98. CHECK(TestExpression("5*(1+2)") == "15");
  99. CHECK(TestExpression("2*(-2)/4") == "-1");
  100. CHECK(TestExpression("5.2 + 19 + 'px'") == "24.2px");
  101. CHECK(TestExpression("(radius | format(2)) + 'm'") == "8.70m");
  102. CHECK(TestExpression("radius < 10.5 ? 'smaller' : 'larger'") == "smaller");
  103. CHECK(TestAssignment("radius = 15"));
  104. CHECK(radius == doctest::Approx(15.f));
  105. CHECK(TestExpression("radius < 10.5 ? 'smaller' : 'larger'") == "larger");
  106. CHECK(TestAssignment("radius = 4; color_name = 'image-color'"));
  107. CHECK(radius == doctest::Approx(4.f));
  108. CHECK(color_name == "image-color");
  109. CHECK(TestExpression("radius == 4 && color_name == 'image-color'") == "1");
  110. CHECK(TestExpression("5 == 1 + 2*2 || 8 == 1 + 4 ? 'yes' : 'no'") == "yes");
  111. CHECK(TestExpression("!!('fa' + 'lse')") == "0");
  112. CHECK(TestExpression("!!('tr' + 'ue')") == "1");
  113. CHECK(TestExpression("'fox' + 'dog' ? 'FoxyDog' : 'hot' + 'dog' | to_upper") == "HOTDOG");
  114. CHECK(TestExpression("3.62345 | round") == "4");
  115. CHECK(TestExpression("3.62345 | format(0)") == "4");
  116. CHECK(TestExpression("3.62345 | format(2)") == "3.62");
  117. CHECK(TestExpression("3.62345 | format(10)") == "3.6234500000");
  118. CHECK(TestExpression("3.62345 | format(10, true)") == "3.62345");
  119. CHECK(TestExpression("3.62345 | round | format(2)") == "4.00");
  120. CHECK(TestExpression("3.0001 | format(2, false)") == "3.00");
  121. CHECK(TestExpression("3.0001 | format(2, true)") == "3");
  122. CHECK(TestExpression("0.2 + 3.42345 | round") == "4");
  123. CHECK(TestExpression("(3.42345 | round) + 0.2") == "3.2");
  124. CHECK(TestExpression("(3.42345 | format(0)) + 0.2") == "30.2"); // Here, format(0) returns a string, so the + means string concatenation.
  125. }