From: Stevan Little Date: Sun, 21 Oct 2007 02:59:22 +0000 (+0000) Subject: adding in a utf8 test X-Git-Tag: 0_09~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9740e0b7a5f3ba2c10c47abd9d3d66a6ecb8731b;p=gitmo%2FMooseX-Storage.git adding in a utf8 test --- diff --git a/Changes b/Changes index 20fe33a..ef1400c 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,9 @@ Revision history for MooseX-Storage 0.09 * MooseX::Storage::Util - added support to deal with utf8 strings correctly + + * t/ + - added a test for the utf8 handling 0.08 Wed. Oct. 10, 2007 * MooseX::Storage::Format::JSON diff --git a/MANIFEST b/MANIFEST index e1f3b91..49a2a73 100644 --- a/MANIFEST +++ b/MANIFEST @@ -29,6 +29,7 @@ t/005_w_versions_and_authority_check.t t/006_w_custom_type_handlers.t t/007_false.t t/010_basic_json.t +t/011_basic_json_w_utf8.t t/020_basic_yaml.t t/030_with_checksum.t t/040_basic_utils.t diff --git a/t/011_basic_json_w_utf8.t b/t/011_basic_json_w_utf8.t new file mode 100644 index 0000000..bcec79b --- /dev/null +++ b/t/011_basic_json_w_utf8.t @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +BEGIN { + eval "use Encode"; + plan skip_all => "Encode is required for this test" if $@; + # NOTE: + # this is because JSON::XS is + # the only one which really gets + # utf8 correct + # - SL + BEGIN { + $ENV{JSON_ANY_ORDER} = qw(XS); + $ENV{JSON_ANY_CONFIG} = "utf8=1"; + } + plan tests => 16; + use_ok('MooseX::Storage'); +} + +{ + package Foo; + use Moose; + use MooseX::Storage; + + with Storage( 'format' => 'JSON' ); + + has 'utf8_string' => ( + is => 'rw', + isa => 'Str', + default => sub { "ネットスーパー (Internet Shopping)" } + ); +} + +{ + my $foo = Foo->new; + isa_ok( $foo, 'Foo' ); + + my $json = $foo->freeze; + + is($json, + '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}', + '... got the right JSON'); + + my $foo2 = Foo->thaw($json); + isa_ok( $foo, 'Foo' ); + + is($foo2->utf8_string, + "ネットスーパー (Internet Shopping)", + '... got the string we expected'); + + is($foo2->freeze, + '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}', + '... got the right JSON'); +} + +{ + my $test_string; + { + use utf8; + $test_string = "ネットスーパー (Internet Shopping)"; + no utf8; + } + + ok(utf8::is_utf8($test_string), '... got a utf8 string'); + ok(utf8::valid($test_string), '... got a valid utf8 string'); + + Encode::_utf8_off($test_string); + + ok(!utf8::is_utf8($test_string), '... no longer is utf8 string'); + ok(utf8::valid($test_string), '... got a valid utf8 string'); + + my $foo = Foo->new( + utf8_string => $test_string + ); + isa_ok( $foo, 'Foo' ); + + ok(!utf8::is_utf8($foo->utf8_string), '... not a utf8 string'); + ok(utf8::valid($foo->utf8_string), '... but is a valid utf8 string'); + + my $json = $foo->freeze; + + ok(utf8::is_utf8($json), '... is a utf8 string now'); + ok(utf8::valid($json), '... got a valid utf8 string'); + + is($json, + '{"__CLASS__":"Foo","utf8_string":"ネットスーパー (Internet Shopping)"}', + '... got the right JSON'); +}