Add test preambles to Compress::Zlib.
[p5sagit/p5-mst-13.2.git] / ext / Compress / Zlib / t / 05examples.t
CommitLineData
16816334 1BEGIN {
2 if ($ENV{PERL_CORE} {
3 chdir 't' if -d 't';
4 @INC = '../lib';
5 }
6}
f4c6fd49 7
642e522c 8use lib 't';
f4c6fd49 9
642e522c 10use strict;
11use warnings;
12use bytes;
f4c6fd49 13
642e522c 14use Test::More ;
15use ZlibTestUtils;
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
28 plan tests => 30 + $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" ;
667342e9 40my $examples = $ENV{PERL_CORE} ? "../ext/Compress/Zlib/examples"
41 : "./examples";
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" ;
71unlink $stderr ;
f4c6fd49 72
642e522c 73unlink $file1, $file2 ;
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';
89 unlink $stderr;
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
109 unlink $stderr;
110}
f4c6fd49 111
667342e9 112# gzcat
113# #####
114
642e522c 115title "gzcat.zlib" ;
116check "$Perl ${examples}/gzcat.zlib $file1 $file2 ", $hello1 . $hello2 ;
117
118title "gzcat - command line" ;
119check "$Perl ${examples}/gzcat $file1 $file2", $hello1 . $hello2;
120
121title "gzcat - stdin" ;
122check "$Perl ${examples}/gzcat <$file1 ", $hello1;
123
f4c6fd49 124
125# gzgrep
126# ######
127
642e522c 128title "gzgrep";
129check "$Perl ${examples}/gzgrep the $file1 $file2",
667342e9 130 join('', grep(/the/, @hello1, @hello2));
f4c6fd49 131
f4c6fd49 132unlink $file1, $file2 ;
133
134
135# filtdef/filtinf
136# ##############
137
138
f4c6fd49 139writeFile($file1, $hello1) ;
140writeFile($file2, $hello2) ;
141
642e522c 142title "filtdef" ;
f4c6fd49 143# there's no way to set binmode on backticks in Win32 so we won't use $a later
642e522c 144check "$Perl ${examples}/filtdef $file1 $file2" ;
f4c6fd49 145
642e522c 146title "filtdef | filtinf";
147check "$Perl ${examples}/filtdef $file1 $file2 | $Perl ${examples}/filtinf",
148 $hello1 . $hello2;
f4c6fd49 149# gzstream
150# ########
151
152{
642e522c 153 title "gzstream" ;
f4c6fd49 154 writeFile($file1, $hello1) ;
642e522c 155 check "$Perl ${examples}/gzstream <$file1 >$file2";
76e6f389 156
642e522c 157 title "gzcat" ;
158 check "$Perl ${examples}/gzcat $file2", $hello1 ;
f4c6fd49 159}
160
5993747c 161END
162{
163 for ($file1, $file2, $stderr) { 1 while unlink $_ } ;
164}
642e522c 165