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