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