DataExpression.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
  2. #include <doctest.h>
  3. #include "../../../Source/Core/DataExpression.cpp"
  4. using namespace Rml;
  5. DataTypeRegister type_register;
  6. DataModel model(type_register.GetTransformFuncRegister());
  7. String TestExpression(String expression)
  8. {
  9. String result;
  10. DataExpressionInterface interface(&model, nullptr);
  11. DataParser parser(expression, interface);
  12. if (parser.Parse(false))
  13. {
  14. Program program = parser.ReleaseProgram();
  15. AddressList addresses = parser.ReleaseAddresses();
  16. DataInterpreter interpreter(program, addresses, interface);
  17. if (interpreter.Run())
  18. result = interpreter.Result().Get<String>();
  19. // If it fails, it can be useful to check: interpreter.DumpProgram()
  20. }
  21. else
  22. {
  23. FAIL_CHECK("Could not parse expression.");
  24. }
  25. return result;
  26. };
  27. bool TestAssignment(String expression)
  28. {
  29. bool result = false;
  30. DataExpressionInterface interface(&model, nullptr);
  31. DataParser parser(expression, interface);
  32. if (parser.Parse(true))
  33. {
  34. Program program = parser.ReleaseProgram();
  35. AddressList addresses = parser.ReleaseAddresses();
  36. DataInterpreter interpreter(program, addresses, interface);
  37. result = interpreter.Run();
  38. }
  39. return result;
  40. };
  41. TEST_CASE("Data expression parsing and execution") {
  42. float radius = 8.7f;
  43. String color_name = "color";
  44. Colourb color_value = Colourb(180, 100, 255);
  45. DataModelConstructor handle(&model, &type_register);
  46. handle.Bind("radius", &radius);
  47. handle.Bind("color_name", &color_name);
  48. handle.BindFunc("color_value", [&](Variant& variant) {
  49. variant = ToString(color_value);
  50. });
  51. CHECK(TestExpression("!!10 - 1 ? 'hello' : 'world' | to_upper") == "WORLD");
  52. CHECK(TestExpression("(color_name) + (': rgba(' + color_value + ')')") == "color: rgba(180, 100, 255, 255)");
  53. CHECK(TestExpression("'hello world' | to_upper(5 + 12 == 17 ? 'yes' : 'no', 9*2)") == "HELLO WORLD");
  54. CHECK(TestExpression("true == false") == "0");
  55. CHECK(TestExpression("true != false") == "1");
  56. CHECK(TestExpression("true") == "1");
  57. CHECK(TestExpression("true || false ? true && 3==1+2 ? 'Absolutely!' : 'well..' : 'no'") == "Absolutely!");
  58. CHECK(TestExpression(R"('It\'s a fit')") == R"(It's a fit)");
  59. CHECK(TestExpression("2 * 2") == "4");
  60. CHECK(TestExpression("50000 / 1500") == "33.333");
  61. CHECK(TestExpression("5*1+2") == "7");
  62. CHECK(TestExpression("5*(1+2)") == "15");
  63. CHECK(TestExpression("2*(-2)/4") == "-1");
  64. CHECK(TestExpression("5.2 + 19 + 'px'") == "24.2px");
  65. CHECK(TestExpression("(radius | format(2)) + 'm'") == "8.70m");
  66. CHECK(TestExpression("radius < 10.5 ? 'smaller' : 'larger'") == "smaller");
  67. CHECK(TestAssignment("radius = 15"));
  68. CHECK(radius == doctest::Approx(15.f));
  69. CHECK(TestExpression("radius < 10.5 ? 'smaller' : 'larger'") == "larger");
  70. CHECK(TestAssignment("radius = 4; color_name = 'image-color'"));
  71. CHECK(radius == doctest::Approx(4.f));
  72. CHECK(color_name == "image-color");
  73. CHECK(TestExpression("radius == 4 && color_name == 'image-color'") == "1");
  74. CHECK(TestExpression("5 == 1 + 2*2 || 8 == 1 + 4 ? 'yes' : 'no'") == "yes");
  75. CHECK(TestExpression("!!('fa' + 'lse')") == "0");
  76. CHECK(TestExpression("!!('tr' + 'ue')") == "1");
  77. CHECK(TestExpression("'fox' + 'dog' ? 'FoxyDog' : 'hot' + 'dog' | to_upper") == "HOTDOG");
  78. CHECK(TestExpression("3.62345 | round") == "4");
  79. CHECK(TestExpression("3.62345 | format(0)") == "4");
  80. CHECK(TestExpression("3.62345 | format(2)") == "3.62");
  81. CHECK(TestExpression("3.62345 | format(10)") == "3.6234500000");
  82. CHECK(TestExpression("3.62345 | format(10, true)") == "3.62345");
  83. CHECK(TestExpression("3.62345 | round | format(2)") == "4.00");
  84. CHECK(TestExpression("3.0001 | format(2, false)") == "3.00");
  85. CHECK(TestExpression("3.0001 | format(2, true)") == "3");
  86. CHECK(TestExpression("0.2 + 3.42345 | round") == "4");
  87. CHECK(TestExpression("(3.42345 | round) + 0.2") == "3.2");
  88. CHECK(TestExpression("(3.42345 | format(0)) + 0.2") == "30.2"); // Here, format(0) returns a string, so the + means string concatenation.
  89. }