Add the following packages libalgorithm-diff-perl libspiffy-perl libtext-diff-perl...
[pkg-perl] / deb-src / libtext-diff-perl / libtext-diff-perl-0.35 / t / general.t
1 #!/usr/local/bin/perl -w
2
3 use strict ;
4 use Test ;
5 use Text::Diff ;
6 use Algorithm::Diff qw( traverse_sequences ) ;
7
8 ## Each test specifies options to pass to "diff" when the --update option
9 ## is present in @ARGV and options to pass to Text::Diff::diff when the
10 ## tests are run.
11 my @tests= (
12     ["-u", 
13         ''
14     ],
15     ["-c",
16         'STYLE => "Context"'
17     ],
18     ["-C0",
19         'STYLE => "Context", CONTEXT => 0'
20     ],
21     ["-U0",
22         'STYLE => "Unified", CONTEXT => 0'
23     ],
24     ["",
25         'STYLE => "OldStyle"'     
26     ],
27 ) ;
28
29 my @A = map "$_\n", qw( 1 2 3 4 5d 6 7 8 9    10 11 11d 12 13 ) ;
30 my @B = map "$_\n", qw( 1 2 3 4 5a 6 7 8 9 9a 10 11     12 13 ) ;
31
32 my $sep = ( "----8<----" x 7 ) . "\n" ;
33
34 if ( grep "--update", @ARGV ) {
35     my $version = `diff -v` ;
36
37     die "Could not determine your diff's version"
38         unless defined $version && length $version ;
39     chomp $version ;
40     die "Requires GNU's diff, not '$version'" 
41         unless $version =~ /GNU/ ;
42
43     ## Here are the two files to feed to diff
44     open A, ">A" or die $! ; print A @A ; close A ;
45     open B, ">B" or die $! ; print B @B ; close B ;
46
47     my $mtime_A = time ;
48     my $mtime_B = $mtime_A + 1 ;
49
50     utime $mtime_A, $mtime_A, "A" or die $! ;
51     utime $mtime_B, $mtime_B, "B" or die $! ;
52
53     my $file_options = <<END_OPTIONS ;
54 FILENAME_A => "A",
55 MTIME_A => $mtime_A,
56 FILENAME_B => "B",
57 MTIME_B => $mtime_B,
58 END_OPTIONS
59
60     open ME, "<$0" or die $! ;
61     my $me = join( "", <ME> ) ;
62     close ME or die $! ;
63
64     open BAK, ">$0.bak" or die $! ;
65     print BAK $me or die $! ;
66     close BAK or die $! ;
67
68     my @diffs = map scalar `diff $_->[0] A B`, @tests ;
69
70     for ( @diffs ) {
71         s/(Sun|Mon|Tue|Wed|Thu|Fri|Sat).*/<MTIME_A>/m ;
72         s/(Sun|Mon|Tue|Wed|Thu|Fri|Sat).*/<MTIME_B>/m ;
73     }
74
75     $me =~ s/^(__DATA__\n).*//ms ;
76     open ME, ">$0" or die $! ;
77     print ME
78         $me,
79         "__DATA__\n",
80         join $sep, "$file_options\n", @diffs
81     or die $! ;
82
83     close ME or die $! ;
84 #    unlink "A" or warn "$! unlinking A" ;
85 #    unlink "B" or warn "$! unlinking B" ;
86     exit 0 ;
87 }
88
89 ## Ok, we're not updating, so run the tests...
90
91 my @data = split $sep, join "", <DATA> ;
92 close DATA or die $! ;
93 die "Found " . @data,
94     " elements, not ", ( @tests + 1 ),
95     ", time to --update?\n"
96     unless @data == @tests + 1 ;
97
98 my @file_options = eval "(" . shift( @data ) . ")" ;
99 die if $@ ;
100
101 my ( $mtime_A, $mtime_B ) ;
102
103 {
104     my %o = @file_options ;
105     $mtime_A = $o{MTIME_A} ;
106     $mtime_B = $o{MTIME_B} ;
107 }
108
109 plan tests => scalar @tests ;
110 for ( @tests ) {
111     my ( $diff_opts, $Diff_opts ) = @$_ ;
112     my $expect = shift @data ;
113
114     $expect =~ s/<MTIME_A>/localtime $mtime_A/e ;
115     $expect =~ s/<MTIME_B>/localtime $mtime_B/e ;
116
117     my @Diff_opts = eval "($Diff_opts)" ;
118     die if $@ ;
119
120     my $output = diff \@A, \@B, { @file_options, @Diff_opts } ;
121     if ( $output eq $expect ) {
122         ok( 1 ) ;
123     }
124     else {
125         ok( 0 ) ;
126         warn "# diff options: $diff_opts\n" ;
127         warn "# my options: $Diff_opts\n" ;
128         ## Merge the outputs using A::D
129         my @E = split /^/, $expect ;
130         my @G = split /^/, $output ;
131         my $w = length "Expected" ;
132         for ( @E, @G ) {
133             s/\n/\\n/g ;
134             $w = length if length > $w ;
135         }
136         my $fmt = "# %-${w}s %-2s %-${w}s\n" ;
137         printf STDERR $fmt, "Expected", " ", "Got" ;
138         print STDERR "# ", "-" x ( $w * 2 + 4 ), "\n" ;
139
140         my ( $E_start, $G_start ) ;
141         my $print_diff = sub {
142             my ( $E_end, $G_end ) = @_ ;
143             if ( defined $E_start || defined $G_start ) {
144                 while ( $E_start < $E_end || $G_start < $G_end ) {
145                     printf STDERR (
146                         $fmt,
147                         $E_start < $E_end ? $E[$E_start] : "",
148                         "!=",
149                         $G_start < $G_end ? $G[$G_start] : ""
150                      ) ;
151
152                      ++$E_start ;
153                      ++$G_start ;
154                 }
155                 $E_start = $G_start = undef ;
156                 
157             }
158         } ;
159
160         my $dis = sub {
161            $E_start = $_[0] unless defined $E_start ;
162            $G_start = $_[1] unless defined $G_start ;
163         } ;
164
165         traverse_sequences(
166             \@E, \@G,
167             {
168                 MATCH => sub {
169                     $print_diff->( @_ ) ;
170                     printf STDERR $fmt, $E[$_[0]], "==", $G[$_[1]] ;
171                 },
172                 DISCARD_A => $dis,
173                 DISCARD_B => $dis,
174             }
175         ) ;
176         $print_diff->( scalar @E, scalar @G ) ;
177
178         print STDERR "# ", "-" x ( $w * 2 + 4 ), "\n" ;
179         print STDERR "#\n" ;
180     }
181 }
182
183
184 __DATA__
185 FILENAME_A => "A",
186 MTIME_A => 1007983243,
187 FILENAME_B => "B",
188 MTIME_B => 1007983244,
189
190 ----8<--------8<--------8<--------8<--------8<--------8<--------8<----
191 --- A   <MTIME_A>
192 +++ B   <MTIME_B>
193 @@ -2,13 +2,13 @@
194  2
195  3
196  4
197 -5d
198 +5a
199  6
200  7
201  8
202  9
203 +9a
204  10
205  11
206 -11d
207  12
208  13
209 ----8<--------8<--------8<--------8<--------8<--------8<--------8<----
210 *** A   <MTIME_A>
211 --- B   <MTIME_B>
212 ***************
213 *** 2,14 ****
214   2
215   3
216   4
217 ! 5d
218   6
219   7
220   8
221   9
222   10
223   11
224 - 11d
225   12
226   13
227 --- 2,14 ----
228   2
229   3
230   4
231 ! 5a
232   6
233   7
234   8
235   9
236 + 9a
237   10
238   11
239   12
240   13
241 ----8<--------8<--------8<--------8<--------8<--------8<--------8<----
242 *** A   <MTIME_A>
243 --- B   <MTIME_B>
244 ***************
245 *** 5 ****
246 ! 5d
247 --- 5 ----
248 ! 5a
249 ***************
250 *** 9 ****
251 --- 10 ----
252 + 9a
253 ***************
254 *** 12 ****
255 - 11d
256 --- 12 ----
257 ----8<--------8<--------8<--------8<--------8<--------8<--------8<----
258 --- A   <MTIME_A>
259 +++ B   <MTIME_B>
260 @@ -5 +5 @@
261 -5d
262 +5a
263 @@ -9,0 +10 @@
264 +9a
265 @@ -12 +12,0 @@
266 -11d
267 ----8<--------8<--------8<--------8<--------8<--------8<--------8<----
268 5c5
269 < 5d
270 ---
271 > 5a
272 9a10
273 > 9a
274 12d12
275 < 11d