1package Text::Levenshtein::TestUtils; 2 3use strict; 4use warnings; 5 6# This line is needed before we use Test::More, to suppress warnings 7# about "Wide character in print" from Test::Builder 8use open ':std', ':encoding(utf8)'; 9 10use Test::More 0.88; 11use Text::Levenshtein qw/ distance fastdistance /; 12use parent 'Exporter'; 13use Carp; 14 15our @EXPORT_OK = qw/ run_data_tests /; 16 17sub run_data_tests 18{ 19 my $opt = {}; 20 my $package = (caller(0))[0]; 21 my $distance; 22 my $fh; 23 my @extra; 24 25 @extra = @_; 26 27 $fh = do { 28 no strict 'refs'; 29 \*{"${package}::DATA"}; 30 }; 31 32 my @tests = parse_tests($fh); 33 34 plan tests => 4 * @tests; 35 36 foreach my $test (@tests) { 37 $distance = distance($test->{word1}, $test->{word2}, @extra); 38 ok($distance == $test->{distance}, 39 "$test->{title} (distance)"); 40 41 $distance = distance($test->{word2}, $test->{word1}, @extra); 42 ok($distance == $test->{distance}, 43 "$test->{title} (reverse distance)"); 44 45 $distance = fastdistance($test->{word1}, $test->{word2}, @extra); 46 ok($distance == $test->{distance}, 47 "$test->{title} (fastdistance)"); 48 49 $distance = fastdistance($test->{word2}, $test->{word1}, @extra); 50 ok($distance == $test->{distance}, 51 "$test->{title} (reverse fastdistance)"); 52 } 53 54} 55 56sub parse_tests 57{ 58 my $fh = shift; 59 my @tests; 60 my @fields; 61 local $_; 62 63 while (<$fh>) { 64 next if /^--/; # test case divider 65 next if /^#/; # comment 66 chomp; 67 push(@fields, $_); 68 if (@fields == 3) { 69 my ($word1, $word2, $expected_distance) = @fields; 70 push(@tests, { title => "$word1 vs $word2", 71 word1 => $word1, 72 word2 => $word2, 73 distance => $expected_distance, 74 }); 75 @fields = (); 76 } 77 } 78 return @tests; 79} 80 811; 82