Removed dependency on Best; YAML tests now require YAML (which will automatically...
[gitmo/MooseX-Storage.git] / t / 020_basic_yaml.t
CommitLineData
6f0912d0 1#!/usr/bin/perl
ea189007 2$|++;
6f0912d0 3use strict;
4use warnings;
5
8d8356bb 6use Test::More;
6f0912d0 7
8BEGIN {
b519434d 9 eval "use YAML";
10 plan skip_all => "YAML is required for this test" if $@;
8d8356bb 11 eval "use Test::YAML::Valid";
b5f363ac 12 plan skip_all => "Test::YAML::Valid is required for this test" if $@;
b519434d 13 plan tests => 11;
6f0912d0 14 use_ok('MooseX::Storage');
15}
16
17{
18
19 package Foo;
20 use Moose;
21 use MooseX::Storage;
22
23 with Storage( 'format' => 'YAML' );
24
25 has 'number' => ( is => 'ro', isa => 'Int' );
26 has 'string' => ( is => 'ro', isa => 'Str' );
27 has 'float' => ( is => 'ro', isa => 'Num' );
28 has 'array' => ( is => 'ro', isa => 'ArrayRef' );
29 has 'hash' => ( is => 'ro', isa => 'HashRef' );
30 has 'object' => ( is => 'ro', isa => 'Object' );
31}
32
33{
34 my $foo = Foo->new(
35 number => 10,
36 string => 'foo',
37 float => 10.5,
38 array => [ 1 .. 10 ],
39 hash => { map { $_ => undef } ( 1 .. 10 ) },
40 object => Foo->new( number => 2 ),
41 );
42 isa_ok( $foo, 'Foo' );
7aac8ce9 43
6f0912d0 44 my $yaml = $foo->freeze;
7aac8ce9 45
46 yaml_string_ok( $yaml, '... we got valid YAML out of it' );
47
6f0912d0 48}
49
50{
7aac8ce9 51 my $foo = Foo->thaw(
b5f363ac 52 q{---
ba5bba75 53__CLASS__: Foo
b5f363ac 54array:
6f0912d0 55 - 1
56 - 2
57 - 3
58 - 4
59 - 5
60 - 6
61 - 7
62 - 8
63 - 9
64 - 10
65float: 10.5
b5f363ac 66hash:
6f0912d0 67 1: ~
68 10: ~
69 2: ~
70 3: ~
71 4: ~
72 5: ~
73 6: ~
74 7: ~
75 8: ~
76 9: ~
77number: 10
b5f363ac 78object:
ba5bba75 79 __CLASS__: Foo
6f0912d0 80 number: 2
81string: foo
7aac8ce9 82}
83 );
6f0912d0 84 isa_ok( $foo, 'Foo' );
85
86 is( $foo->number, 10, '... got the right number' );
87 is( $foo->string, 'foo', '... got the right string' );
88 is( $foo->float, 10.5, '... got the right float' );
89 is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
90 is_deeply(
91 $foo->hash,
92 { map { $_ => undef } ( 1 .. 10 ) },
93 '... got the right hash'
94 );
95
96 isa_ok( $foo->object, 'Foo' );
97 is( $foo->object->number, 2,
98 '... got the right number (in the embedded object)' );
99}
100