1use strict;
2use warnings;
3
4use Test2::Require::Module (
5    Storable => '0',
6);
7
8use Test2::V0;
9use Test::File::ShareDir::Dist { 'DateTime-Locale' => 'share' };
10
11use DateTime::Locale;
12use File::Spec;
13use File::Temp qw( tempdir );
14use IPC::System::Simple qw( capturex );
15use Storable;
16
17my $dir = tempdir( CLEANUP => 1 );
18
19{
20    my $loc1   = DateTime::Locale->load('en-US');
21    my $frozen = Storable::nfreeze($loc1);
22
23    ok(
24        length $frozen < 2000,
25        'the serialized locale object should not be immense'
26    );
27
28    my $loc2 = Storable::thaw($frozen);
29
30    is( $loc2->code, 'en-US', 'thaw frozen locale object' );
31
32    my $loc3 = Storable::dclone($loc1);
33
34    is( $loc3->code, 'en-US', 'dclone object' );
35
36    my $file = File::Spec->catfile( $dir, 'dt-locale.storable' );
37
38    open my $fh, '>', $file or die $!;
39    print {$fh} $frozen or die $!;
40    close $fh           or die $!;
41}
42
43{
44    my $pl_file   = File::Spec->catfile( $dir, 'storable-test.pl' );
45    my $data_file = File::Spec->catfile( $dir, 'dt-locale.storable' );
46
47    # We need to make sure that the object can be thawed in a process that has not
48    # yet loaded DateTime::Locale. See
49    # https://github.com/houseabsolute/DateTime-Locale/issues/18.
50    my $code = <<'EOF';
51use strict;
52use warnings;
53
54use Storable qw( thaw );
55
56open my $fh, '<', shift or die $!;
57my $loc = thaw( do { local $/; <$fh> });
58print $loc->code . "\n";
59EOF
60
61    open my $fh, '>', $pl_file or die $!;
62    print {$fh} $code or die $!;
63    close $fh         or die $!;
64
65    my $id = capturex( $^X, $pl_file, $data_file );
66    chomp $id;
67    is(
68        $id,
69        'en-US',
70        'can thaw a DateTime::Locale::FromData object in a process that has not loaded DateTime::Locale yet'
71    );
72}
73
74done_testing();
75