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