1use strict;
2use warnings;
3use lib 't/lib/';
4use Test::More 0.88;
5use SubtestCompat;
6use TestUtils;
7use TestBridge;
8
9use CPAN::Meta::YAML ();
10
11#--------------------------------------------------------------------------#
12# Generally, read_string can be tested with .tml files in t/tml-local/*
13#
14# This file is for error tests that can't be easily tested via .tml
15#--------------------------------------------------------------------------#
16
17subtest 'read_string without arg' => sub {
18    eval { CPAN::Meta::YAML->read_string(); };
19    error_like(qr/Did not provide a string to load/,
20        "Got expected error: no string provided to read_string()"
21    );
22};
23
24subtest 'YAML without newline' => sub {
25    my $str = join("\n" => ('---', '- foo', '---', '- bar', '---'));
26    my $obj = eval { CPAN::Meta::YAML->read_string($str); };
27    is( $@, '', "YAML without newline is OK");
28};
29
30subtest 'read_string as object method' => sub {
31    ok( my $obj = CPAN::Meta::YAML->new( { foo => 'bar' } ), "new YAML object" );
32    ok( my $obj2 = $obj->read_string( "---\nfoo: bar\n"  ),
33        "read_string object method"
34    );
35    isnt( $obj, $obj2, "objects are different" );
36    cmp_deeply( $obj, $obj2, "objects have same content" );
37};
38
39subtest 'invalid UTF-8' => sub {
40    # get invalid UTF-8 by reading Latin-1 with lax :utf8 layer
41    my $string = do {
42        local $SIG{__WARN__} = sub {};
43        slurp( test_data_file('latin1.yml'), ":utf8" );
44    };
45    my $obj = eval { CPAN::Meta::YAML->read_string($string); };
46    is( $obj, undef, "read_string should return undef" );
47    error_like( qr/invalid UTF-8 string/,
48        "Got expected error about invalid UTF-8 string"
49    );
50};
51
52done_testing;
53