0.01 releaase
[gitmo/MooseX-Storage.git] / t / 020_basic_yaml.t
CommitLineData
6f0912d0 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8d8356bb 6use Test::More;
6f0912d0 7
8BEGIN {
8d8356bb 9 eval "use Test::YAML::Valid";
10 plan skip_all => "Test::YAML::Valid is required for this test" if $@;
11 plan tests => 12;
6f0912d0 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{---
ba5bba75 49__CLASS__: Foo
6f0912d0 50array:
51 - 1
52 - 2
53 - 3
54 - 4
55 - 5
56 - 6
57 - 7
58 - 8
59 - 9
60 - 10
61float: 10.5
62hash:
63 1: ~
64 10: ~
65 2: ~
66 3: ~
67 4: ~
68 5: ~
69 6: ~
70 7: ~
71 8: ~
72 9: ~
73number: 10
74object:
ba5bba75 75 __CLASS__: Foo
6f0912d0 76 number: 2
77string: foo
78},
79 '... got the same YAML');
80
81}
82
83{
84 my $foo = Foo->thaw(q{---
ba5bba75 85__CLASS__: Foo
6f0912d0 86array:
87 - 1
88 - 2
89 - 3
90 - 4
91 - 5
92 - 6
93 - 7
94 - 8
95 - 9
96 - 10
97float: 10.5
98hash:
99 1: ~
100 10: ~
101 2: ~
102 3: ~
103 4: ~
104 5: ~
105 6: ~
106 7: ~
107 8: ~
108 9: ~
109number: 10
110object:
ba5bba75 111 __CLASS__: Foo
6f0912d0 112 number: 2
113string: foo
114});
115 isa_ok( $foo, 'Foo' );
116
117 is( $foo->number, 10, '... got the right number' );
118 is( $foo->string, 'foo', '... got the right string' );
119 is( $foo->float, 10.5, '... got the right float' );
120 is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
121 is_deeply(
122 $foo->hash,
123 { map { $_ => undef } ( 1 .. 10 ) },
124 '... got the right hash'
125 );
126
127 isa_ok( $foo->object, 'Foo' );
128 is( $foo->object->number, 2,
129 '... got the right number (in the embedded object)' );
130}
131