Commit | Line | Data |
e59193fb |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
d7ef03f6 |
6 | use Test::More tests => 14; |
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' ); |
d7ef03f6 |
27 | has 'union' => ( is => 'ro', isa => 'ArrayRef|Str' ); |
28 | has 'union2' => ( is => 'ro', isa => 'ArrayRef|Str' ); |
e59193fb |
29 | } |
30 | |
ec9c1923 |
31 | { |
a23e18d7 |
32 | my $foo = Foo->new( |
d691721b |
33 | number => 10, |
34 | string => 'foo', |
35 | boolean => 1, |
36 | float => 10.5, |
37 | array => [ 1 .. 10 ], |
38 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
39 | object => Foo->new( number => 2 ), |
d7ef03f6 |
40 | union => [ 1, 2, 3 ], |
41 | union2 => 'A String' |
b5384d08 |
42 | ); |
43 | isa_ok( $foo, 'Foo' ); |
d7ef03f6 |
44 | |
ec9c1923 |
45 | is_deeply( |
46 | $foo->pack, |
47 | { |
ba5bba75 |
48 | __CLASS__ => 'Foo', |
ec9c1923 |
49 | number => 10, |
50 | string => 'foo', |
d691721b |
51 | boolean => 1, |
ec9c1923 |
52 | float => 10.5, |
53 | array => [ 1 .. 10 ], |
54 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
d7ef03f6 |
55 | object => { |
56 | __CLASS__ => 'Foo', |
57 | number => 2 |
58 | }, |
59 | union => [ 1, 2, 3 ], |
60 | union2 => 'A String' |
ec9c1923 |
61 | }, |
62 | '... got the right frozen class' |
a23e18d7 |
63 | ); |
a23e18d7 |
64 | } |
65 | |
66 | { |
ec9c1923 |
67 | my $foo = Foo->unpack( |
68 | { |
ba5bba75 |
69 | __CLASS__ => 'Foo', |
ec9c1923 |
70 | number => 10, |
71 | string => 'foo', |
d691721b |
72 | boolean => 1, |
ec9c1923 |
73 | float => 10.5, |
74 | array => [ 1 .. 10 ], |
75 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
d7ef03f6 |
76 | object => { |
77 | __CLASS__ => 'Foo', |
78 | number => 2 |
79 | }, |
80 | union => [ 1, 2, 3 ], |
81 | union2 => 'A String' |
82 | } |
b5384d08 |
83 | ); |
84 | isa_ok( $foo, 'Foo' ); |
a23e18d7 |
85 | |
b5384d08 |
86 | is( $foo->number, 10, '... got the right number' ); |
87 | is( $foo->string, 'foo', '... got the right string' ); |
d691721b |
88 | ok( $foo->boolean, '... got the right boolean' ); |
b5384d08 |
89 | is( $foo->float, 10.5, '... got the right float' ); |
90 | is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' ); |
91 | is_deeply( |
92 | $foo->hash, |
93 | { map { $_ => undef } ( 1 .. 10 ) }, |
94 | '... got the right hash' |
95 | ); |
a23e18d7 |
96 | |
b5384d08 |
97 | isa_ok( $foo->object, 'Foo' ); |
98 | is( $foo->object->number, 2, |
99 | '... got the right number (in the embedded object)' ); |
d7ef03f6 |
100 | is_deeply( $foo->union, [ 1 .. 3 ], '... got the right array (in the union)' ); |
101 | is( $foo->union2, 'A String', '... got the right string (in the union)' ); |
a23e18d7 |
102 | } |