Commit | Line | Data |
e59193fb |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
d691721b |
6 | use Test::More tests => 12; |
e59193fb |
7 | |
ec9c1923 |
8 | BEGIN { |
9 | use_ok('MooseX::Storage'); |
10 | } |
11 | |
e59193fb |
12 | { |
b5384d08 |
13 | |
e59193fb |
14 | package Foo; |
15 | use Moose; |
16 | use MooseX::Storage; |
b5384d08 |
17 | |
913d96dd |
18 | with Storage; |
b5384d08 |
19 | |
d691721b |
20 | has 'number' => ( is => 'ro', isa => 'Int' ); |
21 | has 'string' => ( is => 'ro', isa => 'Str' ); |
22 | has 'boolean' => ( is => 'ro', isa => 'Bool' ); |
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 => 'Foo' ); |
e59193fb |
27 | } |
28 | |
ec9c1923 |
29 | { |
a23e18d7 |
30 | my $foo = Foo->new( |
d691721b |
31 | number => 10, |
32 | string => 'foo', |
33 | boolean => 1, |
34 | float => 10.5, |
35 | array => [ 1 .. 10 ], |
36 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
37 | object => Foo->new( number => 2 ), |
b5384d08 |
38 | ); |
39 | isa_ok( $foo, 'Foo' ); |
b5f363ac |
40 | |
ec9c1923 |
41 | is_deeply( |
42 | $foo->pack, |
43 | { |
ba5bba75 |
44 | __CLASS__ => 'Foo', |
ec9c1923 |
45 | number => 10, |
46 | string => 'foo', |
d691721b |
47 | boolean => 1, |
ec9c1923 |
48 | float => 10.5, |
49 | array => [ 1 .. 10 ], |
50 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
b5f363ac |
51 | object => { |
52 | __CLASS__ => 'Foo', |
53 | number => 2 |
54 | }, |
ec9c1923 |
55 | }, |
56 | '... got the right frozen class' |
a23e18d7 |
57 | ); |
a23e18d7 |
58 | } |
59 | |
60 | { |
ec9c1923 |
61 | my $foo = Foo->unpack( |
62 | { |
ba5bba75 |
63 | __CLASS__ => 'Foo', |
ec9c1923 |
64 | number => 10, |
65 | string => 'foo', |
d691721b |
66 | boolean => 1, |
ec9c1923 |
67 | float => 10.5, |
68 | array => [ 1 .. 10 ], |
69 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
b5f363ac |
70 | object => { |
71 | __CLASS__ => 'Foo', |
72 | number => 2 |
73 | }, |
74 | } |
b5384d08 |
75 | ); |
76 | isa_ok( $foo, 'Foo' ); |
a23e18d7 |
77 | |
b5384d08 |
78 | is( $foo->number, 10, '... got the right number' ); |
79 | is( $foo->string, 'foo', '... got the right string' ); |
d691721b |
80 | ok( $foo->boolean, '... got the right boolean' ); |
b5384d08 |
81 | is( $foo->float, 10.5, '... got the right float' ); |
82 | is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' ); |
83 | is_deeply( |
84 | $foo->hash, |
85 | { map { $_ => undef } ( 1 .. 10 ) }, |
86 | '... got the right hash' |
87 | ); |
a23e18d7 |
88 | |
b5384d08 |
89 | isa_ok( $foo->object, 'Foo' ); |
90 | is( $foo->object->number, 2, |
91 | '... got the right number (in the embedded object)' ); |
a23e18d7 |
92 | } |