1package TestUtils; 2 3use strict; 4use warnings; 5 6use Exporter (); 7use File::Spec (); 8use File::Find (); 9 10our @ISA = qw{ Exporter }; 11our @EXPORT = qw{ 12 find_tml_files 13 json_class 14 slurp 15 test_data_directory 16 test_data_file 17}; 18 19sub 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# Prefer JSON to JSON::PP; skip if we don't have at least one 30sub json_class { 31 for (qw/JSON JSON::PP/) { 32 return $_ if eval "require $_; 1"; 33 } 34 return; 35} 36 37sub test_data_directory { 38 return File::Spec->catdir( 't', 'data' ); 39} 40 41sub test_data_file { 42 return File::Spec->catfile( test_data_directory(), shift ); 43} 44 45sub slurp { 46 my $file = shift; 47 local $/ = undef; 48 open( FILE, " $file" ) or die "open($file) failed: $!"; 49 binmode( FILE, $_[0] ) if @_ > 0; 50 # binmode(FILE); # disable perl's BOM interpretation 51 my $source = <FILE>; 52 close( FILE ) or die "close($file) failed: $!"; 53 $source; 54} 55 561; 57