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