Commit | Line | Data |
9740e0b7 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
7 | |
8 | BEGIN { |
9 | eval "use Encode"; |
b5f363ac |
10 | plan skip_all => "Encode is required for this test" if $@; |
4fa64e86 |
11 | eval "use JSON::Any"; |
b5f363ac |
12 | plan skip_all => "JSON::Any is required for this test" if $@; |
13 | # NOTE: |
14 | # this is because JSON::XS is |
9740e0b7 |
15 | # the only one which really gets |
16 | # utf8 correct |
b5f363ac |
17 | # - SL |
18 | BEGIN { |
9740e0b7 |
19 | $ENV{JSON_ANY_ORDER} = qw(XS); |
b5f363ac |
20 | $ENV{JSON_ANY_CONFIG} = "utf8=1"; |
21 | } |
9740e0b7 |
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' ); |
b5f363ac |
32 | |
9740e0b7 |
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' ); |
b5f363ac |
52 | |
53 | is($foo2->utf8_string, |
54 | "ネットスーパー (Internet Shopping)", |
9740e0b7 |
55 | '... got the string we expected'); |
b5f363ac |
56 | |
9740e0b7 |
57 | is($foo2->freeze, |
58 | '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}', |
b5f363ac |
59 | '... got the right JSON'); |
9740e0b7 |
60 | } |
61 | |
62 | { |
63 | my $test_string; |
64 | { |
65 | use utf8; |
66 | $test_string = "ネットスーパー (Internet Shopping)"; |
67 | no utf8; |
68 | } |
b5f363ac |
69 | |
9740e0b7 |
70 | ok(utf8::is_utf8($test_string), '... got a utf8 string'); |
b5f363ac |
71 | ok(utf8::valid($test_string), '... got a valid utf8 string'); |
72 | |
9740e0b7 |
73 | Encode::_utf8_off($test_string); |
b5f363ac |
74 | |
9740e0b7 |
75 | ok(!utf8::is_utf8($test_string), '... no longer is utf8 string'); |
b5f363ac |
76 | ok(utf8::valid($test_string), '... got a valid utf8 string'); |
77 | |
9740e0b7 |
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; |
b5f363ac |
87 | |
9740e0b7 |
88 | ok(utf8::is_utf8($json), '... is a utf8 string now'); |
b5f363ac |
89 | ok(utf8::valid($json), '... got a valid utf8 string'); |
9740e0b7 |
90 | |
91 | is($json, |
92 | '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}', |
b5f363ac |
93 | '... got the right JSON'); |
9740e0b7 |
94 | } |