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