346325e398f9b5a156b67e7dcaf78cfc47f66bc8
[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 BEGIN {
9     eval "use Encode";
10     plan skip_all => "Encode is required for this test" if $@;   
11     eval "use JSON::Any";
12     plan skip_all => "JSON::Any is required for this test" if $@;     
13     # NOTE: 
14     # this is because JSON::XS is 
15     # the only one which really gets
16     # utf8 correct
17     # - SL 
18     BEGIN { 
19         $ENV{JSON_ANY_ORDER}  = qw(XS);
20         $ENV{JSON_ANY_CONFIG} = "utf8=1";        
21     }           
22     plan tests => 16;
23     use_ok('MooseX::Storage');
24 }
25
26 {
27     package Foo;
28     use Moose;
29     use MooseX::Storage;
30
31     with Storage( 'format' => 'JSON' );
32     
33     has 'utf8_string' => (
34         is      => 'rw',
35         isa     => 'Str',
36         default => sub { "ネットスーパー (Internet Shopping)" }
37     );
38 }
39
40 {
41     my $foo = Foo->new;
42     isa_ok( $foo, 'Foo' );
43
44     my $json = $foo->freeze;
45
46     is($json,
47        '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
48        '... got the right JSON');
49
50     my $foo2 = Foo->thaw($json);
51     isa_ok( $foo, 'Foo' );
52     
53     is($foo2->utf8_string, 
54       "ネットスーパー (Internet Shopping)", 
55       '... got the string we expected');
56       
57     is($foo2->freeze,
58        '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
59        '... got the right JSON');          
60 }
61
62 {
63     my $test_string;
64     {
65         use utf8;
66         $test_string = "ネットスーパー (Internet Shopping)";
67         no utf8;
68     }
69     
70     ok(utf8::is_utf8($test_string), '... got a utf8 string');
71     ok(utf8::valid($test_string), '... got a valid utf8 string');    
72     
73     Encode::_utf8_off($test_string);
74     
75     ok(!utf8::is_utf8($test_string), '... no longer is utf8 string');
76     ok(utf8::valid($test_string), '... got a valid utf8 string');    
77     
78     my $foo = Foo->new(
79         utf8_string => $test_string
80     );
81     isa_ok( $foo, 'Foo' );
82
83     ok(!utf8::is_utf8($foo->utf8_string), '... not a utf8 string');
84     ok(utf8::valid($foo->utf8_string), '... but is a valid utf8 string');
85
86     my $json = $foo->freeze;
87     
88     ok(utf8::is_utf8($json), '... is a utf8 string now');
89     ok(utf8::valid($json), '... got a valid utf8 string');    
90
91     is($json,
92        '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
93        '... got the right JSON');    
94 }