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