PersistentTest.cpp 4.5 KB

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