eval require instead of use
[gitmo/MooseX-Storage.git] / t / 020_basic_yaml.t
1 #!/usr/bin/perl
2 $|++;
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9     eval "use YAML::Any";
10     plan skip_all => "YAML::Any is required for this test" if $@;
11     eval "require YAML";
12     plan skip_all => "YAML is required for this test" if $@;
13     eval "require Test::Without::Module";
14     plan skip_all => "Test::Without::Module is required for this test" if $@;
15     Test::Without::Module->import(YAML::Any->order);
16     Test::Without::Module->unimport('YAML');
17     plan tests => 10;
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' );
47
48     my $yaml = $foo->freeze;
49
50     my $bar = Foo->thaw( $yaml );
51     isa_ok( $bar, 'Foo' );
52
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' );
56     is_deeply( $bar->array, [ 1 .. 10 ], '... got the right array' );
57     is_deeply(
58         $bar->hash,
59         { map { $_ => undef } ( 1 .. 10 ) },
60         '... got the right hash'
61     );
62
63     isa_ok( $bar->object, 'Foo' );
64     is( $bar->object->number, 2,
65         '... got the right number (in the embedded object)' );
66 }
67