2 # Copyright (C) 2007-2010, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
9 use Parrot::Test tests => 12;
13 t/pmc/exporter.t - test the Exporter PMC
17 % prove t/pmc/exporter.t
21 Tests the Exporter PMC.
25 # L<PDD17/Exporter PMC>
26 pir_output_is( <<'CODE', <<'OUT', 'new' );
28 $P0 = new ['Exporter']
29 say "ok 1 - $P0 = new ['Exporter']"
31 # Most uses of export are short-lived, so use a explicit sweep
32 # here to ensure coverage of the mark vtable.
35 $I0 = isa $P0, 'Exporter'
39 say "ok 2 - isa $P0, 'Exporter'"
42 ok 1 - $P0 = new ['Exporter']
43 ok 2 - isa $P0, 'Exporter'
46 pir_output_is( <<'CODE', <<'OUT', 'source' );
48 $P0 = new ['Exporter']
53 say 'ok 1 - source() returns PMCNULL upon Exporter init'
55 # get a NameSpace PMC for testing
56 # TT #1233 replace with make_namespace, when implemented
58 ns = get_namespace ['Eponymous']
62 if $P1 == 'Eponymous' goto ok_2
65 say 'ok 2 - source() with args sets source namespace'
75 say 'ok 3 - source() with too many args fails'
83 say 'ok 4 - source() with non-namespace arg throws exception'
87 # TT #1233 replace with make_namespace, when implemented
88 .namespace ['Eponymous']
89 .sub 'Eponymous' :anon
92 ok 1 - source() returns PMCNULL upon Exporter init
93 ok 2 - source() with args sets source namespace
94 ok 3 - source() with too many args fails
95 ok 4 - source() with non-namespace arg throws exception
98 pir_output_is( <<'CODE', <<'OUT', 'destination' );
100 $P0 = new ['Exporter']
101 $P1 = $P0.'destination'()
102 unless null $P1 goto ok_1
105 say 'ok 1 - destination() with no args returns destination namespace'
108 if $P1 == $P99 goto ok_2
111 say 'ok 2 - ...which is current namespace at first'
113 # get a NameSpace PMC for testing
114 # TT #1233 replace with make_namespace, when implemented
116 ns = get_namespace ['Eponymous']
118 $P0.'destination'(ns)
119 $P1 = $P0.'destination'()
120 if $P1 == 'Eponymous' goto ok_3
123 say 'ok 3 - destination() with args sets destination namespace'
128 $P0.'destination'(ns, $P1)
133 say 'ok 4 - destination() with too many args fails'
136 $P0.'destination'('foo')
141 say 'ok 5 - destination() with non-namespace arg throws exception'
145 # TT #1233 replace with make_namespace, when implemented
146 .namespace ['Eponymous']
147 .sub 'Eponymous' :anon
150 ok 1 - destination() with no args returns destination namespace
151 ok 2 - ...which is current namespace at first
152 ok 3 - destination() with args sets destination namespace
153 ok 4 - destination() with too many args fails
154 ok 5 - destination() with non-namespace arg throws exception
157 pir_output_is( <<'CODE', <<'OUT', 'globals' );
159 $P0 = new ['Exporter']
161 $P1 = $P0.'globals'()
166 say 'ok 1 - globals() returns PMCNULL upon Exporter init'
168 # create an array to store globals in
169 $P99 = new ['ResizableStringArray']
172 $P1 = $P0.'globals'()
177 say 'ok 2 - globals() with empty array arg sets PMCNULL'
183 $P1 = $P0.'globals'()
184 $I0 = does $P1, 'hash'
187 unless $I0 == 1 goto nok_3
188 unless $I1 == $I99 goto nok_3
189 unless $I1 == 2 goto ok_3
190 $I0 = exists $P1['Prince']
191 unless $I0 goto nok_3
192 $I0 = exists $P1['Alex']
197 say 'ok 3 - globals() with array arg sets globals hash (hash with two keys)'
199 # create a hash to store globals in
203 $P1 = $P0.'globals'()
208 say 'ok 4 - globals() with empty hash arg sets PMCNULL'
214 $P1 = $P0.'globals'()
217 unless $I1 == $I99 goto nok_5
218 unless $I1 == 2 goto nok_5
219 $I0 = exists $P1['Prince']
220 unless $I0 goto nok_5
221 $I0 = exists $P1['Alex']
222 unless $I0 goto nok_5
227 say 'ok 5 - globals() with hash arg sets globals hash (hash with two keys)'
233 $P0.'globals'($P99, $P98)
238 say 'ok 6 - globals() with too many args fails'
242 ok 1 - globals() returns PMCNULL upon Exporter init
243 ok 2 - globals() with empty array arg sets PMCNULL
244 ok 3 - globals() with array arg sets globals hash (hash with two keys)
245 ok 4 - globals() with empty hash arg sets PMCNULL
246 ok 5 - globals() with hash arg sets globals hash (hash with two keys)
247 ok 6 - globals() with too many args fails
250 pir_error_output_like( <<'CODE', <<'OUT', 'import - no args' );
252 $P0 = new ['Exporter']
255 say 'ok 1 - import() with no args throws an exception'
259 /^source namespace not set\n/
262 pir_output_is( <<'CODE', <<'OUT', 'import - same source and destination namespaces' );
264 .local pmc exporter, src
268 exporter = new ['Exporter']
269 exporter.'import'( src :named('source'), src :named('destination'), 'plan ok' :named('globals') )
288 pir_output_is( <<'CODE', <<'OUT', 'import - globals as string' );
290 load_bytecode 'Test/More.pbc'
291 .local pmc exporter, src
293 src = get_namespace [ 'Test'; 'More' ]
295 exporter = new ['Exporter']
296 exporter.'import'( src :named('source'), 'plan ok' :named('globals') )
305 pir_output_is( <<'CODE', <<'OUT', 'import - globals with source passed separately' );
307 load_bytecode 'Test/More.pbc'
308 .local pmc exporter, src
310 src = get_namespace [ 'Test'; 'More' ]
312 exporter = new ['Exporter']
313 exporter.'source'( src )
314 exporter.'import'( 'plan ok' :named('globals') )
323 pir_output_is( <<'CODE', <<'OUT', 'import - globals as array' );
325 load_bytecode 'Test/More.pbc'
326 .local pmc exporter, src, globals
328 src = get_namespace [ 'Test'; 'More' ]
329 globals = new ['ResizableStringArray']
333 exporter = new ['Exporter']
334 exporter.'import'( src :named('source'), globals :named('globals') )
343 pir_output_is( <<'CODE', <<'OUT', 'import - globals as hash - null + empty string' );
345 load_bytecode 'Test/More.pbc'
346 .local pmc exporter, src, globals, nul
349 src = get_namespace [ 'Test'; 'More' ]
350 globals = new ['Hash']
354 exporter = new ['Exporter']
355 exporter.'import'( src :named('source'), globals :named('globals') )
364 pir_output_is( <<'CODE', <<'OUT', 'import - globals as hash - with dest names (latin)' );
366 load_bytecode 'Test/More.pbc'
367 .local pmc exporter, src, globals
369 src = get_namespace [ 'Test'; 'More' ]
370 globals = new ['Hash']
371 globals['plan'] = 'consilium'
372 globals['ok'] = 'rectus'
374 exporter = new ['Exporter']
375 exporter.'import'( src :named('source'), globals :named('globals') )
384 pir_output_is( <<'CODE', <<'OUT', 'import - globals with destination' );
386 load_bytecode 'Test/More.pbc'
387 .local pmc exporter, src, dest, globals
389 src = get_namespace [ 'Test'; 'More' ]
390 dest = get_namespace ['foo']
391 globals = new ['ResizableStringArray']
395 exporter = new ['Exporter']
396 exporter.'import'( src :named('source'), dest :named('destination'), globals :named('globals') )
398 $P0 = get_global ['foo'], 'bar'
412 # Test exporting mmd subs: TT #1205
413 # https://trac.parrot.org/parrot/ticket/1205
417 # cperl-indent-level: 4
420 # vim: expandtab shiftwidth=4: