Make false ints and strings work, bools still no good
[gitmo/MooseX-Storage.git] / t / 010_basic_json.t
CommitLineData
ec9c1923 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8d8356bb 6use Test::More;
ec9c1923 7
8BEGIN {
8d8356bb 9 eval "use Test::JSON";
a7f358fb 10 plan skip_all => "Test::JSON is required for this test" if $@;
11 # NOTE:
12 # this idiocy is cause Test::JSON
13 # uses JSON.pm and that can be
14 # very picky about the JSON output
15 # - SL
16 BEGIN { $ENV{JSON_ANY_ORDER} = qw(JSON) }
8d8356bb 17 plan tests => 12;
ec9c1923 18 use_ok('MooseX::Storage');
19}
20
21{
22
23 package Foo;
24 use Moose;
25 use MooseX::Storage;
26
27 with Storage( 'format' => 'JSON' );
28
29 has 'number' => ( is => 'ro', isa => 'Int' );
30 has 'string' => ( is => 'ro', isa => 'Str' );
31 has 'float' => ( is => 'ro', isa => 'Num' );
32 has 'array' => ( is => 'ro', isa => 'ArrayRef' );
33 has 'hash' => ( is => 'ro', isa => 'HashRef' );
34 has 'object' => ( is => 'ro', isa => 'Object' );
35}
36
37{
38 my $foo = Foo->new(
39 number => 10,
40 string => 'foo',
41 float => 10.5,
42 array => [ 1 .. 10 ],
43 hash => { map { $_ => undef } ( 1 .. 10 ) },
44 object => Foo->new( number => 2 ),
45 );
46 isa_ok( $foo, 'Foo' );
7aac8ce9 47
ec9c1923 48 my $json = $foo->freeze;
7aac8ce9 49
8d8356bb 50 is_valid_json($json, '.. this is valid JSON');
7aac8ce9 51
ec9c1923 52 is_json(
53 $json,
7aac8ce9 54'{"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 55 '... got the right JSON'
56 );
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' );
69 is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
70 is_deeply(
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