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