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