#!/usr/bin/perl -w # $Id$ use strict; use Test::More tests => 17; use Term::ANSIColor qw(:constants); use File::Spec::Functions qw(catfile); use IO::File; use lib qw( /Users/gwg/Text-WordDiff-0.05/lib ); # BEGIN { use_ok 'Text::WordDiff' or die; use_ok 'Text::WordDiff::HTMLTwoLines' or die; # } my $string1 = 'This is a test'; my $string2 = 'That was a test'; my $term_diff = '
This is a test
' . "\n" . '
That was a test
' . "\n"; my %opts = ( STYLE => 'HTMLTwoLines', ); # Test scalar refs. is word_diff(\$string1, \$string2, \%opts), $term_diff, 'Should HTML diff'; # Try code refs. is word_diff( sub { \$string1 }, sub { \$string2 } , \%opts ), $term_diff, 'Should get same result for code refs'; # Try array refs. my $BEGIN_WORD = qr/(?--- $filename1 $time1}; my $header2 = qq{+++ $filename2 $time2}; my $file_diff = qq{
$header1} . qq{This is a tst;} . qq{\n} . qq{it is only a\n} . qq{test. Had it been an\n} . qq{actual diff, the results would\n} . qq{have been output to HTML} . qq{.\n\nSome string with } . qq{funny \$} . qq{\nchars in the end} . qq{*\n
\n} . qq{
$header2} . qq{This is a test.} . qq{\n} . qq{It is only a\n} . qq{test. Had this been an\n} . qq{actual diff, the results would\n} . qq{have been output to the terminal} . qq{.\n\nSome string with } . qq{funny \@} . qq{\nchars in the end} . qq{?\n
\n}; is word_diff($filename1, $filename2, \%opts), $file_diff, 'Diff by file name should include a header'; # No more header after this. $file_diff =~ s/\Q$header1\E//; $file_diff =~ s/\Q$header2\E//; # Try globs. local (*FILE1, *FILE2); open *FILE1, "<$filename1" or die qq{Cannot open "$filename1": $!}; open *FILE2, "<$filename2" or die qq{Cannot open "$filename2": $!}; is word_diff(\*FILE1, \*FILE2, \%opts), $file_diff, 'Diff by glob file handles should work'; close *FILE1; close *FILE2; # Try file handles. my $fh1 = IO::File->new($filename1, '<') or die qq{Cannot open "$filename1": $!}; my $fh2 = IO::File->new($filename2, '<') or die qq{Cannot open "$filename2": $!}; is word_diff($fh1, $fh2, \%opts), $file_diff, 'Diff by IO::File objects should work'; $fh1->close; $fh2->close; # Try a code refence output handler. my $output = ''; $opts{OUTPUT} = sub { $output .= shift }; is word_diff(\$string1, \$string2, \%opts ), 2, 'Should get a count of 2 hunks with code ref output'; is $output, $term_diff, 'The code ref should have been called'; # Try a scalar ref output handler. $output = ''; $opts{OUTPUT} = \$output; is word_diff(\$string1, \$string2, \%opts), 2, 'Should get a count of 2 hunks with scalar ref output'; is $output, $term_diff, 'The scalar ref should have been appended to'; # Try an array ref output handler. my $hunks = []; $opts{OUTPUT} = $hunks; is word_diff(\$string1, \$string2, \%opts), 2, 'Should get a count of 2 hunks with array ref output'; is join('', @$hunks), $term_diff, 'The array ref should have been appended to'; # Try a file handle output handler. my $fh = IO::File->new_tmpfile; SKIP: { skip 'Cannot create temp filehandle', 2 unless $fh; $opts{OUTPUT} = $fh; is word_diff(\$string1, \$string2, \%opts), 2, 'Should get a count of 2 hunks with file handle output'; $fh->seek(0, 0); is do { local $/; <$fh>; }, $term_diff, 'The file handle should have been written to'; }