File: 10_read.t

package info (click to toggle)
libyaml-tiny-perl 1.64-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 556 kB
  • ctags: 80
  • sloc: perl: 1,363; makefile: 2
file content (105 lines) | stat: -rw-r--r-- 3,144 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use strict;
use warnings;
use utf8;
use lib 't/lib/';
use Test::More 0.99;
use TestUtils;
use TestBridge;

use YAML::Tiny;

#--------------------------------------------------------------------------#
# read() should read these files without error
#--------------------------------------------------------------------------#

my %passes = (
    array => {
        file => 'ascii.yml',
        perl => [
            [ 'foo' ]
        ],
    },
    'multibyte UTF-8' => {
        file => 'multibyte.yml',
        perl => [
            { author => 'Ævar Arnfjörð Bjarmason <avar@cpan.org>' }
        ],
        utf8 => 'author',
    },
    'UTF-8 BOM' => {
        file => 'utf_8_bom.yml',
        perl => [
            { author => 'Ævar Arnfjörð Bjarmason <avar@cpan.org>' }
        ],
        utf8 => 'author',
    },
);

for my $key ( sort keys %passes ) {
    subtest $key => sub {
        my $case = $passes{$key};
        my $file = test_data_file( $case->{file} );
        ok( -f $file, "Found $case->{file}" );

        my $got = eval { YAML::Tiny->read( $file ) };
        is( $@, '', "YAML::Tiny reads without exception" );
        SKIP: {
            skip( "Shortcutting after failure", 2 ) if $@;
            isa_ok( $got, 'YAML::Tiny' )
                or diag "ERROR: " . YAML::Tiny->errstr;
            cmp_deeply( $got, $case->{perl}, "YAML::Tiny parses correctly" );
        }

        if ( $case->{utf8} ) {
            ok( utf8::is_utf8( $got->[0]->{$case->{utf8}} ), "utf8 decoded" );
        }

        # test that read method on object is also a constructor
        ok( my $got2 = eval { $got->read( $file ) }, "read() object method");
        isnt( $got, $got2, "objects are different" );
        cmp_deeply( $got, $got2, "objects have same content" );
    }
}

#--------------------------------------------------------------------------#
# read() should fail to read these files and provide expected errors
#--------------------------------------------------------------------------#

my %errors = (
    'latin1.yml' => qr/latin1\.yml.*does not map to Unicode/,
    'utf_16_le_bom.yml' => qr/utf_16_le_bom\.yml.*does not map to Unicode/,
);

for my $key ( sort keys %errors ) {
    subtest $key => sub {
        my $file = test_data_file( $key );
        ok( -f $file, "Found $key" );

        my $result = eval { YAML::Tiny->read( $file ) };
        ok( !$result, "returned false" );
        error_like( $errors{$key}, "Got expected error" );
    };
}

# Additional errors without a file to read

subtest "bad read arguments" => sub {
    eval { YAML::Tiny->read(); };
    error_like(qr/You did not specify a file name/,
        "Got expected error: no filename provided to read()"
    );

    eval { YAML::Tiny->read( test_data_file('nonexistent.yml') ); };
    error_like(qr/File '.*?' does not exist/,
        "Got expected error: nonexistent filename provided to read()"
    );

    eval { YAML::Tiny->read( test_data_directory() ); };
    error_like(qr/'.*?' is a directory, not a file/,
        "Got expected error: directory provided to read()"
    );
};

done_testing;
# COPYRIGHT
# vim: ts=4 sts=4 sw=4 et: