bump to latest version of bundle
[gitmo/MooseX-Storage.git] / t / 012_param_json.t
CommitLineData
9ff679e4 1use strict;
2use warnings;
3
4use Test::More;
619ab942 5use Test::Deep;
9ff679e4 6
0b173188 7use Test::Requires {
8 'MooseX::Storage::Format::JSONpm' => 0.01, # skip all if not installed
9};
9ff679e4 10
0b173188 11BEGIN {
12 plan tests => 6;
13 use_ok('MooseX::Storage');
14}
9ff679e4 15
16{
9ff679e4 17 package Foo;
18 use Moose;
19 use MooseX::Storage;
20
21 with Storage(format => [ JSONpm => { json_opts => { pretty => 1 } } ] );
9ff679e4 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
54d6ff36 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
57for 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
619ab942 66 cmp_deeply(
54d6ff36 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}