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