3 # Copyright (C) 2006, The Perl Foundation.
9 use Test::More tests => 26;
12 my $hello_pbc = File::Spec->catfile( 't', 'greet.pbc' );
14 my $module = 'Parrot::Interpreter';
15 use_ok('Parrot::Embed') or exit;
17 can_ok( $module, 'new' );
18 my $interp = $module->new();
19 ok( $interp, 'new() should return a valid interpreter' );
20 isa_ok( $interp, $module );
22 can_ok( $module, 'load_file' );
23 my $result = eval { $interp->load_file('no file here') };
25 ok( !$result, 'load_file() should return false unless it can load a file' );
26 like( $except, qr/File 'no file here' not found/, '... throwing exception' );
27 $result = eval { $interp->load_file($hello_pbc) };
29 ok( $result, '... returning true if it could load the file' );
30 is( $except, '', '... throwing no exeption if so' );
32 can_ok( $module, 'find_global' );
33 my $global_greet = $interp->find_global('greet');
34 ok( $global_greet, 'find_global() should return non-namespaced global, if found' );
35 isa_ok( $global_greet, 'Parrot::PMC' );
37 ok( !$interp->find_global('goat'),
38 '... or nothing, if there is no non-namespaced global of that name' );
40 my $else_greet = $interp->find_global( 'greet', 'Elsewhere' );
41 ok( $else_greet, '... or a namespaced global, if it exists in the namespace' );
42 isnt( $$global_greet, $$else_greet, '... and definitely the namespaced version' );
44 ok( !$interp->find_global( 'goat', 'Elsewhere' ),
45 '... but again, not if there is no global of that name there' );
47 can_ok( $global_greet, 'invoke' );
48 my $pmc = $global_greet->invoke( 'PS', 'Bob' );
49 ok( $pmc, 'invoke() should return a PMC, given that signature' );
51 is( $pmc->get_string(), 'Hello, Bob!', '... containing a string returned in the PMC' );
53 can_ok( $module, 'compile' );
54 my $eval = $interp->compile( <<END_PIR );
58 .local string string_s
66 ok( $eval, 'compile() should compile PIR code and return a PMC' );
67 isa_ok( $eval, 'Parrot::PMC' );
71 local $TODO = 'compile_string() returns wrong results';
72 ok( !$interp->compile('blah'), '... but only for valid PIR' );
75 $pmc = $else_greet->invoke( 'P', '' );
76 is( $pmc->get_string(), 'Hiya!', '... calling the passed-in subroutine' );
78 my $foo = $interp->find_global('foo');
79 $pmc = $foo->invoke( 'PS', 'BAR' );
80 is( $pmc->get_string(), 'BAR FOO ',
81 '... and compiled sub should work just like any other Sub pmc' );
84 my $die_interp = $module->new($interp);
85 eval { $die_interp->load_file($hello_pbc) };
86 $foo = $die_interp->find_global('greet');
89 $pmc = $foo->invoke( 'PS', 'out of scope' );
92 'Hello, out of scope!',
93 '... even if interpreter object has gone out of scope'
98 # cperl-indent-level: 4
101 # vim: expandtab shiftwidth=4: