summaryrefslogtreecommitdiff
path: root/t/marshall.t
blob: d64258d5f3e929fbed033242b8ac96e62b3050d2 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use strict;
use lib -e 't' ? 't' : 'test';
use TestYAML tests => 10;

use strict;
use warnings;

#-------------------------------------------------------------------------------
package Foo::Bar;
BEGIN {
    require TestYAMLBase;
    @Foo::Bar::ISA = 'TestYAMLBase';
}
use YAML::Marshall;

sub yaml_dump {
    my $self = shift;
    my $array = [];
    for my $k (sort keys %$self) {
        push @$array, $k, $self->{$k};
    }
    $self->yaml_node($array, 'perl/Foo::Bar');
}

sub yaml_load {
    my $class = shift;
    my $node = shift;
    my $self = $class->new;
    %$self = @$node;
    return $self;
}

#-------------------------------------------------------------------------------
package Bar::Baz;
BEGIN {
    require TestYAMLBase;
    @Bar::Baz::ISA = 'TestYAMLBase';
}
use YAML::Marshall 'random/object:bar.baz';

#-------------------------------------------------------------------------------
package Baz::Foo;
BEGIN {
    require TestYAMLBase;
    @Bar::Foo::ISA = 'TestYAMLBase';
}
use YAML::Marshall;

sub yaml_dump {
    my $self = shift;
    my $node = $self->SUPER::yaml_dump(@_);
    $node->{comment} = "Hi, Mom";
    return $node;
}

sub yaml_load {
    my $class = shift;
    my $node = $class->SUPER::yaml_load(@_);
    delete $node->{comment};
    return $node;
}

#-------------------------------------------------------------------------------
package main;
no_diff;
run_roundtrip_nyn;

is $main::BazFoo->{11}, 12,
   'first key exists';

is $main::BazFoo->{13}, 14,
   'second key exists';

ok not($main::BazFoo->{comment}),
   'extra key not added';

__DATA__

=== Serialize a hash object as a sequence
+++ perl
my $fb = Foo::Bar->new;
$fb->{x} = 5;
$fb->{y} = 'che';
[$fb];
+++ yaml
---
- !perl/Foo::Bar
  - x
  - 5
  - y
  - che


=== Use a non-standard tag
+++ perl: bless {11 .. 14}, 'Bar::Baz';
+++ yaml
--- !random/object:bar.baz
11: 12
13: 14


=== super calls to mixins work
+++ perl: bless {11 .. 14}, 'Baz::Foo';
+++ yaml
--- !perl/Baz::Foo
11: 12
13: 14
comment: 'Hi, Mom'


=== yaml_dump doesn't mutate original hash
+++ no_round_trip
+++ perl: $main::BazFoo = bless {11 .. 14}, 'Baz::Foo';
+++ yaml
--- !perl/Baz::Foo
11: 12
13: 14
comment: 'Hi, Mom'