xref: /openbsd-src/gnu/usr.bin/perl/cpan/Pod-Simple/t/reinit.t (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
1b39c5158Smillertuse strict;
2256a93a4Safresh1use warnings;
3*5486feefSafresh1use Test::More tests => 5;
4b39c5158Smillert
5*5486feefSafresh1use File::Spec;
6*5486feefSafresh1use Cwd ();
7*5486feefSafresh1use File::Basename ();
8b39c5158Smillert
9b39c5158Smillertuse Pod::Simple::Text;
10b39c5158Smillert$Pod::Simple::Text::FREAKYMODE = 1;
11b39c5158Smillert
12b39c5158Smillertmy $parser  = Pod::Simple::Text->new();
13b39c5158Smillert
14b39c5158Smillertforeach my $file (
15b39c5158Smillert  "junk1.pod",
16b39c5158Smillert  "junk2.pod",
17b39c5158Smillert  "perlcyg.pod",
18b39c5158Smillert  "perlfaq.pod",
19b39c5158Smillert  "perlvar.pod",
20b39c5158Smillert) {
21*5486feefSafresh1    my $full_file = File::Spec->catfile(File::Basename::dirname(Cwd::abs_path(__FILE__)), $file);
22b39c5158Smillert
23*5486feefSafresh1    unless(-e $full_file) {
24b39c5158Smillert        ok 0;
25*5486feefSafresh1        print "# But $full_file doesn't exist!!\n";
26b39c5158Smillert        next;
27b39c5158Smillert    }
28b39c5158Smillert
29*5486feefSafresh1    my $precooked = $full_file;
30b39c5158Smillert    my $outstring;
31b39c5158Smillert    my $compstring;
32b39c5158Smillert    $precooked =~ s<\.pod><o.txt>s;
33b39c5158Smillert    $parser->reinit;
34b39c5158Smillert    $parser->output_string(\$outstring);
35*5486feefSafresh1    $parser->parse_file($full_file);
36b39c5158Smillert
37b39c5158Smillert    open(IN, $precooked) or die "Can't read-open $precooked: $!";
38b39c5158Smillert    {
39b39c5158Smillert      local $/;
40b39c5158Smillert      $compstring = <IN>;
41b39c5158Smillert    }
42b39c5158Smillert    close(IN);
43b39c5158Smillert
44b39c5158Smillert    for ($outstring,$compstring) { s/\s+/ /g; s/^\s+//s; s/\s+$//s; }
45b39c5158Smillert
46b39c5158Smillert    if($outstring eq $compstring) {
47b39c5158Smillert      ok 1;
48b39c5158Smillert      next;
49b39c5158Smillert    } elsif( do{
50b39c5158Smillert      for ($outstring, $compstring) { tr/ //d; };
51b39c5158Smillert      $outstring eq $compstring;
52b39c5158Smillert    }){
53b39c5158Smillert      print "# Differ only in whitespace.\n";
54b39c5158Smillert      ok 1;
55b39c5158Smillert      next;
56b39c5158Smillert    } else {
57b39c5158Smillert
58b39c5158Smillert      my $x = $outstring ^ $compstring;
59b39c5158Smillert      $x =~ m/^(\x00*)/s or die;
60b39c5158Smillert      my $at = length($1);
61b39c5158Smillert      print "# Difference at byte $at...\n";
62b39c5158Smillert      if($at > 10) {
63b39c5158Smillert        $at -= 5;
64b39c5158Smillert      }
65b39c5158Smillert      {
66b39c5158Smillert        print "# ", substr($outstring,$at,20), "\n";
67b39c5158Smillert        print "# ", substr($compstring,$at,20), "\n";
68b39c5158Smillert        print "#      ^...";
69b39c5158Smillert      }
70b39c5158Smillert
71b39c5158Smillert      ok 0;
72b39c5158Smillert      printf "# Unequal lengths %s and %s\n", length($outstring), length($compstring);
73b39c5158Smillert      next;
74b39c5158Smillert    }
75b39c5158Smillert  }
76