1#!/usr/bin/perl -w
2
3use Test::More;
4use strict;
5
6sub _write_utf8_file
7{
8    my ($out_path, $contents) = @_;
9
10    open my $out_fh, '>:encoding(utf8)', $out_path
11        or die "Cannot open '$out_path' for writing - $!";
12
13    print {$out_fh} $contents;
14
15    close($out_fh);
16
17    return;
18}
19
20# test text file input => ASCII output, and back to as_txt() again
21
22BEGIN
23   {
24   plan tests => 451;
25   # TEST
26   use_ok ("Graph::Easy") or die($@);
27   # TEST
28   use_ok ("Graph::Easy::Parser") or die($@);
29   };
30
31#############################################################################
32# parser object
33
34my $parser = Graph::Easy::Parser->new( debug => 0);
35
36is (ref($parser), 'Graph::Easy::Parser');
37is ($parser->error(), '', 'no error yet');
38
39opendir DIR, "t/in" or die ("Cannot read dir 'in': $!");
40my @files = readdir(DIR); closedir(DIR);
41
42my @failures;
43
44eval { require Test::Differences; };
45
46binmode (STDERR, ':utf8') or die ("Cannot do binmode(':utf8') on STDERR: $!");
47binmode (STDOUT, ':utf8') or die ("Cannot do binmode(':utf8') on STDOUT: $!");
48
49foreach my $f (sort @files)
50  {
51      my $path =  "t/in/$f";
52  next unless -f $path; 			# only files
53
54  next unless $f =~ /\.txt/;			# ignore anything else
55
56  print "# at $f\n";
57  my $txt = readfile($path);
58  my $graph = $parser->from_text($txt);		# reuse parser object
59
60  $txt =~ s/\n\s+\z/\n/;			# remove trailing whitespace
61  $txt =~ s/(^|\n)\s*#[^#]{2}.*\n//g;		# remove comments
62
63  $f =~ /^(\d+)/;
64  my $nodes = $1;
65
66  if (!defined $graph)
67    {
68    warn ("Graph input was invalid: " . $parser->error());
69    push @failures, $f;
70    next;
71    }
72  is (scalar $graph->nodes(), $nodes, "$nodes nodes");
73
74  # for slow testing machines
75  $graph->timeout(20);
76  my $ascii = $graph->as_ascii();
77
78  my $out_path = "t/out/$f";
79  my $out = readfile($out_path);
80  $out =~ s/(^|\n)\s*#[^#=]{2}.*\n//g;		# remove comments
81  $out =~ s/\n\n\z/\n/mg;			# remove empty lines
82
83# print "txt: $txt\n";
84# print "ascii: $ascii\n";
85# print "should: $out\n";
86
87  if (!
88    is ($ascii, $out, "from $f"))
89    {
90        if ($ENV{__SHLOMIF__UPDATE_ME})
91        {
92            _write_utf8_file($out_path, $ascii);
93        }
94    push @failures, $f;
95    if (defined $Test::Differences::VERSION)
96      {
97      Test::Differences::eq_or_diff ($ascii, $out);
98      }
99    else
100      {
101      fail ("Test::Differences not installed");
102      }
103    }
104
105  my $txt_path = "t/txt/$f";
106  # if the txt output differes, read it in
107  if (-f $txt_path)
108    {
109    $txt = readfile($txt_path);
110    }
111#  else
112#    {
113#    # input might have whitespace at front, remove it because output doesn't
114#    $txt =~ s/(^|\n)\x20+/$1/g;
115#    }
116
117  if (!
118    is ($graph->as_txt(), $txt, "$f as_txt"))
119    {
120        if ($ENV{__SHLOMIF__UPDATE_ME})
121        {
122            _write_utf8_file($txt_path, scalar( $graph->as_txt() ));
123        }
124    push @failures, $f;
125    if (defined $Test::Differences::VERSION)
126      {
127      Test::Differences::eq_or_diff ($graph->as_txt(), $txt);
128      }
129    else
130      {
131      fail ("Test::Differences not installed");
132      }
133    }
134
135  # print a debug output
136  my $debug = $ascii;
137  $debug =~ s/\n/\n# /g;
138  print "# Generated:\n#\n# $debug\n";
139  }
140
141if (@failures)
142  {
143  print "# !!! Failed the following tests:\n";
144  for my $f (@failures)
145    {
146    print "#      $f\n";
147    }
148  print "# !!!\n\n";
149  }
150
1511;
152
153sub readfile
154  {
155  my ($filename) = @_;
156
157  open my $fh, $filename or die ("Cannot read file ${filename}: $!");
158  binmode ($fh, ':utf8') or die ("Cannot do binmode(':utf8') on ${fh}: $!");
159  local $/ = undef;				# slurp mode
160  my $doc = <$fh>;
161  close $fh;
162
163  $doc;
164  }
165