D'oh. Don't turn on warnings on the #! line without actually testing
[p5sagit/p5-mst-13.2.git] / ext / B / t / o.t
1 #!./perl -w
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = ('../lib', 'lib', '.');
6         require 'test.pl';
7 }
8
9 use strict;
10 use Config;
11 use File::Spec;
12 use File::Path;
13
14 my $path = File::Spec->catdir( 'lib', 'B' );
15 unless (-d $path) {
16         mkpath( $path ) or skip_all( 'Cannot create fake module path' );
17 }
18
19 my $file = File::Spec->catfile( $path, 'success.pm' );
20 local *OUT;
21 open(OUT, '>', $file) or skip_all( 'Cannot write fake backend module');
22 print OUT while <DATA>;
23 close *OUT;
24
25 plan( 9 ); # And someone's responsible.
26
27 # use() makes it difficult to avoid O::import()
28 require_ok( 'O' );
29
30 my @args = ('-Ilib', '-MO=success,foo,bar', '-e', '1' );
31 my @lines = get_lines( @args );
32
33 is( $lines[0], 'Compiling!', 'Output should not be saved without -q switch' );
34 is( $lines[1], '(foo) <bar>', 'O.pm should call backend compile() method' );
35 is( $lines[2], '[]', 'Nothing should be in $O::BEGIN_output without -q' );
36 is( $lines[3], '-e syntax OK', 'O.pm should not munge perl output without -qq');
37
38 $args[1] = '-MO=-q,success,foo,bar';
39 @lines = get_lines( @args );
40 isnt( $lines[1], 'Compiling!', 'Output should not be printed with -q switch' );
41
42 SKIP: {
43         skip( '-q redirection does not work without PerlIO', 2)
44                 unless $Config{useperlio};
45         is( $lines[1], "[Compiling!", '... but should be in $O::BEGIN_output' );
46
47         $args[1] = '-MO=-qq,success,foo,bar';
48         @lines = get_lines( @args );
49         is( scalar @lines, 3, '-qq should suppress even the syntax OK message' );
50 }
51
52 $args[1] = '-MO=success,fail';
53 @lines = get_lines( @args );
54 like( $lines[1], qr/fail at .eval/,
55         'O.pm should die if backend compile() does not return a subref' );
56
57 sub get_lines {
58         split(/[\r\n]+/, runperl( args => [ @_ ], stderr => 1 ));
59 }
60
61 END {
62         1 while unlink($file);
63         rmdir($path); # not "1 while" since there might be more in there
64 }
65
66 __END__
67 package B::success;
68
69 $| = 1;
70 print "Compiling!\n";
71
72 sub compile {
73         return 'fail' if ($_[0] eq 'fail');
74         print "($_[0]) <$_[1]>\n";
75         return sub { print "[$O::BEGIN_output]\n" };
76 }
77
78 1;