Commit | Line | Data |
ec9c1923 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8d8356bb |
6 | use Test::More; |
619ab942 |
7 | use Test::Deep; |
ec9c1923 |
8 | |
0b173188 |
9 | use Test::Requires { |
10 | 'Test::JSON' => 0.01, # skip all if not installed |
11 | 'JSON::Any' => 0.01, |
12 | }; |
13 | |
14 | BEGIN { |
8d8356bb |
15 | plan tests => 12; |
ec9c1923 |
16 | use_ok('MooseX::Storage'); |
17 | } |
18 | |
19 | { |
20 | |
21 | package Foo; |
22 | use Moose; |
23 | use MooseX::Storage; |
24 | |
25 | with Storage( 'format' => 'JSON' ); |
26 | |
27 | has 'number' => ( is => 'ro', isa => 'Int' ); |
28 | has 'string' => ( is => 'ro', isa => 'Str' ); |
29 | has 'float' => ( is => 'ro', isa => 'Num' ); |
30 | has 'array' => ( is => 'ro', isa => 'ArrayRef' ); |
31 | has 'hash' => ( is => 'ro', isa => 'HashRef' ); |
32 | has 'object' => ( is => 'ro', isa => 'Object' ); |
33 | } |
34 | |
35 | { |
36 | my $foo = Foo->new( |
37 | number => 10, |
38 | string => 'foo', |
39 | float => 10.5, |
40 | array => [ 1 .. 10 ], |
41 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
42 | object => Foo->new( number => 2 ), |
43 | ); |
44 | isa_ok( $foo, 'Foo' ); |
7aac8ce9 |
45 | |
ec9c1923 |
46 | my $json = $foo->freeze; |
7aac8ce9 |
47 | |
8d8356bb |
48 | is_valid_json($json, '.. this is valid JSON'); |
7aac8ce9 |
49 | |
cfee09ad |
50 | |
ec9c1923 |
51 | is_json( |
52 | $json, |
7aac8ce9 |
53 | '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__CLASS__":"Foo"},"number":10,"__CLASS__":"Foo","string":"foo"}', |
ec9c1923 |
54 | '... got the right JSON' |
55 | ); |
cfee09ad |
56 | |
ec9c1923 |
57 | } |
58 | |
59 | { |
7aac8ce9 |
60 | my $foo = |
61 | Foo->thaw( |
62 | '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__CLASS__":"Foo"},"number":10,"__CLASS__":"Foo","string":"foo"}' |
63 | ); |
ec9c1923 |
64 | isa_ok( $foo, 'Foo' ); |
65 | |
66 | is( $foo->number, 10, '... got the right number' ); |
67 | is( $foo->string, 'foo', '... got the right string' ); |
68 | is( $foo->float, 10.5, '... got the right float' ); |
619ab942 |
69 | cmp_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' ); |
70 | cmp_deeply( |
ec9c1923 |
71 | $foo->hash, |
72 | { map { $_ => undef } ( 1 .. 10 ) }, |
73 | '... got the right hash' |
74 | ); |
75 | |
76 | isa_ok( $foo->object, 'Foo' ); |
77 | is( $foo->object->number, 2, |
78 | '... got the right number (in the embedded object)' ); |
79 | } |
80 | |