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