reworking tests
[gitmo/MooseX-Storage.git] / t / 020_basic_yaml.t
CommitLineData
6f0912d0 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 1;
7use Test::YAML::Valid;
8
9BEGIN {
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{---
ba5bba75 47__CLASS__: Foo
6f0912d0 48array:
49 - 1
50 - 2
51 - 3
52 - 4
53 - 5
54 - 6
55 - 7
56 - 8
57 - 9
58 - 10
59float: 10.5
60hash:
61 1: ~
62 10: ~
63 2: ~
64 3: ~
65 4: ~
66 5: ~
67 6: ~
68 7: ~
69 8: ~
70 9: ~
71number: 10
72object:
ba5bba75 73 __CLASS__: Foo
6f0912d0 74 number: 2
75string: foo
76},
77 '... got the same YAML');
78
79}
80
81{
82 my $foo = Foo->thaw(q{---
ba5bba75 83__CLASS__: Foo
6f0912d0 84array:
85 - 1
86 - 2
87 - 3
88 - 4
89 - 5
90 - 6
91 - 7
92 - 8
93 - 9
94 - 10
95float: 10.5
96hash:
97 1: ~
98 10: ~
99 2: ~
100 3: ~
101 4: ~
102 5: ~
103 6: ~
104 7: ~
105 8: ~
106 9: ~
107number: 10
108object:
ba5bba75 109 __CLASS__: Foo
6f0912d0 110 number: 2
111string: 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