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