Ticket #1386: exporter.t.patch
File exporter.t.patch, 15.9 KB (added by kurahaupo, 12 years ago) |
---|
-
t/pmc/exporter.t
1 #!p erl2 # Copyright (C) 200 7, Parrot Foundation.1 #!parrot 2 # Copyright (C) 2009, Parrot Foundation. 3 3 # $Id$ 4 4 5 use strict;6 use warnings;7 use lib qw( . lib ../lib ../../lib );8 use Test::More;9 use Parrot::Test tests => 12;10 11 5 =head1 NAME 12 6 13 7 t/pmc/exporter.t - test the Exporter PMC … … 23 17 =cut 24 18 25 19 # L<PDD17/Exporter PMC> 26 pir_output_is( <<'CODE', <<'OUT', 'new' ); 27 .sub 'test' :main 20 21 .sub 'main' :main 22 .include 'test_more.pir' 23 plan(28) 24 25 # 26 # Arguably the rest of this file is moot, since if we can import test_more.pir 27 # and use it, exporter.pir has already been tested. However our point is to 28 # ensure exhausive testing, and to document any failures in a sensible manner, 29 # so we press on... 30 # 31 32 test_new_exporter() # 5 tests 33 test_source() # 4 tests 34 test_destination() # 5 tests 35 36 $P0 = get_global ['test_globals'], 'test' 37 $P0() # 6 tests 38 39 $P0 = get_global ['test_import_without_args'], 'test' 40 $P0() # 1 test 41 42 $P0 = get_global ['test_import_with_s_d_g'], 'test' 43 $P0() # 1 test 44 45 $P0 = get_global ['test_import_with_s_gs'], 'test' 46 $P0() # 1 test 47 48 $P0 = get_global ['test_import_with_gs'], 'test' 49 $P0() # 1 test 50 51 $P0 = get_global ['test_import_with_s_ga'], 'test' 52 $P0() # 1 test 53 54 $P0 = get_global ['test_import_with_s_gh'], 'test' 55 $P0() # 1 test 56 57 $P0 = get_global ['test_import_and_call'], 'test' 58 $P0() # 1 test 59 60 $P0 = get_global ['test_import_and_ind_call'], 'test' 61 $P0() # 1 test 62 63 64 .end 65 66 .sub 'test_new_exporter' 28 67 $P0 = new ['Exporter'] 29 say "ok 1 - $P0 = new ['Exporter']" 68 $I0 = isnull $P0 69 is( $I0, 0, 'Constructed "Exporter" object' ) 30 70 31 71 $I0 = isa $P0, 'Exporter' 32 if $I0 goto ok_2 33 print 'not ' 34 ok_2: 35 say "ok 2 - isa $P0, 'Exporter'" 72 ok( $I0, '... isa "Exporter"' ) 73 74 $I0 = can $P0, 'source' 75 ok( $I0, '... can "source"' ) 76 77 $I0 = can $P0, 'destination' 78 ok( $I0, '... can "destination"' ) 79 80 $I0 = can $P0, 'import' 81 ok( $I0, '... can "import"' ) 36 82 .end 37 CODE38 ok 1 - $P0 = new ['Exporter']39 ok 2 - isa $P0, 'Exporter'40 OUT41 83 42 pir_output_is( <<'CODE', <<'OUT', 'source' ); 43 .sub 'test' :main 84 .sub 'test_source' 44 85 $P0 = new ['Exporter'] 45 86 $P1 = $P0.'source'() 46 if null $P1 goto ok_1 47 print 'not ' 48 ok_1: 49 say 'ok 1 - source() returns PMCNULL upon Exporter init' 87 $I0 = isnull $P1 88 ok( $I0, 'source() returns PMCNULL upon Exporter init' ) 50 89 51 90 # get a NameSpace PMC for testing 52 # TT #1233 replace with make_namespace, when implemented 53 .local pmc ns 54 ns = get_namespace ['Eponymous'] 91 # RT #46859 replace with make_namespace, when implemented 92 $P98 = get_namespace ['Eponymous'] 55 93 56 $P0.'source'( ns)94 $P0.'source'($P98) 57 95 $P1 = $P0.'source'() 58 if $P1 == 'Eponymous' goto ok_2 59 print 'not ' 60 ok_2: 61 say 'ok 2 - source() with args sets source namespace' 96 is( $P1, 'Eponymous', '... with args sets source namespace' ) 62 97 63 $P1 = clone ns64 98 $P1 = clone $P98 99 $I0 = 1 65 100 push_eh ok_3 66 $P0.'source'(ns, $P1) 101 $P0.'source'($P98, $P1) 102 $I0 = 0 103 ok_3: 67 104 pop_eh 105 ok( $I0, '... with too many args throws exception' ) 68 106 69 print 'not ' 70 ok_3: 71 say 'ok 3 - source() with too many args fails' 72 107 $I0 = 1 73 108 push_eh ok_4 74 109 $P0.'source'('foo') 75 pop_eh 76 print 'not ' 110 $I0 = 0 77 111 78 112 ok_4: 79 say 'ok 4 - source() with non-namespace arg throws exception' 113 pop_eh 114 ok( $I0, '... with non-namespace arg throws exception' ) 80 115 .end 81 116 82 83 # TT #1233 replace with make_namespace, when implemented 84 .namespace ['Eponymous'] 85 .sub 'Eponymous' :anon 86 .end 87 CODE 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 92 OUT 93 94 pir_output_is( <<'CODE', <<'OUT', 'destination' ); 95 .sub 'test' :main 117 .sub 'test_destination' 96 118 $P0 = new ['Exporter'] 97 119 $P1 = $P0.'destination'() 98 unless null $P1 goto ok_1 99 print 'not ' 100 ok_1: 101 say 'ok 1 - destination() with no args returns destination namespace' 120 $I0 = isnull $P1 121 is( $I0, 0, 'destination() with no args returns destination namespace' ) 102 122 103 123 $P99 = get_namespace 104 if $P1 == $P99 goto ok_2 105 print 'not ' 106 ok_2: 107 say 'ok 2 - ...which is current namespace at first' 124 is( $P1, $P99, '... which is current namespace at first' ) 108 125 109 126 # get a NameSpace PMC for testing 110 # TT #1233 replace with make_namespace, when implemented 111 .local pmc ns 112 ns = get_namespace ['Eponymous'] 127 # RT #46859 replace with make_namespace, when implemented 128 $P98 = get_namespace ['Eponymous'] 113 129 114 $P0.'destination'( ns)130 $P0.'destination'($P98) 115 131 $P1 = $P0.'destination'() 116 if $P1 == 'Eponymous' goto ok_3 117 print 'not ' 118 ok_3: 119 say 'ok 3 - destination() with args sets destination namespace' 132 is( $P1, 'Eponymous', '... with args sets destination namespace' ) 120 133 121 $P1 = clone ns122 134 $P1 = clone $P98 135 $I0 = 1 123 136 push_eh ok_4 124 $P0.'destination'(ns, $P1) 137 $P0.'destination'($P98, $P1) 138 $I0 = 0 139 ok_4: 125 140 pop_eh 141 ok( $I0, '... with too many args throws exception' ) 126 142 127 print 'not ' 128 ok_4: 129 say 'ok 4 - destination() with too many args fails' 130 143 $I0 = 1 131 144 push_eh ok_5 132 145 $P0.'destination'('foo') 146 $I0 = 0 147 ok_5: 133 148 pop_eh 134 print 'not '149 ok( $I0, '... with non-namespace arg throws exception' ) 135 150 136 ok_5:137 say 'ok 5 - destination() with non-namespace arg throws exception'138 151 .end 139 152 140 141 # TT #1233 replace with make_namespace, when implemented 153 # RT #46859 replace with make_namespace, when implemented 142 154 .namespace ['Eponymous'] 143 155 .sub 'Eponymous' :anon 144 156 .end 145 CODE146 ok 1 - destination() with no args returns destination namespace147 ok 2 - ...which is current namespace at first148 ok 3 - destination() with args sets destination namespace149 ok 4 - destination() with too many args fails150 ok 5 - destination() with non-namespace arg throws exception151 OUT152 157 153 pir_output_is( <<'CODE', <<'OUT', 'globals' ); 154 .sub 'test' :main 158 159 .namespace ['test_globals'] 160 .sub 'test' 155 161 $P0 = new ['Exporter'] 156 162 157 163 $P1 = $P0.'globals'() 158 164 $I0 = isnull $P1 159 if $I0 goto ok_1 160 print 'not ' 161 ok_1: 162 say 'ok 1 - globals() returns PMCNULL upon Exporter init' 165 ok( $I0, 'globals() returns PMCNULL upon Exporter init' ) 163 166 164 167 # create an array to store globals in 165 168 $P99 = new ['ResizableStringArray'] … … 167 170 $P0.'globals'($P99) 168 171 $P1 = $P0.'globals'() 169 172 $I0 = isnull $P1 170 if $I0 goto ok_2 171 print 'not ' 172 ok_2: 173 say 'ok 2 - globals() with empty array arg sets PMCNULL' 173 ok( $I0, '... with empty array arg sets PMCNULL' ) 174 174 175 175 $P99 = push 'Alex' 176 176 $P99 = push 'Prince' 177 177 178 $I9 = 1 178 179 $P0.'globals'($P99) 179 180 $P1 = $P0.'globals'() 180 181 $I0 = does $P1, 'hash' 182 eq $I0, 0, nok_3 181 183 $I99 = $P99 182 184 $I1 = $P1 183 unless $I0 == 1 goto nok_3 184 unless $I1 == $I99 goto nok_3 185 unless $I1 == 2 goto ok_3 185 ne $I1, $I99, nok_3 186 ne $I1, 2, ok_3 # <<< should this be nok_3 instead?!? 186 187 $I0 = exists $P1['Prince'] 187 unless $I0 gotonok_3188 eq $I0, 0, nok_3 188 189 $I0 = exists $P1['Alex'] 190 eq $I0, 0, nok_3 189 191 goto ok_3 190 192 nok_3: 191 print 'not '193 $I9 = 0 192 194 ok_3: 193 say 'ok 3 - globals() with array arg sets globals hash (hash with two keys)'195 ok( $I9, '... with array arg sets globals hash (hash with two keys)' ) 194 196 195 197 # create a hash to store globals in 196 198 $P99 = new ['Hash'] … … 198 200 $P0.'globals'($P99) 199 201 $P1 = $P0.'globals'() 200 202 $I0 = isnull $P1 201 if $I0 goto ok_4 202 print 'not ' 203 ok_4: 204 say 'ok 4 - globals() with empty hash arg sets PMCNULL' 203 ok( $I0, '... with empty hash arg sets PMCNULL' ) 205 204 206 205 $P99['Prince'] = '' 207 206 $P99['Alex'] = '' 208 207 208 $I9 = 1 209 209 $P0.'globals'($P99) 210 210 $P1 = $P0.'globals'() 211 211 $I99 = $P99 212 212 $I1 = $P1 213 unless $I1 == $I99 gotonok_5214 unless $I1 == 2 gotonok_5213 ne $I1, $I99, nok_5 214 ne $I1, 2, nok_5 215 215 $I0 = exists $P1['Prince'] 216 unless $I0 gotonok_5216 eq $I0, 0, nok_5 217 217 $I0 = exists $P1['Alex'] 218 unless $I0 gotonok_5218 eq $I0, 0, nok_5 219 219 goto ok_5 220 220 nok_5: 221 print 'not '221 $I9 = 0 222 222 ok_5: 223 say 'ok 5 - globals() with hash arg sets globals hash (hash with two keys)'223 ok( $I9, '... with hash arg sets globals hash (hash with two keys)' ) 224 224 225 226 225 $P98 = clone $P99 227 226 227 $I9 = 1 228 228 push_eh ok_6 229 229 $P0.'globals'($P99, $P98) 230 $I9 = 0 231 ok_6: 230 232 pop_eh 233 ok( $I9, '... with too many args throws exception' ) 231 234 232 print 'not '233 ok_6:234 say 'ok 6 - globals() with too many args fails'235 236 235 .end 237 CODE238 ok 1 - globals() returns PMCNULL upon Exporter init239 ok 2 - globals() with empty array arg sets PMCNULL240 ok 3 - globals() with array arg sets globals hash (hash with two keys)241 ok 4 - globals() with empty hash arg sets PMCNULL242 ok 5 - globals() with hash arg sets globals hash (hash with two keys)243 ok 6 - globals() with too many args fails244 OUT245 236 246 pir_error_output_like( <<'CODE', <<'OUT', 'import - no args' ); 247 .sub 'test' :main237 .namespace ['test_import_without_args'] 238 .sub 'test' 248 239 $P0 = new ['Exporter'] 249 240 241 $I0 = 1 242 push_eh e1 250 243 $P0.'import'() 251 say 'ok 1 - import() with no args throws an exception' 252 244 $I0 = 0 245 e1: 246 pop_eh 247 # /^source namespace not set\n/ 248 ok( $I0, 'import() with no args throws exception' ) 253 249 .end 254 CODE255 /^source namespace not set\n/256 OUT257 250 258 pir_output_is( <<'CODE', <<'OUT', 'import - same source and destination namespaces' ); 259 .sub 'test' :main260 .local pmc exporter, src251 .namespace ['test_import_with_s_d_g'] 252 .sub 'test' 253 $P99 = get_namespace 261 254 262 src = get_namespace 263 264 exporter = new ['Exporter'] 265 exporter.'import'( src :named('source'), src :named('destination'), 'plan ok' :named('globals') ) 266 plan(1) 267 ok(1) 255 $P97 = new ['Exporter'] 256 $I0 = 0 257 push_eh e1 258 $P97.'import'( $P99 :named('source'), $P99 :named('destination'), 'plan ok' :named('globals') ) 259 $I0 = 1 260 e1: 261 pop_eh 262 ok( $I0, '... idempotently does nothing (and succeeds)' ) 268 263 .end 269 264 270 265 .sub 'plan' 271 .param int one 272 say '1..1' 266 die 'Extra planning was not expected' 273 267 .end 274 268 275 269 .sub 'ok' 276 .param int one 277 say 'ok 1' 270 .param pmc args :slurpy 271 $P0 = get_root_global [ 'parrot'; 'Test'; 'More' ], 'ok' 272 $P0( args :flat ) 278 273 .end 279 CODE280 1..1281 ok 1282 OUT283 274 284 pir_output_is( <<'CODE', <<'OUT', 'import - globals as string' ); 285 .sub 'test' :main 286 load_bytecode 'Test/More.pbc' 287 .local pmc exporter, src 288 289 src = get_namespace [ 'Test'; 'More' ] 290 291 exporter = new ['Exporter'] 292 exporter.'import'( src :named('source'), 'plan ok' :named('globals') ) 293 plan(1) 294 ok(1) 275 .namespace ['test_import_with_s_gs'] 276 .sub 'test' 277 $P99 = get_root_namespace [ 'parrot'; 'Test'; 'More' ] 278 $P97 = new ['Exporter'] 279 $P97.'import'( $P99 :named('source'), 'plan ok' :named('globals') ) 280 ok( 1, '... with globals-as-string succeeds' ) 295 281 .end 296 CODE297 1..1298 ok 1299 OUT300 282 301 pir_output_is( <<'CODE', <<'OUT', 'import - globals with source passed separately' ); 302 .sub 'test' :main 303 load_bytecode 'Test/More.pbc' 304 .local pmc exporter, src 283 .namespace ['test_import_with_gs'] 284 .sub 'test' 285 $P99 = get_root_namespace [ 'parrot'; 'Test'; 'More' ] 305 286 306 src = get_namespace [ 'Test'; 'More' ] 307 308 exporter = new ['Exporter'] 309 exporter.'source'( src ) 310 exporter.'import'( 'plan ok' :named('globals') ) 311 plan(1) 312 ok(1) 287 $P97 = new ['Exporter'] 288 $P97.'source'( $P99 ) 289 $P97.'import'( 'plan ok' :named('globals') ) 290 ok( 1, '... having called "source" separately succeeds' ) 313 291 .end 314 CODE315 1..1316 ok 1317 OUT318 292 319 pir_output_is( <<'CODE', <<'OUT', 'import - globals as array' ); 320 .sub 'test' :main 321 load_bytecode 'Test/More.pbc' 322 .local pmc exporter, src, globals 293 .namespace ['test_import_with_s_ga'] 294 .sub 'test' 295 $P99 = get_root_namespace [ 'parrot'; 'Test'; 'More' ] 296 $P96 = new ['ResizableStringArray'] 297 $P96 = push 'ok' 298 $P96 = push 'plan' 323 299 324 src = get_namespace [ 'Test'; 'More' ] 325 globals = new ['ResizableStringArray'] 326 globals = push 'ok' 327 globals = push 'plan' 328 329 exporter = new ['Exporter'] 330 exporter.'import'( src :named('source'), globals :named('globals') ) 331 plan(1) 332 ok(1) 300 $P97 = new ['Exporter'] 301 $P97.'import'( $P99 :named('source'), $P96 :named('globals') ) 302 ok( 1, '... with globals-as-array succeeds' ) 333 303 .end 334 CODE335 1..1336 ok 1337 OUT338 304 339 pir_output_is( <<'CODE', <<'OUT', 'import - globals as hash - null + empty string' ); 340 .sub 'test' :main 341 load_bytecode 'Test/More.pbc' 342 .local pmc exporter, src, globals, nul 305 .namespace ['test_import_with_s_gh'] 306 .sub 'test' 307 $P95 = new ['Null'] 308 $P99 = get_root_namespace [ 'parrot'; 'Test'; 'More' ] 309 $P96 = new ['Hash'] 310 $P96['ok'] = $P95 311 $P96['plan'] = '' 343 312 344 nul = new ['Null'] 345 src = get_namespace [ 'Test'; 'More' ] 346 globals = new ['Hash'] 347 globals['ok'] = nul 348 globals['plan'] = '' 349 350 exporter = new ['Exporter'] 351 exporter.'import'( src :named('source'), globals :named('globals') ) 352 plan(1) 353 ok(1) 313 $P97 = new ['Exporter'] 314 $P97.'import'( $P99 :named('source'), $P96 :named('globals') ) 315 ok( 1, '... with globals-as-hash succeeds' ) 354 316 .end 355 CODE356 1..1357 ok 1358 OUT359 317 360 pir_output_is( <<'CODE', <<'OUT', 'import - globals as hash - with dest names (latin)' ); 361 .sub 'test' :main 362 load_bytecode 'Test/More.pbc' 363 .local pmc exporter, src, globals 318 .namespace ['test_import_and_call'] 319 .sub 'test' 320 $P99 = get_root_namespace [ 'parrot'; 'Test'; 'More' ] 321 $P96 = new ['Hash'] 322 $P96['plan'] = 'consilium' 323 $P96['ok'] = 'rectus' 364 324 365 src = get_namespace [ 'Test'; 'More' ] 366 globals = new ['Hash'] 367 globals['plan'] = 'consilium' 368 globals['ok'] = 'rectus' 369 370 exporter = new ['Exporter'] 371 exporter.'import'( src :named('source'), globals :named('globals') ) 372 consilium(1) 373 rectus(1) 325 $P97 = new ['Exporter'] 326 $P97.'import'( $P99 :named('source'), $P96 :named('globals') ) 327 #consilium(1) 328 rectus( 1, '... into current namespace with another (global) name succeeds' ) 374 329 .end 375 CODE376 1..1377 ok 1378 OUT379 330 380 pir_output_is( <<'CODE', <<'OUT', 'import - globals with destination' ); 381 .sub 'test' :main 382 load_bytecode 'Test/More.pbc' 383 .local pmc exporter, src, dest, globals 331 .namespace ['test_import_and_ind_call'] 332 .sub 'test' 333 $P99 = get_root_namespace [ 'parrot'; 'Test'; 'More' ] 334 $P98 = get_root_namespace [ 'parrot'; 'foo' ] 335 $P96 = new ['ResizableStringArray'] 336 $P96 = push 'ok' 337 $P96 = push 'plan' 384 338 385 src = get_namespace [ 'Test'; 'More' ] 386 dest = get_namespace ['foo'] 387 globals = new ['ResizableStringArray'] 388 globals = push 'ok' 389 globals = push 'plan' 339 $P97 = new ['Exporter'] 340 $P97.'import'( $P99 :named('source'), $P98 :named('destination'), $P96 :named('globals') ) 390 341 391 exporter = new ['Exporter'] 392 exporter.'import'( src :named('source'), dest :named('destination'), globals :named('globals') ) 393 394 $P0 = get_global ['foo'], 'bar' 342 $P0 = get_root_global [ 'parrot'; 'foo' ], 'bar' 395 343 $P0() 396 344 .end 397 345 398 346 .namespace ['foo'] 399 347 .sub 'bar' 400 plan(1) 401 ok(1) 348 ok( 1, '... into another namespace succeeds' ) 402 349 .end 403 CODE404 1..1405 ok 1406 OUT407 350 408 # TODO: Test exporting mmd subs: TT #1205 409 # https://trac.parrot.org/parrot/ticket/1205 351 # RT #46861 test exporting mmd subs 410 352 411 353 # Local Variables: 412 # mode: cperl354 # mode: pir 413 355 # cperl-indent-level: 4 414 356 # fill-column: 100 415 357 # End: 416 # vim: expandtab shiftwidth=4 :358 # vim: expandtab shiftwidth=4 ft=pir: