Whitespace trim tests
[gitmo/MooseX-Storage.git] / t / 011_basic_json_w_utf8.t
CommitLineData
9740e0b7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8BEGIN {
9 eval "use Encode";
766ab81f 10 plan skip_all => "Encode is required for this test" if $@;
4fa64e86 11 eval "use JSON::Any";
766ab81f 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
766ab81f 17 # - SL
18 BEGIN {
9740e0b7 19 $ENV{JSON_ANY_ORDER} = qw(XS);
766ab81f 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' );
766ab81f 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' );
766ab81f 52
53 is($foo2->utf8_string,
54 "ネットスーパー (Internet Shopping)",
9740e0b7 55 '... got the string we expected');
766ab81f 56
9740e0b7 57 is($foo2->freeze,
58 '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
766ab81f 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 }
766ab81f 69
9740e0b7 70 ok(utf8::is_utf8($test_string), '... got a utf8 string');
766ab81f 71 ok(utf8::valid($test_string), '... got a valid utf8 string');
72
9740e0b7 73 Encode::_utf8_off($test_string);
766ab81f 74
9740e0b7 75 ok(!utf8::is_utf8($test_string), '... no longer is utf8 string');
766ab81f 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;
766ab81f 87
9740e0b7 88 ok(utf8::is_utf8($json), '... is a utf8 string now');
766ab81f 89 ok(utf8::valid($json), '... got a valid utf8 string');
9740e0b7 90
91 is($json,
92 '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}',
766ab81f 93 '... got the right JSON');
9740e0b7 94}