DataExpression.cpp 4.7 KB

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