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