File: TestUtils.pm

package info (click to toggle)
libcpan-meta-yaml-perl 0.012-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 384 kB
  • ctags: 76
  • sloc: perl: 1,300; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package TestUtils;

use strict;
use warnings;

use Exporter   ();
use File::Spec ();
use File::Find ();

our @ISA    = qw{ Exporter };
our @EXPORT = qw{
    find_tml_files
    json_class
    slurp
    test_data_directory
    test_data_file
};

sub find_tml_files {
    my $dir = shift;
    my @files;
    File::Find::find(
        sub { push @files, $File::Find::name if -f and /\.tml$/ },
        $dir
    );
    return @files;
}

# Prefer JSON to JSON::PP; skip if we don't have at least one
sub json_class {
    for (qw/JSON JSON::PP/) {
        return $_ if eval "require $_; 1";
    }
    return;
}

sub test_data_directory {
    return File::Spec->catdir( 't', 'data' );
}

sub test_data_file {
    return File::Spec->catfile( test_data_directory(), shift );
}

sub slurp {
    my $file = shift;
    local $/ = undef;
    open( FILE, " $file" ) or die "open($file) failed: $!";
    binmode( FILE, $_[0] ) if @_ > 0;
    # binmode(FILE); # disable perl's BOM interpretation
    my $source = <FILE>;
    close( FILE ) or die "close($file) failed: $!";
    $source;
}

1;