Commit | Line | Data |
4fa64e86 |
1 | use strict; |
2 | use warnings; |
3 | |
335eb377 |
4 | use Test::More tests => 13; |
619ab942 |
5 | use Test::Deep; |
9d3c60f5 |
6 | use Test::Fatal; |
4fa64e86 |
7 | |
8 | BEGIN { |
9 | use_ok('MooseX::Storage'); |
10 | } |
11 | |
12 | { |
13 | package Foo; |
14 | use Moose; |
15 | use MooseX::Storage; |
16 | |
17 | with Storage; |
18 | |
19 | has 'bar' => ( |
20 | metaclass => 'DoNotSerialize', |
21 | is => 'rw', |
c2dae5d8 |
22 | default => sub { 'BAR' } |
4fa64e86 |
23 | ); |
c2dae5d8 |
24 | |
4fa64e86 |
25 | has 'baz' => ( |
26 | traits => [ 'DoNotSerialize' ], |
27 | is => 'rw', |
c2dae5d8 |
28 | default => sub { 'BAZ' } |
29 | ); |
30 | |
4fa64e86 |
31 | has 'gorch' => ( |
c2dae5d8 |
32 | is => 'rw', |
4fa64e86 |
33 | default => sub { 'GORCH' } |
c2dae5d8 |
34 | ); |
4fa64e86 |
35 | |
36 | 1; |
37 | } |
38 | |
186d680b |
39 | { my $foo = Foo->new; |
40 | isa_ok($foo, 'Foo'); |
c2dae5d8 |
41 | |
186d680b |
42 | is($foo->bar, 'BAR', '... got the value we expected'); |
43 | is($foo->baz, 'BAZ', '... got the value we expected'); |
44 | is($foo->gorch, 'GORCH', '... got the value we expected'); |
c2dae5d8 |
45 | |
619ab942 |
46 | cmp_deeply( |
186d680b |
47 | $foo->pack, |
48 | { |
49 | __CLASS__ => 'Foo', |
50 | gorch => 'GORCH' |
51 | }, |
52 | '... got the right packed class data' |
53 | ); |
54 | } |
04990d7a |
55 | |
186d680b |
56 | ### more involved test; required attribute that's not serialized |
57 | { package Bar; |
58 | use Moose; |
59 | use MooseX::Storage; |
04990d7a |
60 | |
186d680b |
61 | with Storage; |
04990d7a |
62 | |
186d680b |
63 | has foo => ( |
64 | metaclass => 'DoNotSerialize', |
65 | required => 1, |
66 | is => 'rw', |
c21a034f |
67 | isa => 'Object', # type constraint is important |
186d680b |
68 | ); |
c2dae5d8 |
69 | |
186d680b |
70 | has zot => ( |
71 | default => sub { $$ }, |
72 | is => 'rw', |
c2dae5d8 |
73 | ); |
186d680b |
74 | } |
04990d7a |
75 | |
c21a034f |
76 | { my $obj = bless {}; |
77 | my $bar = Bar->new( foo => $obj ); |
c2dae5d8 |
78 | |
186d680b |
79 | ok( $bar, "New object created" ); |
c21a034f |
80 | is( $bar->foo, $obj, " ->foo => $obj" ); |
186d680b |
81 | is( $bar->zot, $$, " ->zot => $$" ); |
c2dae5d8 |
82 | |
186d680b |
83 | my $bpack = $bar->pack; |
619ab942 |
84 | cmp_deeply( |
186d680b |
85 | $bpack, |
86 | { __CLASS__ => 'Bar', |
87 | zot => $$, |
88 | }, " Packed correctly" ); |
c2dae5d8 |
89 | |
335eb377 |
90 | eval { Bar->unpack( $bpack ) }; |
91 | ok( $@, " Unpack without required attribute fails" ); |
92 | like( $@, qr/foo/, " Proper error recorded" ); |
c2dae5d8 |
93 | |
c21a034f |
94 | my $bar2 = Bar->unpack( $bpack, inject => { foo => bless {} } ); |
c2dae5d8 |
95 | ok( $bar2, " Unpacked correctly with foo => Object"); |
96 | } |
97 | |
98 | |
99 | |
100 | |
4fa64e86 |
101 | |