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