Commit | Line | Data |
be96f5c3 |
1 | package Parse::CPAN::Meta::Test; |
2 | |
3 | use strict; |
4 | use Test::More (); |
5 | use Parse::CPAN::Meta; |
6 | use File::Spec; |
7 | |
8 | use vars qw{@ISA @EXPORT}; |
9 | BEGIN { |
10 | require Exporter; |
11 | @ISA = qw{ Exporter }; |
de044c36 |
12 | @EXPORT = qw{ |
13 | tests yaml_ok yaml_error slurp load_ok |
14 | test_data_directory |
15 | }; |
be96f5c3 |
16 | } |
17 | |
18 | sub test_data_directory { |
19 | return( |
20 | $ENV{PERL_CORE} |
21 | ? File::Spec->catdir(File::Spec->updir, qw(lib Parse CPAN Meta t data)) |
22 | : File::Spec->catdir(qw(t data)) |
23 | ); |
24 | } |
25 | |
26 | # 22 tests per call to yaml_ok |
27 | # 4 tests per call to load_ok |
28 | sub tests { |
29 | return ( tests => count(@_) ); |
30 | } |
31 | |
32 | sub count { |
33 | my $yaml_ok = shift || 0; |
34 | my $load_ok = shift || 0; |
35 | my $single = shift || 0; |
36 | my $count = $yaml_ok * 3 + $load_ok * 4 + $single; |
37 | return $count; |
38 | } |
39 | |
40 | sub yaml_ok { |
41 | my $string = shift; |
42 | my $array = shift; |
43 | my $name = shift || 'unnamed'; |
44 | |
45 | # Does the string parse to the structure |
46 | my $yaml_copy = $string; |
47 | my @yaml = eval { Parse::CPAN::Meta::Load( $yaml_copy ); }; |
48 | Test::More::is( $@, '', "$name: Parse::CPAN::Meta parses without error" ); |
49 | Test::More::is( $yaml_copy, $string, "$name: Parse::CPAN::Meta does not modify the input string" ); |
50 | SKIP: { |
51 | Test::More::skip( "Shortcutting after failure", 1 ) if $@; |
52 | Test::More::is_deeply( \@yaml, $array, "$name: Parse::CPAN::Meta parses correctly" ); |
53 | } |
54 | |
55 | # Return true as a convenience |
56 | return 1; |
57 | } |
58 | |
de044c36 |
59 | sub yaml_error { |
60 | my $string = shift; |
61 | my $yaml = eval { Parse::CPAN::Meta::Load( $string ); }; |
62 | Test::More::like( $@, qr/$_[0]/, "YAML::Tiny throws expected error" ); |
63 | } |
64 | |
be96f5c3 |
65 | sub slurp { |
66 | my $file = shift; |
67 | local $/ = undef; |
68 | open( FILE, " $file" ) or die "open($file) failed: $!"; |
69 | my $source = <FILE>; |
70 | close( FILE ) or die "close($file) failed: $!"; |
71 | $source; |
72 | } |
73 | |
74 | sub load_ok { |
75 | my $name = shift; |
76 | my $file = shift; |
77 | my $size = shift; |
78 | Test::More::ok( -f $file, "Found $name" ) or Test::More::diag("Searched at '$file'"); |
79 | Test::More::ok( -r $file, "Can read $name" ); |
80 | my $content = slurp( $file ); |
81 | Test::More::ok( (defined $content and ! ref $content), "Loaded $name" ); |
82 | Test::More::ok( ($size < length $content), "Content of $name larger than $size bytes" ); |
83 | return $content; |
84 | } |
85 | |
86 | 1; |