ae2609257e44fb0e2d559d420d07dfe811f0de72
[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 {
85     my $foo = Foo->thaw(
86         q{--- 
87 __CLASS__: Foo
88 array: 
89   - 1
90   - 2
91   - 3
92   - 4
93   - 5
94   - 6
95   - 7
96   - 8
97   - 9
98   - 10
99 float: 10.5
100 hash: 
101   1: ~
102   10: ~
103   2: ~
104   3: ~
105   4: ~
106   5: ~
107   6: ~
108   7: ~
109   8: ~
110   9: ~
111 number: 10
112 object: 
113   __CLASS__: Foo
114   number: 2
115 string: foo
116 }
117     );
118     isa_ok( $foo, 'Foo' );
119
120     is( $foo->number, 10,    '... got the right number' );
121     is( $foo->string, 'foo', '... got the right string' );
122     is( $foo->float,  10.5,  '... got the right float' );
123     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
124     is_deeply(
125         $foo->hash,
126         { map { $_ => undef } ( 1 .. 10 ) },
127         '... got the right hash'
128     );
129
130     isa_ok( $foo->object, 'Foo' );
131     is( $foo->object->number, 2,
132         '... got the right number (in the embedded object)' );
133 }
134