PageRenderTime 124ms CodeModel.GetById 94ms app.highlight 25ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 28#include "../linden_common.h"
 29#include "../reflective.h"
 30#include "../metaclasst.h"
 31#include "../metapropertyt.h"
 32#include "../stdtypes.h"
 33
 34#include "../test/lltut.h"
 35
 36namespace tut
 37{
 38  class TestAggregatedData : public LLReflective
 39  {
 40  public:
 41	TestAggregatedData() {;}
 42	virtual const LLMetaClass& getMetaClass() const;
 43  
 44  private:
 45  };
 46  
 47  class TestReflectionData : public LLReflective
 48  {
 49  public:
 50	TestReflectionData() : mInt(42), mString("foo"), mNullPtr(NULL), mPtr(new TestAggregatedData()), mRef(*(new TestAggregatedData)) {;}
 51	virtual ~TestReflectionData() {delete mPtr;}
 52	virtual const LLMetaClass& getMetaClass() const;
 53	
 54	static U32 getPropertyCount() {return 5;}
 55	
 56  private:
 57  
 58	friend class LLMetaClassT<TestReflectionData>;
 59    S32 mInt;
 60	std::string mString;
 61	TestAggregatedData* mNullPtr;
 62	TestAggregatedData* mPtr;
 63	TestAggregatedData mObj;
 64	TestAggregatedData& mRef;
 65  };
 66}
 67
 68template <>
 69void LLMetaClassT<tut::TestReflectionData>::reflectProperties(LLMetaClass& meta_class)
 70{
 71	reflectProperty(meta_class, "mInt", &tut::TestReflectionData::mInt);
 72	reflectProperty(meta_class, "mString", &tut::TestReflectionData::mString);
 73	reflectPtrProperty(meta_class, "mNullPtr", &tut::TestReflectionData::mNullPtr);
 74	reflectPtrProperty(meta_class, "mPtr", &tut::TestReflectionData::mPtr);
 75	reflectProperty(meta_class, "mObj", &tut::TestReflectionData::mObj);
 76	//reflectProperty(meta_class, "mRef", &tut::TestReflectionData::mRef); // AARGH!
 77}
 78
 79namespace tut
 80{
 81	// virtual
 82	const LLMetaClass& TestReflectionData::getMetaClass() const
 83	{
 84	   return LLMetaClassT<TestReflectionData>::instance();
 85    }
 86	
 87	const LLMetaClass& TestAggregatedData::getMetaClass() const
 88	{
 89	   return LLMetaClassT<TestAggregatedData>::instance();
 90    }
 91}
 92
 93namespace tut
 94{
 95  typedef tut::test_group<TestReflectionData> TestReflectionGroup;
 96  typedef TestReflectionGroup::object TestReflectionObject;
 97  TestReflectionGroup gTestReflectionGroup("reflection");
 98
 99  template<> template<>
100  void TestReflectionObject::test<1>()
101  {
102	// Check properties can be found.
103    const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
104	const LLMetaProperty* null = NULL;
105	ensure_not_equals(meta_class.findProperty("mInt"), null);
106	ensure_not_equals(meta_class.findProperty("mString"), null);
107  }
108  
109  template<> template<>
110  void TestReflectionObject::test<2>()
111  {
112	// Check non-existent property cannot be found.
113    const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
114	const LLMetaProperty* null = NULL;
115	ensure_equals(meta_class.findProperty("foo"), null);
116  }
117  
118  template<> template<>
119  void TestReflectionObject::test<3>()
120  {
121	// Check integer property has correct value.	
122    const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
123	ensure_equals(meta_class.findProperty("mInt")->getLLSD(this).asInteger(), 42);
124  }
125  
126  template<> template<>
127  void TestReflectionObject::test<4>()
128  {
129	// Check string property has correct value.	
130    const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
131	ensure_equals(meta_class.findProperty("mString")->getLLSD(this).asString(), std::string("foo"));
132  }
133  
134  template<> template<>
135  void TestReflectionObject::test<5>()
136  {
137	// Check NULL reference property has correct value.
138	const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
139	const LLReflective* null = NULL;
140	ensure_equals(meta_class.findProperty("mNullPtr")->get(this), null);
141  }
142  
143  template<> template<>
144  void TestReflectionObject::test<6>()
145  {
146	// Check reference property has correct value.
147	const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
148	const LLReflective* null = NULL;
149	const LLReflective* ref = meta_class.findProperty("mPtr")->get(this);
150	ensure_not_equals(ref, null);
151  }
152  
153  template<> template<>
154  void TestReflectionObject::test<7>()
155  {
156	// Check reflective property has correct value.
157	const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
158	const LLReflective* null = NULL;
159	const LLReflective* ref = meta_class.findProperty("mObj")->get(this);
160	ensure_not_equals(ref, null);
161  }
162
163  template<> template<>
164  void TestReflectionObject::test<8>()
165  {
166	// Check property count.
167    const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
168	ensure_equals(meta_class.getPropertyCount(), TestReflectionData::getPropertyCount());
169  }
170  
171  template<> template<>
172  void TestReflectionObject::test<9>()
173  {
174	// Check property iteration.
175    const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
176	U32 count = 0;
177	LLMetaClass::PropertyIterator iter;
178	for(iter = meta_class.beginProperties(); iter != meta_class.endProperties(); ++iter)
179	{
180		++count;
181	}
182	ensure_equals(count, TestReflectionData::getPropertyCount());
183  }
184  
185  template<> template<>
186  void TestReflectionObject::test<10>()
187  {
188	// Check meta classes of different types do not compare equal.
189	const LLMetaClass* reflection_data_meta_class = &(LLMetaClassT<TestReflectionData>::instance());
190	const LLMetaClass* aggregated_data_meta_class = &(LLMetaClassT<TestAggregatedData>::instance());
191	ensure_not_equals(reflection_data_meta_class, aggregated_data_meta_class);
192  }
193  
194  template<> template<>
195  void TestReflectionObject::test<11>()
196  {
197	// Check class cast checks.
198	const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
199	TestAggregatedData* aggregated_data = new TestAggregatedData();
200	LLMetaClass::PropertyIterator iter;
201	U32 exception_count = 0;
202	for(iter = meta_class.beginProperties(); iter != meta_class.endProperties(); ++iter)
203	{
204		try
205		{
206			const LLMetaProperty* property = (*iter).second;
207			const LLReflective* reflective = property->get(aggregated_data); // Wrong reflective type, should throw exception.
208
209			// useless op to get rid of compiler warning.
210			reflective = NULL;
211		}
212		catch(...)
213		{
214			++exception_count;
215		}
216	}
217	ensure_equals(exception_count, getPropertyCount());
218	
219  }
220}