File: storable.t

package info (click to toggle)
libdatetime-locale-perl 1%3A1.37-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,856 kB
  • sloc: perl: 225,932; sh: 23; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 1,770 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use strict;
use warnings;

use Test2::Require::Module (
    Storable => '0',
);

use Test2::V0;
use Test::File::ShareDir::Dist { 'DateTime-Locale' => 'share' };

use DateTime::Locale;
use File::Spec;
use File::Temp          qw( tempdir );
use IPC::System::Simple qw( capturex );
use Storable;

my $dir = tempdir( CLEANUP => 1 );

{
    my $loc1   = DateTime::Locale->load('en-US');
    my $frozen = Storable::nfreeze($loc1);

    ok(
        length $frozen < 2000,
        'the serialized locale object should not be immense'
    );

    my $loc2 = Storable::thaw($frozen);

    is( $loc2->code, 'en-US', 'thaw frozen locale object' );

    my $loc3 = Storable::dclone($loc1);

    is( $loc3->code, 'en-US', 'dclone object' );

    my $file = File::Spec->catfile( $dir, 'dt-locale.storable' );

    open my $fh, '>', $file or die $!;
    print {$fh} $frozen or die $!;
    close $fh           or die $!;
}

{
    my $pl_file   = File::Spec->catfile( $dir, 'storable-test.pl' );
    my $data_file = File::Spec->catfile( $dir, 'dt-locale.storable' );

    # We need to make sure that the object can be thawed in a process that has not
    # yet loaded DateTime::Locale. See
    # https://github.com/houseabsolute/DateTime-Locale/issues/18.
    my $code = <<'EOF';
use strict;
use warnings;

use Storable qw( thaw );

open my $fh, '<', shift or die $!;
my $loc = thaw( do { local $/; <$fh> });
print $loc->code . "\n";
EOF

    open my $fh, '>', $pl_file or die $!;
    print {$fh} $code or die $!;
    close $fh         or die $!;

    my $id = capturex( $^X, $pl_file, $data_file );
    chomp $id;
    is(
        $id,
        'en-US',
        'can thaw a DateTime::Locale::FromData object in a process that has not loaded DateTime::Locale yet'
    );
}

done_testing();