6da8d3eb55ce3e526d5b1f4501a32ab83d3900f1
[gitmo/MooseX-Storage.git] / t / 105_io_atomic_w_utf8.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {  
9     eval "use IO::AtomicFile";
10     plan skip_all => "IO::AtomicFile is required for this test" if $@;        
11     eval "use JSON::Any";
12     plan skip_all => "JSON::Any is required for this test" if $@;        
13     # NOTE: 
14     # this is because JSON::XS is 
15     # the only one which really gets
16     # utf8 correct
17     # - SL 
18     BEGIN { 
19         $ENV{JSON_ANY_ORDER}  = qw(XS);
20         $ENV{JSON_ANY_CONFIG} = "utf8=1";        
21     }           
22     plan tests => 8;
23     use_ok('MooseX::Storage');
24 }
25
26 use utf8;
27
28 {
29     package Foo;
30     use Moose;
31     use MooseX::Storage;
32
33     with Storage( 'format' => 'JSON', 'io' => 'AtomicFile' );
34     
35     has 'utf8_string' => (
36         is      => 'rw',
37         isa     => 'Str',
38         default => sub { "ネットスーパー (Internet Shopping)" }
39     );
40 }
41
42 my $file = 'temp.json';
43
44 {
45     my $foo = Foo->new;
46     isa_ok( $foo, 'Foo' );
47        
48     $foo->store($file);         
49 }
50
51 {
52     my $foo = Foo->load($file);
53     isa_ok($foo, 'Foo');
54
55     is($foo->utf8_string, 
56       "ネットスーパー (Internet Shopping)", 
57       '... got the string we expected');
58 }
59
60 no utf8;
61
62 unlink $file;
63
64 {
65     my $foo = Foo->new(
66         utf8_string => 'Escritório'
67     );
68     isa_ok( $foo, 'Foo' );
69        
70     $foo->store($file);         
71 }
72
73 {
74     my $foo = Foo->load($file);
75     isa_ok($foo, 'Foo');
76     
77     ok(utf8::is_utf8($foo->utf8_string), '... the string is still utf8');
78
79     is($foo->utf8_string, 
80       "Escritório", 
81       '... got the string we expected');
82 }
83
84 unlink $file;