DataModel.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/DataModel.cpp"
  29. #include <RmlUi/Core/DataModelHandle.h>
  30. #include <RmlUi/Core/Types.h>
  31. #include <doctest.h>
  32. using namespace Rml;
  33. TEST_CASE("Data variables")
  34. {
  35. using IntVector = Vector<int>;
  36. struct FunData {
  37. int i = 99;
  38. String x = "hello";
  39. IntVector magic = {3, 5, 7, 11, 13};
  40. };
  41. using FunArray = Array<FunData, 3>;
  42. struct SmartData {
  43. bool valid = true;
  44. FunData fun;
  45. FunArray more_fun;
  46. };
  47. DataTypeRegister types;
  48. DataModel model(&types);
  49. DataModelConstructor handle(&model);
  50. // Setup data type register
  51. {
  52. handle.RegisterArray<IntVector>();
  53. if (auto fun_handle = handle.RegisterStruct<FunData>())
  54. {
  55. fun_handle.RegisterMember("i", &FunData::i);
  56. fun_handle.RegisterMember("x", &FunData::x);
  57. fun_handle.RegisterMember("magic", &FunData::magic);
  58. }
  59. handle.RegisterArray<FunArray>();
  60. if (auto smart_handle = handle.RegisterStruct<SmartData>())
  61. {
  62. smart_handle.RegisterMember("valid", &SmartData::valid);
  63. smart_handle.RegisterMember("fun", &SmartData::fun);
  64. smart_handle.RegisterMember("more_fun", &SmartData::more_fun);
  65. }
  66. }
  67. SmartData data;
  68. data.fun.x = "Hello, we're in SmartData!";
  69. handle.Bind("data", &data);
  70. // Test data addresses, setters, and assignments
  71. {
  72. Vector<String> test_addresses = {"data.more_fun[1].magic[3]", "data.more_fun[1].magic.size", "data.fun.x", "data.valid"};
  73. Vector<String> expected_results = {ToString(data.more_fun[1].magic[3]), ToString(int(data.more_fun[1].magic.size())), ToString(data.fun.x),
  74. ToString(data.valid)};
  75. Vector<String> results;
  76. for (auto& str_address : test_addresses)
  77. {
  78. DataAddress address = ParseAddress(str_address);
  79. Variant result;
  80. if (model.GetVariableInto(address, result))
  81. results.push_back(result.Get<String>());
  82. }
  83. CHECK(results == expected_results);
  84. REQUIRE(model.GetVariable(ParseAddress("data.more_fun[1].magic[1]")).Set(Variant(String("199"))));
  85. CHECK(data.more_fun[1].magic[1] == 199);
  86. data.fun.magic = {99, 190, 55, 2000, 50, 60, 70, 80, 90};
  87. Variant get_result;
  88. const int magic_size = int(data.fun.magic.size());
  89. REQUIRE(model.GetVariable(ParseAddress("data.fun.magic.size")).Get(get_result));
  90. CHECK(get_result.Get<String>() == ToString(magic_size));
  91. CHECK(model.GetVariable(ParseAddress("data.fun.magic")).Size() == magic_size);
  92. REQUIRE(model.GetVariable(ParseAddress("data.fun.magic[8]")).Get(get_result));
  93. CHECK(get_result.Get<String>() == "90");
  94. }
  95. }