File: 01-diff.t

package info (click to toggle)
libhtml-diff-perl 0.600-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, stretch
  • size: 104 kB
  • ctags: 15
  • sloc: perl: 520; makefile: 8
file content (407 lines) | stat: -rwxr-xr-x 11,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/perl

use strict;

use Test;

BEGIN { plan tests => 10 }

use Getopt::Long;
my ($verbose);
GetOptions("verbose!" => \$verbose) or die "Parsing command line failed.";

use Data::Dumper;

use HTML::Diff qw(line_diff word_diff html_word_diff);

my $test_text_a = "Four score
and seven years ago, our forefathers 
brought forth on this continent
a new nation conceived 
in liberty and
dedicated to the proposition
that all men are created equal.
Now our great nation is
engaged in civil war.";

my $test_text_b = "Four score
and seven years ago, our forefathers 
brought forth on this continent
dedicated to the proposition
that all men are created equal.
Now our great nation is enagaged
in civil war";

my $test_text_c = 'PEOPLE said, "The evening-bell is sounding, the sun is setting." A strange wondrous tone was heard in the narrow streets of a large town. It was like the sound of a church-bell: but it was only heard for a moment, for the rolling of the carriages, and the voices of the multitude made too great a noise.';

my $test_text_d = 'PEOPLE said, "The bell is sounding." A strange wondrous was heard tone in the narrow streets of a large town. A long time passed. It was like the sound of a church-bell: but it was only heard for a moment, for the rolling of the carriages, and the voices of the multitude made too great a noise.';

# This next pair tests the usual HTML changes 
# (e.g. "<b>a b c d</b>" -> "<i>a b c d</i>" considers all of "a b c d" 
# as a change). It also tests that whitespace changes are effectively ignored.

my $test_html_a = '<BASE HREF= "http://some-site.com/index.html">
<center>
<h1>
<a href="http://www.cs.brown.edu/people/jes/acm.strategic.dirs.html">Strategic Directions for <b> Research in Theory of Computing </b></a>
</h1>

<!-- This is an HTML comment -->

September 23, 1996
</center>

<p>

< this is an unkown html tag />

Anne Condon, University of Wisconsin <br>
Faith Fich, University of Toronto <br>
Greg N. Frederickson, Purdue University <br>
Andrew V. Goldberg, NEC Research Institute <br>
David S. Johnson, AT&amp;T Bell Laboratories <br>
Michael C. Loui, University of Illinois at Urbana-Champaign  <br>
Steven Mahaney, DIMACS <P>

Prabhakar Raghavan, IBM Almaden Research Center <br>
John Savage, Brown University <br>
Alan Selman, SUNY at Buffalo <br>
David B. Shmoys, Cornell University 
<p>

<strong>Abstract.</strong>
This report focuses on two core areas of theory of computing:
discrete algorithms and computational complexity theory.
The report 
reviews the purposes and goals of theoretical research,
summarizes selected past and recent achievements,
<i>explains the importance of</i> sustaining core research,
and identifies promising opportunities for future research.
Some research opportunities build bridges between
theory of computing and other areas of computer science,
and other science and engineering disciplines.
<p>';

my $test_html_b = '<center>
<h1>
<a href="http://www.google.com/">Strategic Directions for <i> Research in Theory of Computing </i></a>
</h1>

September 23, 1996
</center>

<p>

Anne Condon, University of Wisconsin <br>
Faith Fich, University of Toronto <br>
Greg N. Frederickson, Purdue University <br>
Andrew V. Goldberg, NEC Research Institute <br>
David S. Johnson, AT&amp;T Bell Laboratories <br>
Michael C. Loui, University of Illinois at Urbana-Champaign  <br>
Steven Mahaney, DIMACS <br>

Prabhakar Raghavan, IBM Almaden Research Center <br>
John Savage, Brown University <br>
Alan Selman, SUNY at Buffalo <br>
David B. Shmoys, Cornell University 
<p>

<strong>Abstract.</strong> This report focuses on two core areas of
theory of computing: discrete algorithms and computational complexity
theory.  The report reviews the purposes and goals of theoretical
research, summarizes selected past and recent achievements, explains
the importance of sustaining core research, and identifies promising
opportunities for future research.  Some research opportunities build
bridges between theory of computing and other areas of computer
science, and other science and engineering disciplines.  <p>';

sub print_diff {
    my $ch;
    my ($chunks) = @_;
    foreach $ch (@$chunks) {
	my ($flag, $m, $o) = @$ch;
	unless ($flag eq 'u') {
	    print "<< old\n";
	    print "$o";
	    print ">> new\n";
	    print "$m";
	    print "==\n";
	    # TBD: make some kind of warning about lacking a newline at the end
	} else {
	    print "$m";
	}
    }
}

sub test_diff_continuity {
    my ($a, $b, $diffalgo, $ignore_whitespace) = @_;
    my $chunks = &$diffalgo($a, $b);
    my ($runningb, $runninga);
    $runninga = $runningb = "";
    my $ch;
    foreach $ch (@$chunks)
    {
	my ($flag, $ach, $bch) = @$ch;
	$runninga .= $ach || '';
	$runningb .= $bch || '';
    }
    if ($ignore_whitespace) {
	$a =~ s/\s\s+/ /g;
	$b =~ s/\s\s+/ /g;
	$runninga =~ s/\s\s+/ /g;
	$runningb =~ s/\s\s+/ /g;
    }
    return ($a eq $runninga) && ($b eq $runningb);
}

sub expect_diff {
    my ($a, $b, $algo, $expectation) = @_;
}

if ($verbose) {
    my $chunks = HTML::Diff::line_diff($test_text_a, $test_text_b);
    print_diff($chunks);
    $chunks = HTML::Diff::word_diff($test_text_c, $test_text_d);
    print "\n";
    print_diff($chunks);
}

ok(deep_compare(html_word_diff('', ''), [['', undef, undef]]));
ok(deep_compare(html_word_diff('0', ''), [['-', '0', '']]));
ok(deep_compare(html_word_diff('', '0'), [['+', '', '0']]));
ok(deep_compare(html_word_diff('0', '0'), [['u', '0', '0']]));
ok(deep_compare(html_word_diff('a b b', 'b b c'),
		[['-', 'a ', ''],
		 ['u', 'b ', 'b '],
		 ['c', 'b', 'b c']]));
   
print "Testing line_diff on test_text_a and test_text_b\n" if $verbose;
ok(test_diff_continuity($test_text_a, $test_text_b, 
			\&HTML::Diff::line_diff));
print "Testing html_word_diff on test_text_a and test_text_b\n"
    if $verbose;
ok(test_diff_continuity($test_text_a, $test_text_b, 
			\&HTML::Diff::html_word_diff));
print "Testing html_word_diff on test_html_a and test_html_b\n" 
    if $verbose;
ok(test_diff_continuity($test_html_a, $test_html_b, 
			\&HTML::Diff::html_word_diff, 1));
my $result = HTML::Diff::html_word_diff($test_html_a, $test_html_b);

# Use the following lines to capture a "correct" result (when you
# think you've got one) which can be used to validate future tests

# open OUT, ">expect";
# print OUT Dumper($result);
# close OUT;

# This value is the result we expect from HTML::Diff::html_word_diff()
# If the actual result differs by one byte, it's a failure.
# When the diff code is changed, you'll need to calculate a new expected
#   value using the lines above, and paste the resulting value below.

my $expect = [
    [
     '-',
     '<BASE HREF= "http://some-site.com/index.html">
',
     ''
    ],
    [
     'u',
     '<center>
<h1>
',
     '<center>
<h1>
'
    ],
    [
     'c',
     '<a href="http://www.cs.brown.edu/people/jes/acm.strategic.dirs.html">',
     '<a href="http://www.google.com/">'
    ],
    [
     'u',
     'Strategic Directions for ',
     'Strategic Directions for '
    ],
    [
     'c',
     '<b> Research in Theory of Computing </b>',
     '<i> Research in Theory of Computing </i>'
    ],
    [
     'u',
     '</a>
</h1>

',
     '</a>
</h1>

'
    ],
    [
     '-',
     '<!-- This is an HTML comment -->

',
     ''
    ],
    [
     'u',
     'September 23, 1996
</center>

<p>

',
     'September 23, 1996
</center>

<p>

'
    ],
    [
     '-',
     '< this is an unkown html tag />

',
     ''
    ],
    [
     'u',
     'Anne Condon, University of Wisconsin <br>
Faith Fich, University of Toronto <br>
Greg N. Frederickson, Purdue University <br>
Andrew V. Goldberg, NEC Research Institute <br>
David S. Johnson, AT&amp;T Bell Laboratories <br>
Michael C. Loui, University of Illinois at Urbana-Champaign  <br>
Steven Mahaney, DIMACS ',
     'Anne Condon, University of Wisconsin <br>
Faith Fich, University of Toronto <br>
Greg N. Frederickson, Purdue University <br>
Andrew V. Goldberg, NEC Research Institute <br>
David S. Johnson, AT&amp;T Bell Laboratories <br>
Michael C. Loui, University of Illinois at Urbana-Champaign  <br>
Steven Mahaney, DIMACS '
    ],
    [
     'c',
     '<P>

',
     '<br>

'
    ],
    [
     'u',
     'Prabhakar Raghavan, IBM Almaden Research Center <br>
John Savage, Brown University <br>
Alan Selman, SUNY at Buffalo <br>
David B. Shmoys, Cornell University 
<p>

<strong>Abstract.</strong>
This report focuses on two core areas of theory of computing:
discrete algorithms and computational complexity theory.
The report 
reviews the purposes and goals of theoretical research,
summarizes selected past and recent achievements,
',
     'Prabhakar Raghavan, IBM Almaden Research Center <br>
John Savage, Brown University <br>
Alan Selman, SUNY at Buffalo <br>
David B. Shmoys, Cornell University 
<p>

<strong>Abstract.</strong> This report focuses on two core areas of
theory of computing: discrete algorithms and computational complexity
theory.  The report reviews the purposes and goals of theoretical
research, summarizes selected past and recent achievements, '
    ],
    [
     'c',
     '<i>explains the importance of</i> ',
     'explains
the importance of '
    ],
    [
     'u',
     'sustaining core research,
and identifies promising opportunities for future research.
Some research opportunities build bridges between
theory of computing and other areas of computer science,
and other science and engineering disciplines.
<p>',
     'sustaining core research, and identifies promising
opportunities for future research.  Some research opportunities build
bridges between theory of computing and other areas of computer
science, and other science and engineering disciplines.  <p>'
    ],
];

ok(deep_compare($result, $expect));

# Given two array refs of array refs, of array refs...  return true if
# the two structures are isomorphic and all the corresponding scalars
# are equal
# TBD: make it more efficient; builds up call stack too much.
# TBD: Take a binary test as an arg, to replace eq
sub deep_compare {
    my ($a, $b) = @_;
    my ($x, $y);
    if (!ref($a) && !ref($b)) {
	return $a eq $b;
    } else {
	return 0 unless ((ref($a) eq 'ARRAY') && (ref($b) eq 'ARRAY'));
	while ($x = shift @$a) {
	    $y = shift @$b;
	    return 0 unless deep_compare($x, $y);
	}
    }
    return 1;
}

my $diffchunks = HTML::Diff::html_word_diff($test_html_a, $test_html_b);
if ($verbose) {
    print "Result of diff:\n";
    print "[$_]\n" foreach (map {join "||", @$_} @$diffchunks);
}

sub check_diff_integrity {
    my $failure = 0;
    foreach my $chunk (@{$_[0]}) {
	my ($mark, $left, $right) = @$chunk;
	if ($mark ne 'u' && $left eq $right) {
	    print "[$left] is [$right] but HTML::Diff thinks they're different!\n";
	    $failure = 1;
	}
    }
    return !$failure;
}

my $A = "<UL><li>monkey</UL><P>Search</P>";
my $B = "<UL><li>monkey</UL><UL><li>llama</UL><P>Search</P>";

$result = html_word_diff($A, $B);
ok(check_diff_integrity($result));

sub diff_file {
    my ($left, $right) = @_;
    open LEFT, $left;
    open RIGHT, $right;
    $/ = undef;
    my $Left = <LEFT>;
    my $Right = <RIGHT>;
    close LEFT;
    close RIGHT;
    my $diff_chunks = html_word_diff($Left, $Right);
    print_diff($diff_chunks);
    print "\n";
}

1;