move to github
[gitmo/MooseX-Storage.git] / t / 012_param_json.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Deep;
6
7 use Test::Requires {
8     'MooseX::Storage::Format::JSONpm' => 0.01, # skip all if not installed
9 };
10
11 BEGIN {
12     plan tests => 6;
13     use_ok('MooseX::Storage');
14 }
15
16 {
17     package Foo;
18     use Moose;
19     use MooseX::Storage;
20
21     with Storage(format => [ JSONpm => { json_opts => { pretty => 1 } } ] );
22
23     has 'string' => ( is => 'ro', isa => 'Str' );
24     has 'float'  => ( is => 'ro', isa => 'Num' );
25 }
26
27 {
28     my $foo = Foo->new(
29         string => 'foo',
30         float  => 10.5,
31     );
32     isa_ok( $foo, 'Foo' );
33
34     my $json = $foo->freeze;
35
36     isnt(
37         index($json, "\n"),
38         -1,
39         "there are newlines in our JSON, because it is pretty",
40     ) or diag $json;
41
42 }
43
44 {
45     package Bar;
46     use Moose;
47     use MooseX::Storage;
48
49     our $VERSION = '0.01';
50
51     with 'MooseX::Storage::Deferred';
52
53     has 'x' => (is => 'rw', isa => 'Int');
54     has 'y' => (is => 'rw', isa => 'Int');
55 }
56
57 for my $jsonpm (
58   [ string => 'JSONpm' ],
59   [ aref0p => [ JSONpm => ] ],
60   [ aref1p => [ JSONpm => { json_opts => { pretty => 1 } } ] ],
61 ) {
62     my ($name, $p) = @$jsonpm;
63
64     my $json = eval { Bar->new(x => 10, y => 20)->freeze({ format => $p }) };
65
66     cmp_deeply(
67         JSON->new->decode($json),
68         {
69             '__CLASS__' => 'Bar-0.01',
70             x => 10,
71             y => 20,
72         },
73         "correct deferred freeze from $name",
74     );
75 }