Debian Codegraph @

cpan/CPAN-Meta-YAML/t/10_read.t

raw
1 use strict;
2 use warnings;
3 use utf8;
4 use lib 't/lib/';
5 use Test::More 0.88;
6 use SubtestCompat;
7 use TestUtils;
8 use TestBridge;
9
10 use CPAN::Meta::YAML;
11
12 #--------------------------------------------------------------------------#
13 # read() should read these files without error
14 #--------------------------------------------------------------------------#
15
16 my %passes = (
17 array => {
18 file => 'ascii.yml',
19 perl => [
20 [ 'foo' ]
21 ],
22 },
23 'multibyte UTF-8' => {
24 file => 'multibyte.yml',
25 perl => [
26 { author => 'Ævar Arnfjörð Bjarmason <[email protected]>' }
27 ],
28 utf8 => 'author',
29 },
30 'UTF-8 BOM' => {
31 file => 'utf_8_bom.yml',
32 perl => [
33 { author => 'Ævar Arnfjörð Bjarmason <[email protected]>' }
34 ],
35 utf8 => 'author',
36 },
37 );
38
39 for my $key ( sort keys %passes ) {
40 subtest $key => sub {
41 my $case = $passes{$key};
42 my $file = test_data_file( $case->{file} );
43 ok( -f $file, "Found $case->{file}" );
44
45 my $got = eval { CPAN::Meta::YAML->read( $file ) };
46 is( $@, '', "CPAN::Meta::YAML reads without exception" );
47 SKIP: {
48 skip( "Shortcutting after failure", 2 ) if $@;
49 isa_ok( $got, 'CPAN::Meta::YAML' )
50 or diag "ERROR: " . CPAN::Meta::YAML->errstr;
51 cmp_deeply( $got, $case->{perl}, "CPAN::Meta::YAML parses correctly" );
52 }
53
54 if ( $case->{utf8} ) {
55 ok( utf8::is_utf8( $got->[0]->{$case->{utf8}} ), "utf8 decoded" );
56 }
57
58 # test that read method on object is also a constructor
59 ok( my $got2 = eval { $got->read( $file ) }, "read() object method");
60 isnt( $got, $got2, "objects are different" );
61 cmp_deeply( $got, $got2, "objects have same content" );
62 }
63 }
64
65 #--------------------------------------------------------------------------#
66 # read() should fail to read these files and provide expected errors
67 #--------------------------------------------------------------------------#
68
69 my %errors = (
70 'latin1.yml' => qr/latin1\.yml.*does not map to Unicode/,
71 'utf_16_le_bom.yml' => qr/utf_16_le_bom\.yml.*does not map to Unicode/,
72 );
73
74 for my $key ( sort keys %errors ) {
75 subtest $key => sub {
76 my $file = test_data_file( $key );
77 ok( -f $file, "Found $key" );
78
79 my $result = eval { CPAN::Meta::YAML->read( $file ) };
80 ok( !$result, "returned false" );
81 error_like( $errors{$key}, "Got expected error" );
82 };
83 }
84
85 # Additional errors without a file to read
86
87 subtest "bad read arguments" => sub {
88 eval { CPAN::Meta::YAML->read(); };
89 error_like(qr/You did not specify a file name/,
90 "Got expected error: no filename provided to read()"
91 );
92
93 eval { CPAN::Meta::YAML->read( test_data_file('nonexistent.yml') ); };
94 error_like(qr/File '.*?' does not exist/,
95 "Got expected error: nonexistent filename provided to read()"
96 );
97
98 eval { CPAN::Meta::YAML->read( test_data_directory() ); };
99 error_like(qr/'.*?' is a directory, not a file/,
100 "Got expected error: directory provided to read()"
101 );
102 };
103
104 done_testing;
105 # COPYRIGHT
106 # vim: ts=4 sts=4 sw=4 et: