remove useless shebangs in tests
[gitmo/MooseX-Storage.git] / t / 011_basic_json_w_utf8.t
CommitLineData
9740e0b7 1use strict;
2use warnings;
3
4use Test::More;
5
0b173188 6use Test::Requires {
7 'Encode' => 0.01, # skip all if not installed
8 'JSON::Any' => 0.01,
9};
10
9740e0b7 11BEGIN {
b5f363ac 12 # NOTE:
13 # this is because JSON::XS is
9740e0b7 14 # the only one which really gets
15 # utf8 correct
b5f363ac 16 # - SL
0b173188 17 BEGIN {
9740e0b7 18 $ENV{JSON_ANY_ORDER} = qw(XS);
cd4cef76 19 $ENV{JSON_ANY_CONFIG} = "utf8=0,canonical=1";
0b173188 20 }
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}