Commit | Line | Data |
9ff679e4 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
7 | |
b5f363ac |
8 | BEGIN { |
9ff679e4 |
9 | local $@; |
10 | plan skip_all => "MooseX::Storage::Format::JSONpm required for this test" |
11 | unless eval "require MooseX::Storage::Format::JSONpm; 1"; |
12 | } |
13 | |
54d6ff36 |
14 | plan tests => 6; |
9ff679e4 |
15 | use_ok('MooseX::Storage'); |
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 | |
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 | } |