a795783b20da2b4a5e799a9cb06e79a0355d7c0e
[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 use Test::Deep;
8
9 use Test::Requires {
10     'YAML::Any' => 0.01, # skip all if not installed
11     'YAML'      => 0.01,
12     'Test::Without::Module' => 0.01,
13 };
14
15 BEGIN {
16     Test::Without::Module->import(YAML::Any->order);
17     Test::Without::Module->unimport('YAML');
18     plan tests => 10;
19     use_ok('MooseX::Storage');
20 }
21
22 {
23
24     package Foo;
25     use Moose;
26     use MooseX::Storage;
27
28     with Storage( 'format' => 'YAML' );
29
30     has 'number' => ( is => 'ro', isa => 'Int' );
31     has 'string' => ( is => 'ro', isa => 'Str' );
32     has 'float'  => ( is => 'ro', isa => 'Num' );
33     has 'array'  => ( is => 'ro', isa => 'ArrayRef' );
34     has 'hash'   => ( is => 'ro', isa => 'HashRef' );
35     has 'object' => ( is => 'ro', isa => 'Object' );
36 }
37
38 {
39     my $foo = Foo->new(
40         number => 10,
41         string => 'foo',
42         float  => 10.5,
43         array  => [ 1 .. 10 ],
44         hash   => { map { $_ => undef } ( 1 .. 10 ) },
45         object => Foo->new( number => 2 ),
46     );
47     isa_ok( $foo, 'Foo' );
48
49     my $yaml = $foo->freeze;
50
51     my $bar = Foo->thaw( $yaml );
52     isa_ok( $bar, 'Foo' );
53
54     is( $bar->number, 10,    '... got the right number' );
55     is( $bar->string, 'foo', '... got the right string' );
56     is( $bar->float,  10.5,  '... got the right float' );
57     cmp_deeply( $bar->array, [ 1 .. 10 ], '... got the right array' );
58     cmp_deeply(
59         $bar->hash,
60         { map { $_ => undef } ( 1 .. 10 ) },
61         '... got the right hash'
62     );
63
64     isa_ok( $bar->object, 'Foo' );
65     is( $bar->object->number, 2,
66         '... got the right number (in the embedded object)' );
67 }
68