Removed dependency on Best.
[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 YAML";
10     plan skip_all => "YAML is required for this test" if $@;
11     eval "use Test::YAML::Valid";
12     plan skip_all => "Test::YAML::Valid is required for this test" if $@;            
13     plan tests => 11;
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 }
49
50 {
51     my $foo = Foo->thaw(
52         q{--- 
53 __CLASS__: Foo
54 array: 
55   - 1
56   - 2
57   - 3
58   - 4
59   - 5
60   - 6
61   - 7
62   - 8
63   - 9
64   - 10
65 float: 10.5
66 hash: 
67   1: ~
68   10: ~
69   2: ~
70   3: ~
71   4: ~
72   5: ~
73   6: ~
74   7: ~
75   8: ~
76   9: ~
77 number: 10
78 object: 
79   __CLASS__: Foo
80   number: 2
81 string: foo
82 }
83     );
84     isa_ok( $foo, 'Foo' );
85
86     is( $foo->number, 10,    '... got the right number' );
87     is( $foo->string, 'foo', '... got the right string' );
88     is( $foo->float,  10.5,  '... got the right float' );
89     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
90     is_deeply(
91         $foo->hash,
92         { map { $_ => undef } ( 1 .. 10 ) },
93         '... got the right hash'
94     );
95
96     isa_ok( $foo->object, 'Foo' );
97     is( $foo->object->number, 2,
98         '... got the right number (in the embedded object)' );
99 }
100