Commit | Line | Data |
ec9c1923 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More no_plan => 1; |
7 | use Test::JSON; |
8 | |
9 | BEGIN { |
10 | use_ok('MooseX::Storage'); |
11 | } |
12 | |
13 | { |
14 | |
15 | package Foo; |
16 | use Moose; |
17 | use MooseX::Storage; |
18 | |
19 | with Storage( 'format' => 'JSON' ); |
20 | |
21 | has 'number' => ( is => 'ro', isa => 'Int' ); |
22 | has 'string' => ( is => 'ro', isa => 'Str' ); |
23 | has 'float' => ( is => 'ro', isa => 'Num' ); |
24 | has 'array' => ( is => 'ro', isa => 'ArrayRef' ); |
25 | has 'hash' => ( is => 'ro', isa => 'HashRef' ); |
26 | has 'object' => ( is => 'ro', isa => 'Object' ); |
27 | } |
28 | |
29 | { |
30 | my $foo = Foo->new( |
31 | number => 10, |
32 | string => 'foo', |
33 | float => 10.5, |
34 | array => [ 1 .. 10 ], |
35 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
36 | object => Foo->new( number => 2 ), |
37 | ); |
38 | isa_ok( $foo, 'Foo' ); |
39 | |
40 | my $json = $foo->freeze; |
41 | |
42 | is_valid_json($json); |
43 | |
44 | is_json( |
45 | $json, |
ba5bba75 |
46 | '{"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 |
47 | '... got the right JSON' |
48 | ); |
49 | } |
50 | |
51 | { |
52 | my $foo = Foo->thaw( |
ba5bba75 |
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 | ); |
55 | isa_ok( $foo, 'Foo' ); |
56 | |
57 | is( $foo->number, 10, '... got the right number' ); |
58 | is( $foo->string, 'foo', '... got the right string' ); |
59 | is( $foo->float, 10.5, '... got the right float' ); |
60 | is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' ); |
61 | is_deeply( |
62 | $foo->hash, |
63 | { map { $_ => undef } ( 1 .. 10 ) }, |
64 | '... got the right hash' |
65 | ); |
66 | |
67 | isa_ok( $foo->object, 'Foo' ); |
68 | is( $foo->object->number, 2, |
69 | '... got the right number (in the embedded object)' ); |
70 | } |
71 | |