File: load-blessed.t

package info (click to toggle)
libyaml-libyaml-perl 0.76%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 688 kB
  • sloc: perl: 1,603; ansic: 1,140; sh: 29; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,710 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
use FindBin '$Bin';
use lib $Bin;
use TestYAMLTests tests => 15;

my $yaml = <<"EOM";
local_array: !Foo::Bar [a]
local_hash: !Foo::Bar { a: 1 }
local_scalar: !Foo::Bar a
hash: !!perl/hash:Foo::Bar { a: 1 }
array: !!perl/array:Foo::Bar [a]
regex: !!perl/regexp:Foo::Bar OK
scalar: !!perl/scalar:Foo::Bar scalar
EOM

my $objects = Load $yaml;
isa_ok($objects->{local_array}, "Foo::Bar", "local tag (array)");
isa_ok($objects->{local_hash}, "Foo::Bar", "local tag (hash)");
isa_ok($objects->{local_scalar}, "Foo::Bar", "local tag (scalar)");
isa_ok($objects->{array}, "Foo::Bar", "perl tag (array)");
isa_ok($objects->{hash}, "Foo::Bar", "perl tag (hash)");
isa_ok($objects->{regex}, "Foo::Bar", "perl tag (regexp)");
isa_ok($objects->{scalar}, "Foo::Bar", "perl tag (scalar)");

local $YAML::XS::LoadBlessed = 0;
my $hash = Load $yaml;
cmp_ok(ref $hash->{local_array}, 'eq', 'ARRAY', "Array not blessed (local)");
cmp_ok(ref $hash->{local_hash}, 'eq', 'HASH', "Hash not blessed (local)");
cmp_ok(ref $hash->{local_scalar}, 'eq', '', "Scalar not blessed (local)");
cmp_ok(ref $hash->{array}, 'eq', 'ARRAY', "Array not blessed");
cmp_ok(ref $hash->{hash}, 'eq', 'HASH', "Hash not blessed");
cmp_ok(ref $hash->{regex}, 'eq', 'Regexp', "Regexp not blessed");
cmp_ok(ref $hash->{scalar}, 'eq', '', "Scalar not blessed");

my $expected = {
    local_array => ["a"],
    local_hash => { a => 1 },
    local_scalar => "a",
    hash => { a => 1 },
    array => ["a"],
    regex => qr{OK},
    scalar => "scalar",
};
if ($hash->{regex} =~ m/:OK/) {
    $hash->{regex} = $expected->{regex};
}
is_deeply($hash, $expected);

# !!perl/glob and !!perl/ref aren't blessed at the moment.
# !!perl/code isn't loaded at the moment