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