Tidy PerlIO::Via
[p5sagit/p5-mst-13.2.git] / ext / PerlIO / t / via.t
CommitLineData
2b8740d8 1#!./perl
2
99ef548b 3use strict;
4use warnings;
5
2b8740d8 6BEGIN {
7 chdir 't' if -d 't';
8 @INC = '../lib';
9 unless (find PerlIO::Layer 'perlio') {
10 print "1..0 # Skip: not perlio\n";
11 exit 0;
12 }
13}
14
15my $tmp = "via$$";
16
30ef3321 17use Test::More tests => 16;
99ef548b 18
19my $fh;
20my $a = join("", map { chr } 0..255) x 10;
21my $b;
2b8740d8 22
99ef548b 23BEGIN { use_ok('MIME::QuotedPrint'); }
2b8740d8 24
30ef3321 25ok( !open($fh,"<Via(MIME::QuotedPrint)", $tmp), 'open QuotedPrint fails');
99ef548b 26ok( open($fh,">Via(MIME::QuotedPrint)", $tmp), 'open QuotedPrint for output');
27ok( (print $fh $a), "print to output file");
28ok( close($fh), 'close output file');
2b8740d8 29
99ef548b 30ok( open($fh,"<Via(MIME::QuotedPrint)", $tmp), 'open QuotedPrint for input');
2b8740d8 31{ local $/; $b = <$fh> }
99ef548b 32ok( close($fh), "close input file");
33
34is($a, $b, 'compare original data with filtered version');
35
36
37{
38 my $warnings = '';
39 local $SIG{__WARN__} = sub { $warnings = join '', @_ };
40
41 use warnings 'layer';
d9dac8cd 42
43 # Find fd number we should be using
44 my $fd = open($fh,">$tmp") && fileno($fh);
45 print $fh "Hello\n";
46 close($fh);
47
99ef548b 48 ok( ! open($fh,">Via(Unknown::Module)", $tmp), 'open Via Unknown::Module will fail');
49 like( $warnings, qr/^Cannot find package 'Unknown::Module'/, 'warn about unknown package' );
2b8740d8 50
d9dac8cd 51 # Now open normally again to see if we get right fileno
52 my $fd2 = open($fh,"<$tmp") && fileno($fh);
53 is($fd2,$fd,"Wrong fd number after failed open");
54
55 my $data = <$fh>;
56
57 is($data,"Hello\n","File clobbered by failed open");
58
59 close($fh);
60
61
62
99ef548b 63 $warnings = '';
64 no warnings 'layer';
65 ok( ! open($fh,">Via(Unknown::Module)", $tmp), 'open Via Unknown::Module will fail');
66 is( $warnings, "", "don't warn about unknown package" );
d9dac8cd 67}
2b8740d8 68
52f3c1af 69my $obj = '';
70sub Foo::PUSHED { $obj = shift; -1; }
71sub PerlIO::Via::Bar::PUSHED { $obj = shift; -1; }
72open $fh, '<:Via(Foo)', "foo";
73is( $obj, 'Foo', 'search for package Foo' );
74open $fh, '<:Via(Bar)', "bar";
75is( $obj, 'PerlIO::Via::Bar', 'search for package PerlIO::Via::Bar' );
76
2b8740d8 77END {
78 1 while unlink $tmp;
79}
30ef3321 80