make this test function properly when JSON backends aren't installed
[gitmo/MooseX-Storage.git] / t / 020_basic_yaml.t
CommitLineData
ea189007 1$|++;
6f0912d0 2use strict;
3use warnings;
4
8d8356bb 5use Test::More;
619ab942 6use Test::Deep;
6f0912d0 7
0b173188 8use Test::Requires {
9 'YAML::Any' => 0.01, # skip all if not installed
10 'YAML' => 0.01,
11 'Test::Without::Module' => 0.01,
12};
13
6f0912d0 14BEGIN {
f16b5740 15 Test::Without::Module->import(YAML::Any->order);
16 Test::Without::Module->unimport('YAML');
17 plan tests => 10;
6f0912d0 18 use_ok('MooseX::Storage');
19}
20
21{
22
23 package Foo;
24 use Moose;
25 use MooseX::Storage;
26
27 with Storage( 'format' => 'YAML' );
28
29 has 'number' => ( is => 'ro', isa => 'Int' );
30 has 'string' => ( is => 'ro', isa => 'Str' );
31 has 'float' => ( is => 'ro', isa => 'Num' );
32 has 'array' => ( is => 'ro', isa => 'ArrayRef' );
33 has 'hash' => ( is => 'ro', isa => 'HashRef' );
34 has 'object' => ( is => 'ro', isa => 'Object' );
35}
36
37{
38 my $foo = Foo->new(
39 number => 10,
40 string => 'foo',
41 float => 10.5,
42 array => [ 1 .. 10 ],
43 hash => { map { $_ => undef } ( 1 .. 10 ) },
44 object => Foo->new( number => 2 ),
45 );
46 isa_ok( $foo, 'Foo' );
7aac8ce9 47
6f0912d0 48 my $yaml = $foo->freeze;
7aac8ce9 49
f16b5740 50 my $bar = Foo->thaw( $yaml );
51 isa_ok( $bar, 'Foo' );
7aac8ce9 52
f16b5740 53 is( $bar->number, 10, '... got the right number' );
54 is( $bar->string, 'foo', '... got the right string' );
55 is( $bar->float, 10.5, '... got the right float' );
619ab942 56 cmp_deeply( $bar->array, [ 1 .. 10 ], '... got the right array' );
57 cmp_deeply(
f16b5740 58 $bar->hash,
6f0912d0 59 { map { $_ => undef } ( 1 .. 10 ) },
60 '... got the right hash'
61 );
62
f16b5740 63 isa_ok( $bar->object, 'Foo' );
64 is( $bar->object->number, 2,
6f0912d0 65 '... got the right number (in the embedded object)' );
66}
67