| 1 |
package TestUtils; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Exporter (); |
| 7 |
use File::Spec (); |
| 8 |
use File::Find (); |
| 9 |
|
| 10 |
our @ISA = qw{ Exporter }; |
| 11 |
our @EXPORT = qw{ |
| 12 |
find_tml_files |
| 13 |
json_class |
| 14 |
slurp |
| 15 |
test_data_directory |
| 16 |
test_data_file |
| 17 |
}; |
| 18 |
|
| 19 |
sub find_tml_files { |
| 20 |
my $dir = shift; |
| 21 |
my @files; |
| 22 |
File::Find::find( |
| 23 |
sub { push @files, $File::Find::name if -f and /\.tml$/ }, |
| 24 |
$dir |
| 25 |
); |
| 26 |
return @files; |
| 27 |
} |
| 28 |
|
| 29 |
sub json_class { |
| 30 |
return eval { require JSON::MaybeXS; JSON::MaybeXS->VERSION('1.001000'); $JSON::MaybeXS::JSON_Class } |
| 31 |
|| do { require JSON::PP; 'JSON::PP' }; |
| 32 |
} |
| 33 |
|
| 34 |
sub test_data_directory { |
| 35 |
return File::Spec->catdir( 't', 'data' ); |
| 36 |
} |
| 37 |
|
| 38 |
sub test_data_file { |
| 39 |
return File::Spec->catfile( test_data_directory(), shift ); |
| 40 |
} |
| 41 |
|
| 42 |
sub slurp { |
| 43 |
my $file = shift; |
| 44 |
local $/ = undef; |
| 45 |
open( FILE, " $file" ) or die "open($file) failed: $!"; |
| 46 |
binmode( FILE, $_[0] ) if @_ > 0; |
| 47 |
# binmode(FILE); # disable perl's BOM interpretation |
| 48 |
my $source = <FILE>; |
| 49 |
close( FILE ) or die "close($file) failed: $!"; |
| 50 |
$source; |
| 51 |
} |
| 52 |
|
| 53 |
1; |