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