Commit | Line | Data |
6f0912d0 |
1 | #!/usr/bin/perl |
ea189007 |
2 | $|++; |
6f0912d0 |
3 | use strict; |
4 | use warnings; |
5 | |
8d8356bb |
6 | use Test::More; |
619ab942 |
7 | use Test::Deep; |
6f0912d0 |
8 | |
0b173188 |
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 | |
6f0912d0 |
15 | BEGIN { |
f16b5740 |
16 | Test::Without::Module->import(YAML::Any->order); |
17 | Test::Without::Module->unimport('YAML'); |
18 | plan tests => 10; |
6f0912d0 |
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' ); |
7aac8ce9 |
48 | |
6f0912d0 |
49 | my $yaml = $foo->freeze; |
7aac8ce9 |
50 | |
f16b5740 |
51 | my $bar = Foo->thaw( $yaml ); |
52 | isa_ok( $bar, 'Foo' ); |
7aac8ce9 |
53 | |
f16b5740 |
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' ); |
619ab942 |
57 | cmp_deeply( $bar->array, [ 1 .. 10 ], '... got the right array' ); |
58 | cmp_deeply( |
f16b5740 |
59 | $bar->hash, |
6f0912d0 |
60 | { map { $_ => undef } ( 1 .. 10 ) }, |
61 | '... got the right hash' |
62 | ); |
63 | |
f16b5740 |
64 | isa_ok( $bar->object, 'Foo' ); |
65 | is( $bar->object->number, 2, |
6f0912d0 |
66 | '... got the right number (in the embedded object)' ); |
67 | } |
68 | |