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