Create a JSON::Any object, so setting $ENV{JSON_ANY_CONFIG} works.
[gitmo/MooseX-Storage.git] / t / 011_basic_json_w_utf8.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Test::Requires {
9     'Encode' => 0.01, # skip all if not installed
10     'JSON::Any' => 0.01,
11 };
12
13 BEGIN {
14     # NOTE: 
15     # this is because JSON::XS is 
16     # the only one which really gets
17     # utf8 correct
18     # - SL 
19     BEGIN {
20         $ENV{JSON_ANY_ORDER}  = qw(XS);
21         $ENV{JSON_ANY_CONFIG} = "utf8=0";
22     }
23
24     plan tests => 16;
25     use_ok('MooseX::Storage');
26 }
27
28 {
29     package Foo;
30     use Moose;
31     use MooseX::Storage;
32
33     with Storage( 'format' => 'JSON' );
34     
35     has 'utf8_string' => (
36         is      => 'rw',
37         isa     => 'Str',
38         default => sub { "ネットスーパー (Internet Shopping)" }
39     );
40 }
41
42 {
43     my $foo = Foo->new;
44     isa_ok( $foo, 'Foo' );
45
46     my $json = $foo->freeze;
47
48     is($json,
49        '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
50        '... got the right JSON');
51
52     my $foo2 = Foo->thaw($json);
53     isa_ok( $foo, 'Foo' );
54     
55     is($foo2->utf8_string, 
56       "ネットスーパー (Internet Shopping)", 
57       '... got the string we expected');
58       
59     is($foo2->freeze,
60        '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
61        '... got the right JSON');          
62 }
63
64 {
65     my $test_string;
66     {
67         use utf8;
68         $test_string = "ネットスーパー (Internet Shopping)";
69         no utf8;
70     }
71     
72     ok(utf8::is_utf8($test_string), '... got a utf8 string');
73     ok(utf8::valid($test_string), '... got a valid utf8 string');    
74     
75     Encode::_utf8_off($test_string);
76     
77     ok(!utf8::is_utf8($test_string), '... no longer is utf8 string');
78     ok(utf8::valid($test_string), '... got a valid utf8 string');    
79     
80     my $foo = Foo->new(
81         utf8_string => $test_string
82     );
83     isa_ok( $foo, 'Foo' );
84
85     ok(!utf8::is_utf8($foo->utf8_string), '... not a utf8 string');
86     ok(utf8::valid($foo->utf8_string), '... but is a valid utf8 string');
87
88     my $json = $foo->freeze;
89     
90     ok(utf8::is_utf8($json), '... is a utf8 string now');
91     ok(utf8::valid($json), '... got a valid utf8 string');    
92
93     is($json,
94        '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
95        '... got the right JSON');    
96 }