WithCheksum 2.0
[gitmo/MooseX-Storage.git] / t / 020_basic_yaml.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9     eval "use Test::YAML::Valid";
10     plan skip_all => "Test::YAML::Valid is required for this test" if $@;        
11     plan tests => 12;
12     use_ok('MooseX::Storage');
13 }
14
15 {
16
17     package Foo;
18     use Moose;
19     use MooseX::Storage;
20
21     with Storage( 'format' => 'YAML' );
22
23     has 'number' => ( is => 'ro', isa => 'Int' );
24     has 'string' => ( is => 'ro', isa => 'Str' );
25     has 'float'  => ( is => 'ro', isa => 'Num' );
26     has 'array'  => ( is => 'ro', isa => 'ArrayRef' );
27     has 'hash'   => ( is => 'ro', isa => 'HashRef' );
28     has 'object' => ( is => 'ro', isa => 'Object' );
29 }
30
31 {
32     my $foo = Foo->new(
33         number => 10,
34         string => 'foo',
35         float  => 10.5,
36         array  => [ 1 .. 10 ],
37         hash   => { map { $_ => undef } ( 1 .. 10 ) },
38         object => Foo->new( number => 2 ),
39     );
40     isa_ok( $foo, 'Foo' );
41     
42     my $yaml = $foo->freeze;
43     
44     yaml_string_ok($yaml, '... we got valid YAML out of it');
45     
46     is(
47         $yaml,
48         q{--- 
49 __CLASS__: Foo
50 array: 
51   - 1
52   - 2
53   - 3
54   - 4
55   - 5
56   - 6
57   - 7
58   - 8
59   - 9
60   - 10
61 float: 10.5
62 hash: 
63   1: ~
64   10: ~
65   2: ~
66   3: ~
67   4: ~
68   5: ~
69   6: ~
70   7: ~
71   8: ~
72   9: ~
73 number: 10
74 object: 
75   __CLASS__: Foo
76   number: 2
77 string: foo
78 },
79     '... got the same YAML');
80     
81 }
82
83 {
84     my $foo = Foo->thaw(q{--- 
85 __CLASS__: Foo
86 array: 
87   - 1
88   - 2
89   - 3
90   - 4
91   - 5
92   - 6
93   - 7
94   - 8
95   - 9
96   - 10
97 float: 10.5
98 hash: 
99   1: ~
100   10: ~
101   2: ~
102   3: ~
103   4: ~
104   5: ~
105   6: ~
106   7: ~
107   8: ~
108   9: ~
109 number: 10
110 object: 
111   __CLASS__: Foo
112   number: 2
113 string: foo
114 });
115     isa_ok( $foo, 'Foo' );
116
117     is( $foo->number, 10,    '... got the right number' );
118     is( $foo->string, 'foo', '... got the right string' );
119     is( $foo->float,  10.5,  '... got the right float' );
120     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
121     is_deeply(
122         $foo->hash,
123         { map { $_ => undef } ( 1 .. 10 ) },
124         '... got the right hash'
125     );
126
127     isa_ok( $foo->object, 'Foo' );
128     is( $foo->object->number, 2,
129         '... got the right number (in the embedded object)' );
130 }
131