Add test preambles to Compress::Zlib.
[p5sagit/p5-mst-13.2.git] / ext / Compress / Zlib / t / 05examples.t
1 BEGIN {
2     if ($ENV{PERL_CORE} {
3         chdir 't' if -d 't';
4         @INC = '../lib';
5     }
6 }
7
8 use lib 't';
9
10 use strict;
11 use warnings;
12 use bytes;
13
14 use Test::More ;
15 use ZlibTestUtils;
16 use Compress::Zlib;
17
18 BEGIN 
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 ;
29 }
30
31
32 my $Inc = join " ", map qq["-I$_"] => @INC;
33 $Inc = '"-MExtUtils::testlib"'
34     if ! $ENV{PERL_CORE} && eval " require ExtUtils::testlib; " ;
35
36 my $Perl = ($ENV{'FULLPERL'} or $^X or 'perl') ;
37 $Perl = qq["$Perl"] if $^O eq 'MSWin32' ;
38  
39 $Perl = "$Perl $Inc -w" ;
40 my $examples = $ENV{PERL_CORE} ? "../ext/Compress/Zlib/examples" 
41                                : "./examples";
42
43 my $hello1 = <<EOM ;
44 hello
45 this is 
46 a test
47 message
48 x ttttt
49 xuuuuuu
50 the end
51 EOM
52
53 my @hello1 = grep(s/$/\n/, split(/\n/, $hello1)) ;
54
55 my $hello2 = <<EOM;
56
57 Howdy
58 this is the
59 second
60 file
61 x ppppp
62 xuuuuuu
63 really the end
64 EOM
65
66 my @hello2 = grep(s/$/\n/, split(/\n/, $hello2)) ;
67
68 my $file1 = "hello1.gz" ;
69 my $file2 = "hello2.gz" ;
70 my $stderr = "err.out" ;
71 unlink $stderr ;
72
73 unlink $file1, $file2 ;
74
75 my $gz = gzopen($file1, "wb");
76 $gz->gzwrite($hello1);
77 $gz->gzclose();
78
79 $gz = gzopen($file2, "wb");
80 $gz->gzwrite($hello2);
81 $gz->gzclose();
82
83 sub 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 ;
95
96     $aok &= is $?, 0, "  exit status is 0" ;
97
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 }
111
112 # gzcat
113 # #####
114
115 title "gzcat.zlib" ;
116 check "$Perl ${examples}/gzcat.zlib $file1 $file2 ", $hello1 . $hello2 ;
117
118 title "gzcat - command line" ;
119 check "$Perl ${examples}/gzcat $file1 $file2",  $hello1 . $hello2;
120
121 title "gzcat - stdin" ;
122 check "$Perl ${examples}/gzcat <$file1 ", $hello1;
123
124
125 # gzgrep
126 # ######
127
128 title "gzgrep";
129 check "$Perl  ${examples}/gzgrep the $file1 $file2",
130         join('', grep(/the/, @hello1, @hello2));
131
132 unlink $file1, $file2 ;
133
134
135 # filtdef/filtinf
136 # ##############
137
138
139 writeFile($file1, $hello1) ;
140 writeFile($file2, $hello2) ;
141
142 title "filtdef" ;
143 # there's no way to set binmode on backticks in Win32 so we won't use $a later
144 check "$Perl ${examples}/filtdef $file1 $file2" ;
145
146 title "filtdef | filtinf";
147 check "$Perl ${examples}/filtdef $file1 $file2 | $Perl ${examples}/filtinf",
148         $hello1 . $hello2;
149 # gzstream
150 # ########
151
152 {
153     title "gzstream" ;
154     writeFile($file1, $hello1) ;
155     check "$Perl ${examples}/gzstream <$file1 >$file2";
156
157     title "gzcat" ;
158     check "$Perl ${examples}/gzcat $file2", $hello1 ;
159 }
160
161 END
162 {
163     for ($file1, $file2, $stderr) { 1 while unlink $_ } ;
164 }
165