1use strict;
2use warnings;
3use utf8;
4use lib 't/lib/';
5use Test::More 0.88;
6use SubtestCompat;
7use TestUtils;
8use TestBridge;
9
10use CPAN::Meta::YAML;
11
12#--------------------------------------------------------------------------#
13# read() should read these files without error
14#--------------------------------------------------------------------------#
15
16my %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 <avar@cpan.org>' }
27        ],
28        utf8 => 'author',
29    },
30    'UTF-8 BOM' => {
31        file => 'utf_8_bom.yml',
32        perl => [
33            { author => 'Ævar Arnfjörð Bjarmason <avar@cpan.org>' }
34        ],
35        utf8 => 'author',
36    },
37);
38
39for 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
69my %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
74for 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
87subtest "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
104done_testing;
105# COPYRIGHT
106# vim: ts=4 sts=4 sw=4 et:
107