2 # Copyright (C) 2007, 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 $I0 = isa $P0, 'Exporter'
35 say "ok 2 - isa $P0, 'Exporter'"
38 ok 1 - $P0 = new ['Exporter']
39 ok 2 - isa $P0, 'Exporter'
42 pir_output_is( <<'CODE', <<'OUT', 'source' );
44 $P0 = new ['Exporter']
49 say 'ok 1 - source() returns PMCNULL upon Exporter init'
51 # get a NameSpace PMC for testing
52 # TT #1233 replace with make_namespace, when implemented
54 ns = get_namespace ['Eponymous']
58 if $P1 == 'Eponymous' goto ok_2
61 say 'ok 2 - source() with args sets source namespace'
71 say 'ok 3 - source() with too many args fails'
79 say 'ok 4 - source() with non-namespace arg throws exception'
83 # TT #1233 replace with make_namespace, when implemented
84 .namespace ['Eponymous']
85 .sub 'Eponymous' :anon
88 ok 1 - source() returns PMCNULL upon Exporter init
89 ok 2 - source() with args sets source namespace
90 ok 3 - source() with too many args fails
91 ok 4 - source() with non-namespace arg throws exception
94 pir_output_is( <<'CODE', <<'OUT', 'destination' );
96 $P0 = new ['Exporter']
97 $P1 = $P0.'destination'()
98 unless null $P1 goto ok_1
101 say 'ok 1 - destination() with no args returns destination namespace'
104 if $P1 == $P99 goto ok_2
107 say 'ok 2 - ...which is current namespace at first'
109 # get a NameSpace PMC for testing
110 # TT #1233 replace with make_namespace, when implemented
112 ns = get_namespace ['Eponymous']
114 $P0.'destination'(ns)
115 $P1 = $P0.'destination'()
116 if $P1 == 'Eponymous' goto ok_3
119 say 'ok 3 - destination() with args sets destination namespace'
124 $P0.'destination'(ns, $P1)
129 say 'ok 4 - destination() with too many args fails'
132 $P0.'destination'('foo')
137 say 'ok 5 - destination() with non-namespace arg throws exception'
141 # TT #1233 replace with make_namespace, when implemented
142 .namespace ['Eponymous']
143 .sub 'Eponymous' :anon
146 ok 1 - destination() with no args returns destination namespace
147 ok 2 - ...which is current namespace at first
148 ok 3 - destination() with args sets destination namespace
149 ok 4 - destination() with too many args fails
150 ok 5 - destination() with non-namespace arg throws exception
153 pir_output_is( <<'CODE', <<'OUT', 'globals' );
155 $P0 = new ['Exporter']
157 $P1 = $P0.'globals'()
162 say 'ok 1 - globals() returns PMCNULL upon Exporter init'
164 # create an array to store globals in
165 $P99 = new ['ResizableStringArray']
168 $P1 = $P0.'globals'()
173 say 'ok 2 - globals() with empty array arg sets PMCNULL'
179 $P1 = $P0.'globals'()
180 $I0 = does $P1, 'hash'
183 unless $I0 == 1 goto nok_3
184 unless $I1 == $I99 goto nok_3
185 unless $I1 == 2 goto ok_3
186 $I0 = exists $P1['Prince']
187 unless $I0 goto nok_3
188 $I0 = exists $P1['Alex']
193 say 'ok 3 - globals() with array arg sets globals hash (hash with two keys)'
195 # create a hash to store globals in
199 $P1 = $P0.'globals'()
204 say 'ok 4 - globals() with empty hash arg sets PMCNULL'
210 $P1 = $P0.'globals'()
213 unless $I1 == $I99 goto nok_5
214 unless $I1 == 2 goto nok_5
215 $I0 = exists $P1['Prince']
216 unless $I0 goto nok_5
217 $I0 = exists $P1['Alex']
218 unless $I0 goto nok_5
223 say 'ok 5 - globals() with hash arg sets globals hash (hash with two keys)'
229 $P0.'globals'($P99, $P98)
234 say 'ok 6 - globals() with too many args fails'
238 ok 1 - globals() returns PMCNULL upon Exporter init
239 ok 2 - globals() with empty array arg sets PMCNULL
240 ok 3 - globals() with array arg sets globals hash (hash with two keys)
241 ok 4 - globals() with empty hash arg sets PMCNULL
242 ok 5 - globals() with hash arg sets globals hash (hash with two keys)
243 ok 6 - globals() with too many args fails
246 pir_error_output_like( <<'CODE', <<'OUT', 'import - no args' );
248 $P0 = new ['Exporter']
251 say 'ok 1 - import() with no args throws an exception'
255 /^source namespace not set\n/
258 pir_output_is( <<'CODE', <<'OUT', 'import - same source and destination namespaces' );
260 .local pmc exporter, src
264 exporter = new ['Exporter']
265 exporter.'import'( src :named('source'), src :named('destination'), 'plan ok' :named('globals') )
284 pir_output_is( <<'CODE', <<'OUT', 'import - globals as string' );
286 load_bytecode 'Test/More.pbc'
287 .local pmc exporter, src
289 src = get_namespace [ 'Test'; 'More' ]
291 exporter = new ['Exporter']
292 exporter.'import'( src :named('source'), 'plan ok' :named('globals') )
301 pir_output_is( <<'CODE', <<'OUT', 'import - globals with source passed separately' );
303 load_bytecode 'Test/More.pbc'
304 .local pmc exporter, src
306 src = get_namespace [ 'Test'; 'More' ]
308 exporter = new ['Exporter']
309 exporter.'source'( src )
310 exporter.'import'( 'plan ok' :named('globals') )
319 pir_output_is( <<'CODE', <<'OUT', 'import - globals as array' );
321 load_bytecode 'Test/More.pbc'
322 .local pmc exporter, src, globals
324 src = get_namespace [ 'Test'; 'More' ]
325 globals = new ['ResizableStringArray']
327 globals = push 'plan'
329 exporter = new ['Exporter']
330 exporter.'import'( src :named('source'), globals :named('globals') )
339 pir_output_is( <<'CODE', <<'OUT', 'import - globals as hash - null + empty string' );
341 load_bytecode 'Test/More.pbc'
342 .local pmc exporter, src, globals, nul
345 src = get_namespace [ 'Test'; 'More' ]
346 globals = new ['Hash']
350 exporter = new ['Exporter']
351 exporter.'import'( src :named('source'), globals :named('globals') )
360 pir_output_is( <<'CODE', <<'OUT', 'import - globals as hash - with dest names (latin)' );
362 load_bytecode 'Test/More.pbc'
363 .local pmc exporter, src, globals
365 src = get_namespace [ 'Test'; 'More' ]
366 globals = new ['Hash']
367 globals['plan'] = 'consilium'
368 globals['ok'] = 'rectus'
370 exporter = new ['Exporter']
371 exporter.'import'( src :named('source'), globals :named('globals') )
380 pir_output_is( <<'CODE', <<'OUT', 'import - globals with destination' );
382 load_bytecode 'Test/More.pbc'
383 .local pmc exporter, src, dest, globals
385 src = get_namespace [ 'Test'; 'More' ]
386 dest = get_namespace ['foo']
387 globals = new ['ResizableStringArray']
389 globals = push 'plan'
391 exporter = new ['Exporter']
392 exporter.'import'( src :named('source'), dest :named('destination'), globals :named('globals') )
394 $P0 = get_global ['foo'], 'bar'
408 # TODO: Test exporting mmd subs: TT #1205
409 # https://trac.parrot.org/parrot/ticket/1205
413 # cperl-indent-level: 4
416 # vim: expandtab shiftwidth=4: