set JSON configs first before loading, so Test::Requires can skip cleanly
[gitmo/MooseX-Storage.git] / t / 011_basic_json_w_utf8.t
CommitLineData
9740e0b7 1use strict;
2use warnings;
3
4use Test::More;
5
30d5da5c 6# NOTE:
7# this is because JSON::XS (and Cpanel::JSON::XS) is
8# the only one which really gets utf8 correct
9# - SL
10BEGIN {
11 $ENV{JSON_ANY_ORDER} = 'XS CPANEL';
12 $ENV{JSON_ANY_CONFIG} = "utf8=0,canonical=1";
13}
14
0b173188 15use Test::Requires {
16 'Encode' => 0.01, # skip all if not installed
17 'JSON::Any' => 0.01,
18};
19
30d5da5c 20plan tests => 16;
21use_ok('MooseX::Storage');
9740e0b7 22
23{
24 package Foo;
25 use Moose;
26 use MooseX::Storage;
27
28 with Storage( 'format' => 'JSON' );
c2dae5d8 29
9740e0b7 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' );
c2dae5d8 49
50 is($foo2->utf8_string,
51 "ネットスーパー (Internet Shopping)",
9740e0b7 52 '... got the string we expected');
c2dae5d8 53
9740e0b7 54 is($foo2->freeze,
55 '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
c2dae5d8 56 '... got the right JSON');
9740e0b7 57}
58
59{
60 my $test_string;
61 {
62 use utf8;
63 $test_string = "ネットスーパー (Internet Shopping)";
64 no utf8;
65 }
c2dae5d8 66
9740e0b7 67 ok(utf8::is_utf8($test_string), '... got a utf8 string');
c2dae5d8 68 ok(utf8::valid($test_string), '... got a valid utf8 string');
69
9740e0b7 70 Encode::_utf8_off($test_string);
c2dae5d8 71
9740e0b7 72 ok(!utf8::is_utf8($test_string), '... no longer is utf8 string');
c2dae5d8 73 ok(utf8::valid($test_string), '... got a valid utf8 string');
74
9740e0b7 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;
c2dae5d8 84
9740e0b7 85 ok(utf8::is_utf8($json), '... is a utf8 string now');
c2dae5d8 86 ok(utf8::valid($json), '... got a valid utf8 string');
9740e0b7 87
88 is($json,
89 '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
c2dae5d8 90 '... got the right JSON');
9740e0b7 91}