2 # Copyright (C) 2007-2010, Parrot Foundation.
7 t/oo/new.t - Test OO instantiation
15 Tests OO features related to instantiating new objects.
20 .include 'except_types.pasm'
21 .include 'test_more.pir'
24 instantiate_from_class_object()
25 manually_create_anonymous_class_object()
26 manually_create_named_class_object()
27 instantiate_from_class_object_method()
28 instantiate_from_string_name()
29 instantiate_from_string_register_name()
30 instantiate_from_string_PMC_name()
31 instantiate_from_key_name()
32 instantiate_from_key_PMC_name()
33 create_and_instantiate_from_array_of_names()
34 only_string_arrays_work_for_creating_classes()
35 instantiate_from_class_object_with_init()
36 instantiate_from_string_name_with_init()
37 instantiate_from_string_register_name_with_init()
38 instantiate_from_string_PMC_name_with_init()
39 instantiate_from_array_of_names_with_init()
40 instantiate_from_key_name_with_init()
41 create_class_namespace_initializer()
42 regression_test_instantiate_class_within_different_namespace()
43 get_class_retrieves_a_high_level_class_object()
44 get_class_retrieves_a_proxy_class_object()
45 get_class_retrieves_a_class_object_that_doesnt_exist()
46 instantiate_class_from_invalid_key()
57 # Set up local variables
59 .local string class_name
62 $P0 = split ' ', in_str
70 if $I0 == 0 goto BEGIN_TEST
75 concat class_name, ';'
76 concat class_name, $S1
82 .local string typeof_message
83 typeof_message = concat 'New instance is of type: ', class_name
85 is($S1, class_name, typeof_message)
89 .local string keypmc_message
90 $S2 = get_repr key_pmc
91 keypmc_message = concat 'The object isa ', $S2
92 $I2 = isa obj, key_pmc
93 ok($I2, keypmc_message)
95 unless $I1 == 1 goto END_TEST
96 isa_ok(obj, class_name)
103 #############################################################################
106 .sub instantiate_from_class_object
107 ok(1, "Instantiate from class object")
108 $P1 = newclass 'Foo1'
110 is($S1, 'Class', '`newclass "Foo"` creates a Class PMC')
114 _test_instance($P2, 'Foo1')
118 .sub manually_create_anonymous_class_object
119 ok(2, "Manually create anonymous class object")
122 is($S1, 'Class', 'New anonymous class creates a Class PMC')
127 is($S1, '', 'New instance is of type ""')
128 isa_ok($P2, 'Object')
131 is($I3, 0, '"isa" will not match an empty type')
133 is($I3, 0, '"isa" will not match a random type')
137 .sub manually_create_named_class_object
138 ok(3, "Manually create named class object")
142 is($S1, 'Class', 'new named class creates a "Class" PMC')
146 _test_instance($P2, 'Foo2')
150 .sub instantiate_from_class_object_method
151 ok(4, "Instantiate from class object 'new' method")
152 $P1 = newclass 'Foo3'
155 _test_instance($P2, 'Foo3')
159 .sub instantiate_from_string_name
160 ok(5, "Instantiate from string name")
161 $P1 = newclass 'Foo4'
164 _test_instance($P2, 'Foo4')
168 .sub instantiate_from_string_register_name
169 ok(6, "Instantiate from string register name")
170 $P1 = newclass 'Foo5'
174 _test_instance($P2, 'Foo5')
178 .sub instantiate_from_string_PMC_name
179 ok(7, "Instantiate from string PMC name")
180 $P1 = newclass 'Foo6'
185 _test_instance($P2, 'Foo6')
189 .sub instantiate_from_key_name
190 ok(8, "Instantiate from Key name")
191 $P1 = newclass ['Foo';'Bar1']
193 is($S1, 'Class', "`newclass ['Foo';'Bar1']` creates a Class PMC")
197 _test_instance($P2, 'Foo Bar1')
201 .sub instantiate_from_key_PMC_name
202 ok(9, "Instantiate from Key PMC name")
203 $P1 = newclass ['Foo';'Bar2']
212 _test_instance($P2, 'Foo Bar2')
216 .sub create_and_instantiate_from_array_of_names
217 ok(10, "Create and instantiate from ResizableStringArray")
218 $P0 = split ' ', 'Foo Bar3'
221 is($S1, 'Class', "`newclass some_string_array` creates a Class PMC")
225 _test_instance($P2, 'Foo Bar3')
229 .sub only_string_arrays_work_for_creating_classes
230 ok(11, 'Create a class via a ResizablePMCArray')
232 .local string message
233 $P0 = new 'ResizablePMCArray'
242 eh = new 'ExceptionHandler'
243 eh.'handle_types'(.EXCEPTION_INVALID_OPERATION)
253 .get_results(exception)
254 message = exception['message']
259 nok($I0, "Exception caught for ...")
260 is(message, 'Invalid class name key in init_pmc for Class', 'Invalid class name key')
264 .sub instantiate_from_class_object_with_init
265 ok(12, 'Instantiate from Class object, with init')
266 $P1 = newclass 'Foo7'
267 addattribute $P1, 'data'
270 $P4 = 'data for Foo7'
274 _test_instance($P2, 'Foo7')
276 $P5 = getattribute $P2, 'data'
277 is($P5, 'data for Foo7', 'class attribute retrieved via the instance')
281 .sub instantiate_from_string_name_with_init
282 ok(13, 'Instantiate from string name, with init')
283 $P1 = newclass 'Foo8'
284 addattribute $P1, 'data'
287 $P4 = 'data for Foo8'
290 $P2 = new 'Foo8', $P3
291 _test_instance($P2, 'Foo8')
293 $P5 = getattribute $P2, 'data'
294 is($P5, 'data for Foo8', 'class attribute retrieved via the instance')
298 .sub instantiate_from_string_register_name_with_init
299 ok(14, 'Instantiate from string register name, with init')
300 $P1 = newclass 'Foo9'
301 addattribute $P1, 'data'
304 $P4 = 'data for Foo9'
309 _test_instance($P2, 'Foo9')
311 $P5 = getattribute $P2, 'data'
312 is($P5, 'data for Foo9', 'class attribute retrieved via the instance')
316 .sub instantiate_from_string_PMC_name_with_init
317 ok(15, 'Instantiate from string PMC name, with init')
318 $P1 = newclass 'Foo10'
319 addattribute $P1, 'data'
322 $P4 = 'data for Foo10'
328 _test_instance($P2, 'Foo10')
330 $P5 = getattribute $P2, 'data'
331 is($P5, 'data for Foo10', 'class attribute retrieved via the instance')
335 .sub instantiate_from_array_of_names_with_init
336 ok(16, 'Instantiate from string array, with init')
337 $P0 = split ' ', 'Foo Bar5'
339 addattribute $P1, 'data'
342 $P4 = 'data for Foo;Bar5'
348 _test_instance($P2, 'Foo Bar5')
350 $P5 = getattribute $P2, 'data'
351 is($P5, 'data for Foo;Bar5', 'class attribute retrieved via the instance')
355 .sub instantiate_from_key_name_with_init
356 ok(17, 'Instantiate from Key name, with init')
357 $P1 = newclass ['Foo';'Bar6']
358 addattribute $P1, 'data'
362 $P4 = 'data for Foo;Bar6'
365 $P2 = new ['Foo';'Bar6'], $P3
366 _test_instance($P2, 'Foo Bar6')
368 $P5 = getattribute $P2, 'data'
369 is($P5, 'data for Foo;Bar6', 'class attribute retrieved via the instance')
373 .sub create_class_namespace_initializer
375 ns = get_namespace ['Foo';'Bar7']
376 $P0 = new 'Class', ns
378 $P1 = new ['Foo';'Bar7']
380 is($S0, 'foo_bar7 blue', 'Create class namespace initializer')
383 .namespace [ 'Foo';'Bar7' ]
385 .return('foo_bar7 blue')
391 .sub regression_test_instantiate_class_within_different_namespace
392 $P0 = newclass 'Foo11'
393 $P0 = newclass 'Bar11'
397 is($S0, 'foo11 blue bar11 blue', 'Regression test: instantiate class within different namespace')
400 .namespace [ 'Foo11' ]
404 $S0 = concat 'foo11 blue ', $S0
408 .namespace [ 'Bar11' ]
410 .return('bar11 blue')
416 .sub get_class_retrieves_a_high_level_class_object
417 ok(20, 'get_class retrieves a high level class object')
418 $P0 = newclass 'Foo12'
420 is($S1, 'Class',"`newclass 'Foo12' returns a Class PMC`")
422 $P1 = get_class 'Foo12'
424 is($S1, 'Class',"`get_class 'Foo12' returns a Class PMC`")
427 _test_instance($P2, 'Foo12')
431 .sub get_class_retrieves_a_proxy_class_object
432 ok(21, 'get_class retrieves a proxy class object')
433 $P1 = get_class 'String'
435 is($S1, 'PMCProxy', "`get_class 'String'` returns a PMCProxy PMC")
439 is($S1, 'String', 'Instantiating the proxy returns a String PMC')
443 .sub get_class_retrieves_a_class_object_that_doesnt_exist
444 ok(22, 'get_class retrieves a class object that does not exist')
445 .local int murple_not_defined
446 murple_not_defined = 1
447 $P1 = get_class 'Murple'
448 if null $P1 goto not_defined
449 murple_not_defined = 0
452 ok(murple_not_defined, '"Murple" class is not defined')
456 .sub instantiate_class_from_invalid_key
457 ok(23, 'Instantiate a class from invalid Key PMC')
459 .local string message
462 eh = new 'ExceptionHandler'
463 eh.'handle_types'(.EXCEPTION_NO_CLASS)
467 $P0 = new [ 'Foo'; 'Bar'; 'Baz' ]
473 .get_results(exception)
474 message = exception['message']
478 nok($I0, 'Exception caught for ...')
479 is(message, "Class '[ 'Foo' ; 'Bar' ; 'Baz' ]' not found", 'Class not found')
487 # vim: expandtab shiftwidth=4 ft=pir: