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