2 # Copyright (C) 2001-2009, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
9 use Parrot::Test tests => 38;
14 t/pmc/namespace.t - test the NameSpace PMC as described in PDD 21.
18 % prove t/pmc/namespace-old.t
22 Test the NameSpace PMC as described in PDD21.
26 my $temp_a = "temp_a";
27 my $temp_b = "temp_b";
30 unlink( "$temp_a.pir", "$temp_a.pbc", "$temp_b.pir", "$temp_b.pbc" );
33 open my $S, '>', "$temp_a.pir" or die "Can't write $temp_a.pir";
38 $P0 = get_global ["Foo_A"], "A"
40 load_bytecode "temp_b.pbc"
48 open $S, '>', "$temp_b.pir" or die "Can't write $temp_b.pir";
52 $P0 = get_global ["Foo_B"], "B"
62 system(".$PConfig{slash}parrot$PConfig{exe} -o $temp_a.pbc $temp_a.pir");
63 system(".$PConfig{slash}parrot$PConfig{exe} -o $temp_b.pbc $temp_b.pir");
65 pir_output_is( <<'CODE', <<'OUTPUT', "HLL and load_bytecode - #38888" );
67 load_bytecode "temp_a.pbc"
76 pir_output_is( <<'CODE', <<'OUTPUT', "HLL and vars" );
77 # initial storage of _tcl global variable...
84 set_global '$variable', $P0
87 # start running HLL language
92 $P1 = get_root_namespace ['_tcl']
93 $P2 = $P1['$variable']
101 pir_output_is( <<'CODE', <<'OUTPUT', "HLL and namespace directives" );
103 .namespace ['Foo'; 'Bar']
109 $P1 = $P0.'get_name'()
120 my $temp_a = "temp_a.pir";
126 open $S, '>', $temp_a or die "Can't write $temp_a";
133 set_global '$whee', $P1
137 $P0 = get_global '$whee'
143 pir_output_is( <<'CODE', <<'OUTPUT', ":anon subs still get default namespace" );
147 load_bytecode 'temp_a.pir'
149 var = get_root_namespace
162 skip( "immediate test, doesn't with --run-pbc", 1 )
163 if ( exists $ENV{TEST_PROG_ARGS} and $ENV{TEST_PROG_ARGS} =~ m/--run-pbc/ );
165 pir_output_is( <<'CODE', <<'OUTPUT', "get_global in current" );
172 set_global ['sub_namespace'], "eek", $P1
175 .namespace [ 'sub_namespace' ]
178 $P1 = get_global 'eek'
186 open $S, '>', "$temp_b.pir" or die "Can't write $temp_b.pir";
195 pir_output_is( <<"CODE", <<'OUTPUT', "export_to -- success with array" );
199 load_bytecode "$temp_b.pir"
200 .local pmc nsr, nsa, nsb, ar
201 ar = new ['ResizableStringArray']
203 nsr = get_root_namespace
206 nsb."export_to"(nsa, ar)
218 pir_output_is( <<"CODE", <<'OUTPUT', "export_to -- success with hash (empty value)" );
222 load_bytecode "$temp_b.pir"
223 .local pmc nsr, nsa, nsb, ar
226 nsr = get_root_namespace
229 nsb."export_to"(nsa, ar)
241 pir_output_is( <<"CODE", <<'OUTPUT', "export_to -- success with hash (null value)" );
245 load_bytecode "$temp_b.pir"
246 .local pmc nsr, nsa, nsb, ar, nul
250 nsr = get_root_namespace
253 nsb."export_to"(nsa, ar)
265 pir_error_output_like( <<"CODE", <<'OUTPUT', "export_to -- success with hash (and value)" );
269 load_bytecode "$temp_b.pir"
270 .local pmc nsr, nsa, nsb, ar
272 ar["b_foo"] = "c_foo"
273 nsr = get_root_namespace
276 nsb."export_to"(nsa, ar)
287 Could not find sub b_foo/
291 pir_output_is( <<'CODE', <<'OUTPUT', "get_parent" );
294 ns = get_hll_namespace ['Foo']
295 ns = ns.'get_parent'()
306 pir_output_is( <<'CODE', <<'OUTPUT', "get_global [''], \"print_ok\"" );
317 $P0 = get_hll_global [''], 'print_ok'
325 pir_output_is( <<'CODE', <<'OUTPUT', "get_global with array ('')" );
336 $P0 = new ['ResizableStringArray']
338 $P0 = get_hll_global $P0, 'print_ok'
346 pir_output_is( <<'CODE', <<'OUTPUT', "get_global with empty array" );
357 $P0 = new ['ResizablePMCArray']
359 $P0 = get_hll_global $P0, 'print_ok'
367 pir_output_is( <<'CODE', <<'OUTPUT', "Namespace.get_global() with array ('')" );
378 $P1 = new ['ResizableStringArray']
380 $P1 = get_hll_global $P1, 'print_ok'
388 pir_output_is( <<'CODE', <<'OUTPUT', "Namespace introspection" );
391 f = get_hll_global ['Foo'], 'dummy'
396 .local pmc interp, ns_caller
398 ns_caller = interp['namespace'; 1]
406 pir_output_is( <<'CODE', <<'OUTPUT', "Nested namespace introspection" );
408 .local string no_symbol
411 foo_ns = get_hll_namespace [ 'Foo' ]
413 print "Found namespace: "
418 bar_ns = foo_ns.'find_namespace'( 'Bar' )
420 print "Found nested namespace: "
425 baz_ns = bar_ns.'find_namespace'( 'Baz' )
428 .local int is_defined
429 is_defined = defined baz_ns
430 if is_defined goto oops
434 print "Found non-null '"
441 a_sub = bar_ns.'find_sub'( 'a_sub' )
449 some_sub = bar_ns.'find_sub'( 'some_sub' )
450 no_symbol = 'some_sub'
452 is_defined = defined some_sub
453 if is_defined goto oops
456 a_var = bar_ns.'find_var'( 'a_var' )
462 some_var = bar_ns.'find_var'( 'some_var' )
463 no_symbol = 'some_var'
465 is_defined = defined some_var
466 if is_defined goto oops
475 .namespace [ 'Foo'; 'Bar' ]
479 some_var = new ['String']
480 some_var = 'a string PMC'
481 set_hll_global [ 'Foo'; 'Bar' ], 'a_var', some_var
485 Found nested namespace: Bar
487 Found var: a string PMC
490 pir_output_is( <<'CODE', <<'OUTPUT', 'get_root_namespace' );
493 root_ns = get_root_namespace
494 .local int is_defined
495 is_defined = defined root_ns
496 unless is_defined goto NO_NAMESPACE_FOUND
497 print "Found root namespace.\n"
501 Found root namespace.
504 pir_output_is( <<'CODE', <<'OUTPUT', 'root namespace is not a class' );
507 root_ns = get_root_namespace
508 .local pmc root_class
509 root_class = get_class root_ns
511 is_class = defined root_class
518 pir_output_is( <<'CODE', <<'OUTPUT', 'get_root_namespace "Foo"' );
521 foo_ns = get_root_namespace [ "foo" ]
522 .local int is_defined
523 is_defined = defined foo_ns
524 unless is_defined goto NO_NAMESPACE_FOUND
525 print "Found root namespace 'foo'.\n"
532 Found root namespace 'foo'.
535 pir_output_is( <<'CODE', <<'OUTPUT', 'get_root_namespace "Foo", not there' );
538 foo_ns = get_root_namespace [ "Foo" ]
539 .local int is_defined
540 is_defined = defined foo_ns
541 if is_defined goto NAMESPACE_FOUND
542 print "Didn't find root namespace 'Foo'.\n"
546 .namespace [ "NotFoo" ]
548 Didn't find root namespace 'Foo'.
551 my $create_nested_key = <<'CREATE_NESTED_KEY';
552 .sub create_nested_key
554 .param pmc other_names :slurpy
568 tail = create_nested_key(other_names :flat)
575 pir_output_is( <<"CODE", <<'OUTPUT', 'get_name()' );
580 key = create_nested_key( 'SingleName' )
581 print_namespace( key )
583 key = create_nested_key( 'Nested', 'Name', 'Space' )
584 print_namespace( key )
589 ns = key.'get_name'()
591 .local string ns_name
592 ns_name = join ';', ns
597 .sub 'print_namespace'
601 get_ns = get_global key, 'get_namespace'
606 .local pmc name_array
607 name_array = ns.'get_name'()
610 name = join ';', name_array
622 .namespace [ 'SingleName' ]
630 .namespace [ 'Nested'; 'Name'; 'Space' ]
640 parrot;Nested;Name;Space
644 pir_output_is( <<"CODE", <<'OUTPUT', 'add_namespace()' );
649 root_ns = get_namespace
652 child_ns = new ['NameSpace']
653 root_ns.'add_namespace'( 'Nested', child_ns )
655 .local pmc grandchild_ns
656 grandchild_ns = new ['NameSpace']
657 child_ns.'add_namespace'( 'Grandkid', grandchild_ns )
659 .local pmc great_grandchild_ns
660 great_grandchild_ns = new ['NameSpace']
661 grandchild_ns.'add_namespace'( 'Greatgrandkid', great_grandchild_ns )
664 parent = great_grandchild_ns.'get_parent'()
665 print_ns_name( parent )
667 parent = parent.'get_parent'()
668 print_ns_name( parent )
670 parent = parent.'get_parent'()
671 print_ns_name( parent )
678 ns = namespace.'get_name'()
680 .local string ns_name
681 ns_name = join ';', ns
686 parrot;Nested;Grandkid
691 pir_output_like( <<'CODE', <<'OUTPUT', 'add_namespace() with error' );
694 ns_child = subclass 'NameSpace', 'NSChild'
697 child = new ['NSChild']
700 root_ns = get_namespace
702 root_ns.'add_namespace'( 'Really nested', child )
705 not_a_ns = new ['Integer']
708 root_ns.'add_namespace'( 'Nested', not_a_ns )
713 .local string message
714 .get_results( exception )
721 /Invalid type \d+ in add_namespace\(\)/
724 pir_output_is( <<"CODE", <<'OUTPUT', 'add_sub()' );
729 report_ns = get_global 'report_namespace'
732 key = create_nested_key( 'Parent' )
735 parent_ns = get_namespace key
736 parent_ns.'add_sub'( 'report_ns', report_ns )
738 key = create_nested_key( 'Parent', 'Child' )
741 child_ns = get_namespace key
742 child_ns.'add_sub'( 'report_ns', report_ns )
744 .local pmc report_namespace
745 report_namespace = get_global [ 'Parent' ], 'report_ns'
748 report_namespace = get_global [ 'Parent'; 'Child' ], 'report_ns'
752 .sub 'report_namespace'
754 namespace = get_namespace
757 ns = namespace.'get_name'()
759 .local string ns_name
760 ns_name = join ';', ns
765 .namespace [ 'Parent' ]
770 .namespace [ 'Parent'; 'Child' ]
779 pir_output_like( <<'CODE', <<'OUTPUT', 'add_sub() with error' );
782 s_child = subclass 'Sub', 'SubChild'
785 child = new ['SubChild']
788 root_ns = get_namespace
790 root_ns.'add_sub'( 'child', child )
791 print "Added sub child\n"
793 child = new ['Coroutine']
794 root_ns.'add_sub'( 'coroutine', child )
795 print "Added coroutine\n"
798 root_ns.'add_sub'( 'eval', child )
802 not_a_sub = new ['Integer']
805 root_ns.'add_sub'( 'Nested', not_a_sub )
810 .local string message
811 .get_results( exception )
821 Invalid type \d+ in add_sub\(\)/
824 pir_output_is( <<"CODE", <<'OUTPUT', 'add_var()' );
837 key = create_nested_key( 'Parent' )
840 parent_ns = get_namespace key
841 parent_ns.'add_var'( 'foo', foo )
843 key = create_nested_key( 'Parent', 'Child' )
846 child_ns = get_namespace key
847 child_ns.'add_var'( 'bar', bar )
850 my_var = get_global [ 'Parent' ], 'foo'
855 my_var = get_global [ 'Parent'; 'Child' ], 'bar'
861 .namespace [ 'Parent' ]
866 .namespace [ 'Parent'; 'Child' ]
875 pir_output_is( <<"CODE", <<'OUTPUT', 'del_namespace()' );
880 root_ns = get_namespace
883 key = create_nested_key( 'Parent' )
886 child_ns = root_ns.'find_namespace'( key )
888 key = create_nested_key( 'Child' )
890 .local pmc grandchild_ns
891 grandchild_ns = child_ns.'find_namespace'( key )
893 child_ns.'del_namespace'( 'Child' )
895 key = create_nested_key( 'Child' )
897 grandchild_ns = child_ns.'find_namespace'( key )
898 if_null grandchild_ns, CHECK_SIBLING
899 print "Grandchild still exists\\n"
902 key = create_nested_key( 'Sibling' )
903 grandchild_ns = child_ns.'find_namespace'( key )
904 if_null grandchild_ns, DELETE_PARENT
905 print "Sibling not deleted\\n"
908 key = create_nested_key( 'Parent' )
909 root_ns.'del_namespace'( 'Parent' )
910 child_ns = root_ns.'find_namespace'( key )
911 if_null child_ns, CHECK_UNCLE
912 print "Child still exists\\n"
915 key = create_nested_key( 'FunUncle' )
916 grandchild_ns = root_ns.'find_namespace'( key )
917 if_null grandchild_ns, DELETE_PARENT
918 print "Fun uncle stuck around\\n"
923 .namespace [ 'FunUncle' ]
928 .namespace [ 'Parent' ]
933 .namespace [ 'Parent'; 'Child' ]
938 .namespace [ 'Parent'; 'Sibling' ]
944 Fun uncle stuck around
947 pir_output_like( <<'CODE', <<'OUTPUT', 'del_namespace() with error' );
953 not_a_ns = new ['ResizablePMCArray']
955 set_global 'Not_A_NS', not_a_ns
958 root_ns = get_namespace
959 delete_namespace( root_ns, 'dummy' )
960 delete_namespace( root_ns, 'Not_A_NS' )
963 .sub delete_namespace
967 root_ns.'del_namespace'( name )
971 .local string message
972 .get_results( exception )
980 /Invalid type \d+ for 'dummy' in del_namespace\(\)
981 Invalid type \d+ for 'Not_A_NS' in del_namespace\(\)/
984 pir_output_is( <<"CODE", <<'OUTPUT', 'del_sub()' );
987 root_ns = get_namespace
990 parent_ns = root_ns.'find_namespace'( 'Parent' )
991 parent_ns.'del_sub'( 'dummy' )
994 my_sub = get_global [ 'Parent' ], 'dummy'
995 if_null my_sub, PARENT_NO_DUMMY
996 print "Parent did not delete dummy\\n"
999 my_sub = get_global [ 'Parent' ], 'no_dummy'
1003 child_ns = parent_ns.'find_namespace'( 'Child' )
1004 child_ns.'del_sub'( 'dummy' )
1006 my_sub = get_global [ 'Parent'; 'Child' ], 'dummy'
1007 if_null my_sub, CHILD_NO_DUMMY
1008 print "Child did not delete dummy\\n"
1012 my_sub = get_global [ 'Parent'; 'Child' ], 'no_dummy'
1016 .namespace [ 'Parent' ]
1022 print "Parent is no dummy\\n"
1025 .namespace [ 'Parent'; 'Child' ]
1028 print "Dummy sub!\\n"
1032 print "Child is no dummy\\n"
1040 pir_output_like( <<'CODE', <<'OUTPUT', 'del_sub() with error' );
1043 not_a_ns = new ['ResizablePMCArray']
1045 set_global 'Not_A_Sub', not_a_ns
1048 root_ns = get_namespace
1050 push_eh _invalid_sub
1051 root_ns.'del_sub'( 'Not_A_Sub' )
1054 .local pmc exception
1055 .local string message
1056 .get_results( exception )
1064 /Invalid type \d+ for 'Not_A_Sub' in del_sub\(\)/
1067 pir_output_is( <<"CODE", <<'OUTPUT', 'del_var()' );
1070 foo = new ['String']
1074 bar = new ['String']
1077 set_global [ 'Parent' ], 'Foo', foo
1078 set_global [ 'Parent'; 'Child' ], 'Bar', bar
1081 root_ns = get_namespace
1083 .local pmc parent_ns
1084 parent_ns = root_ns.'find_namespace'( 'Parent' )
1085 parent_ns.'del_var'( 'Foo' )
1088 child_ns = parent_ns.'find_namespace'( 'Child' )
1089 child_ns.'del_var'( 'Bar' )
1092 my_var = get_global [ 'Parent' ], 'Foo'
1093 if_null my_var, TEST_CHILD_VAR
1094 print "Parent Foo exists: "
1099 my_var = get_global [ 'Parent'; 'Child' ], 'Bar'
1100 if_null my_var, ALL_DONE
1101 print "Child Bar exists: "
1108 .namespace [ 'Parent' ]
1113 .namespace [ 'Parent'; 'Child' ]
1118 pir_error_output_like( <<'CODE', <<'OUTPUT', 'overriding find_method()' );
1120 $P0 = newclass 'Override'
1121 $P1 = new ['Override']
1122 $P2 = find_method $P1, 'foo'
1125 .namespace [ 'Override' ]
1127 .sub 'find_method' :vtable
1128 .param string method
1129 say "Finding method"
1135 pir_output_is( <<'CODE', <<OUT, "iterate through a NameSpace PMC" );
1137 $P0 = new ['String']
1138 $P0 = "Ook...BANG!\n"
1139 set_root_global [ "DUMMY"; "X"; "Y" ], "Explosion", $P0
1141 $P1 = new ['Integer']
1143 set_root_global [ "DUMMY"; "X"; "Y" ], "T0", $P0
1145 .local pmc dummy_x_y_ns, it, res
1146 dummy_x_y_ns = get_root_namespace [ "DUMMY"; "X"; "Y" ]
1147 it = iter dummy_x_y_ns
1148 res = new ['ResizablePMCArray']
1150 unless it goto loop_end
1165 pir_error_output_like( <<'CODE', <<OUT, "NameSpace with no class" );
1167 $P1 = new ['NameSpace']
1169 tailcallmethod $P1, 'bob'
1172 /Null PMC access in get_string()/
1175 pir_output_is( <<'CODE', <<OUT, "iterate through a NameSpace PMC" );
1176 .namespace [ 'bar' ]
1180 res = new ['ResizablePMCArray']
1195 $S0 = join "\n", res
1211 pir_output_is( <<'CODE', <<'OUT', "make_namespace method");
1213 $P0 = split ';', 'perl6;Foo;Bar'
1214 $P1 = get_root_namespace
1215 $P2 = $P1.'make_namespace'($P0)
1216 $I0 = isa $P2, 'NameSpace'
1218 $P3 = get_root_namespace ['perl6';'Foo';'Bar']
1221 $I0 = issame $P2, $P3
1230 pir_error_output_like( <<'CODE', <<'OUT', 'adding :anon sub to a namespace, TT #56' );
1233 .const 'Sub' $P0 = 'bar'
1235 set_global 'ok', $P0
1236 $P1 = get_global 'ok'
1245 .sub 'nok' :anon :subid('bar')
1251 Could not find sub nok/
1255 pir_output_is( <<'CODE', <<'OUT', 'HLL_map on namespace', todo => 'TT #867');
1258 .sub 'foo' :anon :init
1259 $P1 = get_class 'NameSpace'
1260 $P2 = subclass $P1, 'BSNS'
1262 $P0.'hll_map'($P1, $P2)
1265 .namespace ['a';'b';'c']
1274 $P1 = get_hll_namespace ['a';'b';'c']
1285 # cperl-indent-level: 4
1288 # vim: expandtab shiftwidth=4: