PersistentTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 
  2. #include "../testTools.h"
  3. #include "../../DFPSR/implementation/persistent/includePersistent.h"
  4. // -------- Test example below --------
  5. class MyClass : public Persistent {
  6. PERSISTENT_DECLARATION(MyClass)
  7. public:
  8. PersistentInteger a;
  9. PersistentString b;
  10. public:
  11. MyClass() {}
  12. MyClass(int a, const String &b) : a(a), b(PersistentString::unmangled(b)) {}
  13. public:
  14. void declareAttributes(StructureDefinition &target) const override {
  15. target.declareAttribute(U"a");
  16. target.declareAttribute(U"b");
  17. }
  18. Persistent* findAttribute(const ReadableString &name) override {
  19. if (string_caseInsensitiveMatch(name, U"a")) {
  20. return &(this->a);
  21. } else if (string_caseInsensitiveMatch(name, U"b")) {
  22. return &(this->b);
  23. } else {
  24. return nullptr;
  25. }
  26. }
  27. };
  28. PERSISTENT_DEFINITION(MyClass)
  29. class MySubClass : public MyClass {
  30. PERSISTENT_DECLARATION(MySubClass)
  31. public:
  32. PersistentInteger c;
  33. PersistentInteger d;
  34. public:
  35. MySubClass() {}
  36. MySubClass(int a, const String &b, int c, int d) : MyClass(a, b), c(c), d(d) {}
  37. public:
  38. void declareAttributes(StructureDefinition &target) const override {
  39. MyClass::declareAttributes(target);
  40. target.declareAttribute(U"c");
  41. target.declareAttribute(U"d");
  42. }
  43. Persistent* findAttribute(const ReadableString &name) override {
  44. if (string_caseInsensitiveMatch(name, U"c")) {
  45. return &(this->c);
  46. } else if (string_caseInsensitiveMatch(name, U"d")) {
  47. return &(this->d);
  48. } else {
  49. return MyClass::findAttribute(name);
  50. }
  51. }
  52. };
  53. PERSISTENT_DEFINITION(MySubClass)
  54. class MyCollection : public MyClass {
  55. PERSISTENT_DECLARATION(MyCollection)
  56. public:
  57. MyCollection() {}
  58. MyCollection(int a, const String &b) : MyClass(a, b) {}
  59. public:
  60. List<Handle<MyClass>> children;
  61. bool addChild(Handle<Persistent> child) override {
  62. // Try to cast from base class Persistent to derived class MyClass
  63. Handle<MyClass> myClass = handle_dynamicCast<MyClass>(child);
  64. if (myClass.isNull()) {
  65. return false; // Wrong type!
  66. } else {
  67. this->children.push(myClass);
  68. return true; // Success!
  69. }
  70. }
  71. int getChildCount() const override {
  72. return this->children.length();
  73. }
  74. Handle<Persistent> getChild(int index) const override {
  75. return this->children[index];
  76. }
  77. };
  78. PERSISTENT_DEFINITION(MyCollection)
  79. String exampleOne =
  80. R"QUOTE(Begin : MyClass
  81. a = 1
  82. b = "two"
  83. End
  84. )QUOTE";
  85. String exampleTwo =
  86. R"QUOTE(Begin : MySubClass
  87. a = 1
  88. b = "two"
  89. c = 3
  90. d = 4
  91. End
  92. )QUOTE";
  93. String exampleThree =
  94. R"QUOTE(Begin : MyCollection
  95. a = 1
  96. b = "first"
  97. Begin : MyClass
  98. a = 12
  99. b = "test"
  100. End
  101. Begin : MyCollection
  102. a = 2
  103. b = "second"
  104. Begin : MyClass
  105. a = 3
  106. b = "third"
  107. End
  108. End
  109. Begin : MySubClass
  110. a = 34
  111. b = "foo"
  112. c = 56
  113. d = 78
  114. End
  115. End
  116. )QUOTE";
  117. START_TEST(Persistent)
  118. // Register the non-atomic classes
  119. REGISTER_PERSISTENT_CLASS(MyClass)
  120. REGISTER_PERSISTENT_CLASS(MySubClass)
  121. REGISTER_PERSISTENT_CLASS(MyCollection)
  122. // MyClass to text
  123. MyClass myObject(1, U"two");
  124. String myText = myObject.toString();
  125. ASSERT_EQUAL(myText, exampleOne);
  126. // MyClass from text
  127. Handle<Persistent> myObjectCopy = createPersistentClassFromText(myText, U"");
  128. ASSERT_EQUAL(myObjectCopy->toString(), myText);
  129. // MySubClass to text
  130. MySubClass mySubObject(1, U"two", 3, 4);
  131. String MySubText = mySubObject.toString();
  132. ASSERT_EQUAL(MySubText, exampleTwo);
  133. // MySubClass from text
  134. Handle<Persistent> mySubObjectCopy = createPersistentClassFromText(MySubText, U"");
  135. ASSERT_EQUAL(mySubObjectCopy->toString(), MySubText);
  136. // Tree structure to text
  137. MyCollection tree(1, U"first");
  138. ASSERT_EQUAL(tree.getChildCount(), 0);
  139. Handle<MyCollection> second = handle_create<MyCollection>(2, U"second");
  140. tree.addChild(handle_create<MyClass>(12, U"test"));
  141. ASSERT_EQUAL(tree.getChildCount(), 1);
  142. tree.addChild(second);
  143. ASSERT_EQUAL(tree.getChildCount(), 2);
  144. tree.addChild(handle_create<MySubClass>(34, U"foo", 56, 78));
  145. ASSERT_EQUAL(tree.getChildCount(), 3);
  146. ASSERT_EQUAL(second->getChildCount(), 0);
  147. second->addChild(handle_create<MyClass>(3, U"third"));
  148. ASSERT_EQUAL(second->getChildCount(), 1);
  149. ASSERT_EQUAL(tree.getChildCount(), 3);
  150. ASSERT_EQUAL(tree.toString(), exampleThree);
  151. // Tree structure from text
  152. Handle<Persistent> treeCopy = createPersistentClassFromText(exampleThree, U"");
  153. ASSERT_EQUAL(treeCopy->toString(), exampleThree);
  154. // Persistent string lists
  155. PersistentStringList myList = PersistentStringList();
  156. ASSERT_EQUAL(myList.value.length(), 0);
  157. ASSERT_EQUAL(myList.toString(), U"");
  158. myList = PersistentStringList(U"\"\"", U"");
  159. ASSERT_EQUAL(myList.value.length(), 1);
  160. ASSERT_EQUAL(myList.value[0], U"");
  161. ASSERT_EQUAL(myList.toString(), U"\"\"");
  162. myList = PersistentStringList(U"\"A\", \"B\"", U"");
  163. ASSERT_EQUAL(myList.value.length(), 2);
  164. ASSERT_EQUAL(myList.value[0], U"A");
  165. ASSERT_EQUAL(myList.value[1], U"B");
  166. ASSERT_EQUAL(myList.toString(), U"\"A\", \"B\"");
  167. myList.assignValue(U"\"Only element\"", U"");
  168. ASSERT_EQUAL(myList.value.length(), 1);
  169. ASSERT_EQUAL(myList.value[0], U"Only element");
  170. ASSERT_EQUAL(myList.toString(), U"\"Only element\"");
  171. myList = PersistentStringList(U"", U"");
  172. ASSERT_EQUAL(myList.value.length(), 0);
  173. ASSERT_EQUAL(myList.toString(), U"");
  174. myList.assignValue(U"\"Zero 0\", \"One 1\", \"Two 2\", \"Three 3\"", U"");
  175. ASSERT_EQUAL(myList.value.length(), 4);
  176. ASSERT_EQUAL(myList.value[0], U"Zero 0");
  177. ASSERT_EQUAL(myList.value[1], U"One 1");
  178. ASSERT_EQUAL(myList.value[2], U"Two 2");
  179. ASSERT_EQUAL(myList.value[3], U"Three 3");
  180. ASSERT_EQUAL(myList.toString(), U"\"Zero 0\", \"One 1\", \"Two 2\", \"Three 3\"");
  181. END_TEST