xref: /openbsd-src/gnu/usr.bin/perl/cpan/CPAN-Meta-YAML/t/lib/TestUtils.pm (revision b8851fcc53cbe24fd20b090f26dd149e353f6174)
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
29sub 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
34sub test_data_directory {
35    return File::Spec->catdir( 't', 'data' );
36}
37
38sub test_data_file {
39    return File::Spec->catfile( test_data_directory(), shift );
40}
41
42sub 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
531;
54