Make sure authors have all the deps
[gitmo/MooseX-Storage.git] / t / 012_param_json.t
CommitLineData
9ff679e4 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
0b173188 8use Test::Requires {
9 'MooseX::Storage::Format::JSONpm' => 0.01, # skip all if not installed
10};
9ff679e4 11
0b173188 12BEGIN {
13 plan tests => 6;
14 use_ok('MooseX::Storage');
15}
9ff679e4 16
17{
9ff679e4 18 package Foo;
19 use Moose;
20 use MooseX::Storage;
21
22 with Storage(format => [ JSONpm => { json_opts => { pretty => 1 } } ] );
9ff679e4 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
54d6ff36 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
58for 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}