/indra/llcommon/tests/reflection_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 220 lines · 155 code · 25 blank · 40 comment · 4 complexity · fc7c85439aae5b5c84dddad65b11949d MD5 · raw file

  1. /**
  2. * @file reflection_test.cpp
  3. * @date May 2006
  4. * @brief Reflection unit tests.
  5. *
  6. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #include "../linden_common.h"
  28. #include "../reflective.h"
  29. #include "../metaclasst.h"
  30. #include "../metapropertyt.h"
  31. #include "../stdtypes.h"
  32. #include "../test/lltut.h"
  33. namespace tut
  34. {
  35. class TestAggregatedData : public LLReflective
  36. {
  37. public:
  38. TestAggregatedData() {;}
  39. virtual const LLMetaClass& getMetaClass() const;
  40. private:
  41. };
  42. class TestReflectionData : public LLReflective
  43. {
  44. public:
  45. TestReflectionData() : mInt(42), mString("foo"), mNullPtr(NULL), mPtr(new TestAggregatedData()), mRef(*(new TestAggregatedData)) {;}
  46. virtual ~TestReflectionData() {delete mPtr;}
  47. virtual const LLMetaClass& getMetaClass() const;
  48. static U32 getPropertyCount() {return 5;}
  49. private:
  50. friend class LLMetaClassT<TestReflectionData>;
  51. S32 mInt;
  52. std::string mString;
  53. TestAggregatedData* mNullPtr;
  54. TestAggregatedData* mPtr;
  55. TestAggregatedData mObj;
  56. TestAggregatedData& mRef;
  57. };
  58. }
  59. template <>
  60. void LLMetaClassT<tut::TestReflectionData>::reflectProperties(LLMetaClass& meta_class)
  61. {
  62. reflectProperty(meta_class, "mInt", &tut::TestReflectionData::mInt);
  63. reflectProperty(meta_class, "mString", &tut::TestReflectionData::mString);
  64. reflectPtrProperty(meta_class, "mNullPtr", &tut::TestReflectionData::mNullPtr);
  65. reflectPtrProperty(meta_class, "mPtr", &tut::TestReflectionData::mPtr);
  66. reflectProperty(meta_class, "mObj", &tut::TestReflectionData::mObj);
  67. //reflectProperty(meta_class, "mRef", &tut::TestReflectionData::mRef); // AARGH!
  68. }
  69. namespace tut
  70. {
  71. // virtual
  72. const LLMetaClass& TestReflectionData::getMetaClass() const
  73. {
  74. return LLMetaClassT<TestReflectionData>::instance();
  75. }
  76. const LLMetaClass& TestAggregatedData::getMetaClass() const
  77. {
  78. return LLMetaClassT<TestAggregatedData>::instance();
  79. }
  80. }
  81. namespace tut
  82. {
  83. typedef tut::test_group<TestReflectionData> TestReflectionGroup;
  84. typedef TestReflectionGroup::object TestReflectionObject;
  85. TestReflectionGroup gTestReflectionGroup("reflection");
  86. template<> template<>
  87. void TestReflectionObject::test<1>()
  88. {
  89. // Check properties can be found.
  90. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  91. const LLMetaProperty* null = NULL;
  92. ensure_not_equals(meta_class.findProperty("mInt"), null);
  93. ensure_not_equals(meta_class.findProperty("mString"), null);
  94. }
  95. template<> template<>
  96. void TestReflectionObject::test<2>()
  97. {
  98. // Check non-existent property cannot be found.
  99. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  100. const LLMetaProperty* null = NULL;
  101. ensure_equals(meta_class.findProperty("foo"), null);
  102. }
  103. template<> template<>
  104. void TestReflectionObject::test<3>()
  105. {
  106. // Check integer property has correct value.
  107. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  108. ensure_equals(meta_class.findProperty("mInt")->getLLSD(this).asInteger(), 42);
  109. }
  110. template<> template<>
  111. void TestReflectionObject::test<4>()
  112. {
  113. // Check string property has correct value.
  114. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  115. ensure_equals(meta_class.findProperty("mString")->getLLSD(this).asString(), std::string("foo"));
  116. }
  117. template<> template<>
  118. void TestReflectionObject::test<5>()
  119. {
  120. // Check NULL reference property has correct value.
  121. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  122. const LLReflective* null = NULL;
  123. ensure_equals(meta_class.findProperty("mNullPtr")->get(this), null);
  124. }
  125. template<> template<>
  126. void TestReflectionObject::test<6>()
  127. {
  128. // Check reference property has correct value.
  129. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  130. const LLReflective* null = NULL;
  131. const LLReflective* ref = meta_class.findProperty("mPtr")->get(this);
  132. ensure_not_equals(ref, null);
  133. }
  134. template<> template<>
  135. void TestReflectionObject::test<7>()
  136. {
  137. // Check reflective property has correct value.
  138. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  139. const LLReflective* null = NULL;
  140. const LLReflective* ref = meta_class.findProperty("mObj")->get(this);
  141. ensure_not_equals(ref, null);
  142. }
  143. template<> template<>
  144. void TestReflectionObject::test<8>()
  145. {
  146. // Check property count.
  147. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  148. ensure_equals(meta_class.getPropertyCount(), TestReflectionData::getPropertyCount());
  149. }
  150. template<> template<>
  151. void TestReflectionObject::test<9>()
  152. {
  153. // Check property iteration.
  154. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  155. U32 count = 0;
  156. LLMetaClass::PropertyIterator iter;
  157. for(iter = meta_class.beginProperties(); iter != meta_class.endProperties(); ++iter)
  158. {
  159. ++count;
  160. }
  161. ensure_equals(count, TestReflectionData::getPropertyCount());
  162. }
  163. template<> template<>
  164. void TestReflectionObject::test<10>()
  165. {
  166. // Check meta classes of different types do not compare equal.
  167. const LLMetaClass* reflection_data_meta_class = &(LLMetaClassT<TestReflectionData>::instance());
  168. const LLMetaClass* aggregated_data_meta_class = &(LLMetaClassT<TestAggregatedData>::instance());
  169. ensure_not_equals(reflection_data_meta_class, aggregated_data_meta_class);
  170. }
  171. template<> template<>
  172. void TestReflectionObject::test<11>()
  173. {
  174. // Check class cast checks.
  175. const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
  176. TestAggregatedData* aggregated_data = new TestAggregatedData();
  177. LLMetaClass::PropertyIterator iter;
  178. U32 exception_count = 0;
  179. for(iter = meta_class.beginProperties(); iter != meta_class.endProperties(); ++iter)
  180. {
  181. try
  182. {
  183. const LLMetaProperty* property = (*iter).second;
  184. const LLReflective* reflective = property->get(aggregated_data); // Wrong reflective type, should throw exception.
  185. // useless op to get rid of compiler warning.
  186. reflective = NULL;
  187. }
  188. catch(...)
  189. {
  190. ++exception_count;
  191. }
  192. }
  193. ensure_equals(exception_count, getPropertyCount());
  194. }
  195. }