Move IO::Compress from ext/ to cpan/
[p5sagit/p5-mst-13.2.git] / cpan / IO-Compress / t / cz-05examples.t
CommitLineData
16816334 1BEGIN {
d695c1a1 2 if ($ENV{PERL_CORE}) {
16816334 3 chdir 't' if -d 't';
1a6a8453 4 @INC = ("../lib", "lib/compress");
16816334 5 }
6}
f4c6fd49 7
25f0751f 8use lib qw(t t/compress);
f4c6fd49 9
642e522c 10use strict;
11use warnings;
12use bytes;
f4c6fd49 13
642e522c 14use Test::More ;
25f0751f 15use CompTestUtils;
642e522c 16use Compress::Zlib;
667342e9 17
642e522c 18BEGIN
19{
20 plan(skip_all => "Examples needs Perl 5.005 or better - you have Perl $]" )
21 if $] < 5.005 ;
22
23 # use Test::NoWarnings, if available
24 my $extra = 0 ;
25 $extra = 1
26 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
27
25f0751f 28 plan tests => 26 + $extra ;
667342e9 29}
30
31
5993747c 32my $Inc = join " ", map qq["-I$_"] => @INC;
667342e9 33$Inc = '"-MExtUtils::testlib"'
642e522c 34 if ! $ENV{PERL_CORE} && eval " require ExtUtils::testlib; " ;
f4c6fd49 35
642e522c 36my $Perl = ($ENV{'FULLPERL'} or $^X or 'perl') ;
f4c6fd49 37$Perl = qq["$Perl"] if $^O eq 'MSWin32' ;
38
642e522c 39$Perl = "$Perl $Inc -w" ;
319fab50 40my $examples = $ENV{PERL_CORE} ? "../ext/IO-Compress/examples/compress-zlib"
41 : "./examples/compress-zlib";
f4c6fd49 42
43my $hello1 = <<EOM ;
44hello
45this is
46a test
47message
48x ttttt
49xuuuuuu
50the end
51EOM
52
53my @hello1 = grep(s/$/\n/, split(/\n/, $hello1)) ;
54
55my $hello2 = <<EOM;
56
57Howdy
58this is the
59second
60file
61x ppppp
62xuuuuuu
63really the end
64EOM
65
66my @hello2 = grep(s/$/\n/, split(/\n/, $hello2)) ;
67
f4c6fd49 68my $file1 = "hello1.gz" ;
69my $file2 = "hello2.gz" ;
667342e9 70my $stderr = "err.out" ;
f4c6fd49 71
9f2e3514 72for ($file1, $file2, $stderr) { 1 while unlink $_ } ;
73
642e522c 74
667342e9 75my $gz = gzopen($file1, "wb");
76$gz->gzwrite($hello1);
77$gz->gzclose();
f4c6fd49 78
667342e9 79$gz = gzopen($file2, "wb");
80$gz->gzwrite($hello2);
81$gz->gzclose();
f4c6fd49 82
642e522c 83sub check
84{
85 my $command = shift ;
86 my $expected = shift ;
87
88 my $stderr = 'err.out';
9f2e3514 89 1 while unlink $stderr;
642e522c 90
91 my $cmd = "$command 2>$stderr";
92 my $stdout = `$cmd` ;
93
94 my $aok = 1 ;
f4c6fd49 95
642e522c 96 $aok &= is $?, 0, " exit status is 0" ;
f4c6fd49 97
642e522c 98 $aok &= is readFile($stderr), '', " no stderr" ;
99
100 $aok &= is $stdout, $expected, " expected content is ok"
101 if defined $expected ;
102
103 if (! $aok) {
104 diag "Command line: $cmd";
105 my ($file, $line) = (caller)[1,2];
106 diag "Test called from $file, line $line";
107 }
108
9f2e3514 109 1 while unlink $stderr;
642e522c 110}
f4c6fd49 111
667342e9 112# gzcat
113# #####
114
642e522c 115title "gzcat - command line" ;
116check "$Perl ${examples}/gzcat $file1 $file2", $hello1 . $hello2;
117
118title "gzcat - stdin" ;
119check "$Perl ${examples}/gzcat <$file1 ", $hello1;
120
f4c6fd49 121
122# gzgrep
123# ######
124
642e522c 125title "gzgrep";
126check "$Perl ${examples}/gzgrep the $file1 $file2",
667342e9 127 join('', grep(/the/, @hello1, @hello2));
f4c6fd49 128
1a6a8453 129for ($file1, $file2, $stderr) { 1 while unlink $_ } ;
130
f4c6fd49 131
132
133# filtdef/filtinf
134# ##############
135
136
f4c6fd49 137writeFile($file1, $hello1) ;
138writeFile($file2, $hello2) ;
139
642e522c 140title "filtdef" ;
f4c6fd49 141# there's no way to set binmode on backticks in Win32 so we won't use $a later
642e522c 142check "$Perl ${examples}/filtdef $file1 $file2" ;
f4c6fd49 143
642e522c 144title "filtdef | filtinf";
145check "$Perl ${examples}/filtdef $file1 $file2 | $Perl ${examples}/filtinf",
146 $hello1 . $hello2;
f4c6fd49 147# gzstream
148# ########
149
150{
642e522c 151 title "gzstream" ;
f4c6fd49 152 writeFile($file1, $hello1) ;
642e522c 153 check "$Perl ${examples}/gzstream <$file1 >$file2";
76e6f389 154
642e522c 155 title "gzcat" ;
156 check "$Perl ${examples}/gzcat $file2", $hello1 ;
f4c6fd49 157}
158
5993747c 159END
160{
161 for ($file1, $file2, $stderr) { 1 while unlink $_ } ;
162}
642e522c 163