PageRenderTime 150ms CodeModel.GetById 137ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/libtest/ClassTest.py

http://pyjamas.googlecode.com/
Python | 254 lines | 153 code | 66 blank | 35 comment | 0 complexity | cf99218300411ff22ddc11f4ac771b5f MD5 | raw file
  1from UnitTest import UnitTest
  2
  3class ClassTest(UnitTest):
  4	def __init__(self):
  5		UnitTest.__init__(self)
  6
  7	def getName(self):
  8		return "Class"
  9
 10	# test Class.x
 11	def testClassVars(self):
 12		expected_result1="test"
 13		expected_result2=1
 14		
 15		# check class var value without instance
 16		self.assertEquals(ExampleClass.x, expected_result1)
 17		self.assertEquals(ExampleClass.x.upper(), expected_result1.upper())
 18		
 19		# verify class var value for instances
 20		y = ExampleClass()
 21		self.assertEquals(y.x, expected_result1)
 22		
 23		# modify class var
 24		ExampleClass.x = expected_result2
 25		self.assertEquals(ExampleClass.x, expected_result2)
 26		
 27		# verify that class var changed for NEW instances
 28		z = ExampleClass()
 29		self.assertEquals(z.x, expected_result2)
 30		
 31		# verify that class var changed for EXISTING instances
 32		self.assertEquals(y.x, expected_result2)
 33 
 34	# test Class().x
 35	def testInheritedProperties(self):
 36		expected_result1="test"
 37		expected_result2=1
 38		expected_result3="other"
 39
 40		# check parent property
 41		obj1 = ExampleParentClass()
 42		self.assertEquals(obj1.x, expected_result1)
 43
 44		# check default inherited property
 45		obj1.x = expected_result2
 46		obj2 = ExampleChildClass()
 47		self.assertEquals(obj2.x, expected_result1)
 48
 49		# change inherited property
 50		obj2.x = expected_result3
 51		self.assertEquals(obj2.x, expected_result3)
 52
 53		# verify that parent class properties were NOT changed
 54		self.assertEquals(obj1.x, expected_result2)
 55
 56	# test Class().anObject
 57	def testInheritedPropertyObjects(self):
 58		expected_result1 = "another"
 59		expected_result2 = "other"
 60
 61		# check parent property
 62		obj1 = ExampleParentObject()
 63		self.assertEquals(len(obj1.x), 0)
 64
 65		# check default inherited property
 66		obj1.x.append(expected_result2)
 67		
 68		obj2 = ExampleChildObject()
 69		self.assertEquals(len(obj2.x), 1)
 70
 71		# change inherited property
 72		obj2.x.append(expected_result1)
 73		self.assertEquals(obj2.x[1], expected_result1)
 74
 75		# verify that parent class properties were NOT changed
 76		self.assertEquals(obj1.x[0], expected_result2)
 77
 78	# test Class().__init__
 79	def testInheritedConstructors(self):
 80		expected_result1 = "test"
 81		expected_result2 = "parent"
 82		expected_result3 = "grandparent"
 83		expected_result4 = "older"
 84
 85		# verify that parent.__init__ is called if there is no child.__init__()
 86		obj1 = ExampleChildNoConstructor()
 87		self.assertEquals(obj1.x, expected_result1, "ExampleParentConstructor.__init__() was NOT called for ExampleChildNoConstructor")
 88
 89		# verify that parent.__init__ is NOT called (child.__init__() is defined)
 90		obj2 = ExampleChildConstructor()
 91		self.assertNotEqual(obj2.x, expected_result1, "ExampleParentConstructor.__init__() was called for ExampleChildConstructor")
 92
 93		# verify that parent.__init__ is explicitly called
 94		obj3 = ExampleChildExplicitConstructor()
 95		self.assertEquals(obj3.x, expected_result1, "ExampleParentConstructor.__init__() was NOT called for ExampleChildExplicitConstructor")
 96
 97		# verify inherited values
 98		self.assertEquals(obj1.y, expected_result2, "Did not inherit property from parent")
 99		self.assertEquals(obj2.y, expected_result2, "Did not inherit property from parent")
100		self.assertEquals(obj1.z, expected_result3, "Did not inherit property from grandparent")
101		self.assertEquals(obj2.z, expected_result3, "Did not inherit property from grandparent")
102		
103		self.assertNotEqual(obj1.r, expected_result4, "ExampleGrandParentConstructor.__init__() was called")
104		self.assertNotEqual(obj2.r, expected_result4, "ExampleGrandParentConstructor.__init__() was called")
105
106		# check inherited class vars (from parent)
107		self.assertEqual(ExampleChildConstructor.y, expected_result2, "Did not inherit class var from parent")
108		self.assertEqual(ExampleChildNoConstructor.y, expected_result2, "Did not inherit class var from parent")
109		self.assertEqual(ExampleChildExplicitConstructor.y, expected_result2, "Did not inherit class var from parent")
110
111		# check inherited class vars (from grandparent)
112		self.assertEqual(ExampleChildConstructor.z, expected_result3, "Did not inherit class var from grandparent")
113		self.assertEqual(ExampleChildNoConstructor.z, expected_result3, "Did not inherit class var from grandparent")
114		self.assertEqual(ExampleChildExplicitConstructor.z, expected_result3, "Did not inherit class var from grandparent")
115
116	def testClassMethods(self):
117		results = ExampleClass.sampleClassMethod("a")
118		self.assertEqual(results[0], ExampleClass, "Expected first parameter to be the class instance")
119		self.assertEqual(results[1], "a")
120		
121		results = ExampleParentClass.sampleClassMethod("a")
122		self.assertEqual(results[0], ExampleParentClass, "Expected first parameter to be the class instance")
123		self.assertEqual(results[1], "a")
124
125		results = ExampleChildClass.sampleClassMethod("a")
126		self.assertEqual(results[0], ExampleChildClass, "Expected first parameter to be the class instance")
127		self.assertEqual(results[1], "a")
128		
129		results = ExampleClass.sampleClassMethodVarargs("a", "b", "c")
130		self.assertEqual(results[0], ExampleClass, "Expected first parameter to be the class instance")
131		self.assertEqual(results[1][0], "a")
132		self.assertEqual(results[1][1], "b")
133		self.assertEqual(results[1][2], "c")
134
135		results = ExampleClass.sampleClassMethodKwargs(c=9, b=8, a=7)
136		self.assertEqual(results[0], ExampleClass, "Expected first parameter to be the class instance")
137		self.assertEqual(results[1], 7)
138		self.assertEqual(results[2], 8)
139		self.assertEqual(results[3], 9)
140		
141		#
142		# Repeat the test using class instances; the effect should be the same
143		#
144		
145		results = ExampleClass().sampleClassMethod("a")
146		self.assertEqual(results[0], ExampleClass, "Expected first parameter to be the class instance")
147		self.assertEqual(results[1], "a")
148		
149		results = ExampleParentClass().sampleClassMethod("a")
150		self.assertEqual(results[0], ExampleParentClass, "Expected first parameter to be the class instance")
151		self.assertEqual(results[1], "a")
152
153		results = ExampleChildClass().sampleClassMethod("a")
154		self.assertEqual(results[0], ExampleChildClass, "Expected first parameter to be the class instance")
155		self.assertEqual(results[1], "a")
156		
157		results = ExampleClass().sampleClassMethodVarargs("a", "b", "c")
158		self.assertEqual(results[0], ExampleClass, "Expected first parameter to be the class instance")
159		self.assertEqual(results[1][0], "a")
160		self.assertEqual(results[1][1], "b")
161		self.assertEqual(results[1][2], "c")
162
163#		results = ExampleClass().sampleClassMethodKwargs(c=9, b=8, a=7)
164#		self.assertEqual(results[0], ExampleClass, "Expected first parameter to be the class instance")
165#		self.assertEqual(results[1], 7)
166#		self.assertEqual(results[2], 8)
167#		self.assertEqual(results[3], 9)
168  
169	def testStaticMethod(self):
170		self.assertEqual(ExampleClass.sampleStaticMethod("a"), "a", "Expected static method to take the parameter I give as its first parameter")
171		
172	def testClassDefinitionOrder(self):
173		x = ExampleSubclassDefinedBeforeSuperclass()
174		self.assertEqual(x.someMethod(), "abc", "Expected someMethod to return 'abc'")
175
176# testClassVars
177class ExampleClass:
178	x = "test"
179
180	@classmethod
181	def sampleClassMethod(cls, arg):
182		return cls, arg
183	
184	@classmethod
185	def sampleClassMethodVarargs(cls, *args):
186		return cls, args
187	
188	@classmethod
189	def sampleClassMethodKwargs(cls, a=0, b=1, c=2):
190		return cls, a, b, c
191	
192	@staticmethod
193	def sampleStaticMethod(arg):
194		return arg
195
196# testInheritedProperties
197class ExampleParentClass:
198	x = "test"
199	
200	@classmethod
201	def sampleClassMethod(cls, arg):
202		return cls, arg
203
204class ExampleChildClass(ExampleParentClass):
205	pass
206
207
208# testInheritedPropertyObjects
209class ExampleParentObject:
210	x = []
211
212class ExampleChildObject(ExampleParentObject):
213	pass
214
215
216# testInheritedConstructors
217class ExampleGrandParentConstructor:
218	z = "grandparent"
219
220	def __init__(self):
221		self.r = "older"
222
223	def older(self):
224		self.w = 2
225
226class ExampleParentConstructor(ExampleGrandParentConstructor):
227	y = "parent"
228
229	def __init__(self):
230		self.x = "test"
231
232	def dosomething(self):
233		self.m = 1
234
235class ExampleChildConstructor(ExampleParentConstructor):
236	def __init__(self):
237		pass
238
239class ExampleChildNoConstructor(ExampleParentConstructor):
240	pass
241
242class ExampleChildExplicitConstructor(ExampleParentConstructor):
243	def __init__(self):
244		ExampleParentConstructor.__init__(self)
245
246class ExampleSubclassDefinedBeforeSuperclass(ExampleSuperclassDefinedAfterSubclass):
247	pass
248
249class ExampleSuperclassDefinedAfterSubclass:
250	def someMethod(self):
251		return 'abc'
252
253
254