File: 090_thaw.t

package info (click to toggle)
libsereal-decoder-perl 5.004%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,556 kB
  • sloc: ansic: 11,615; perl: 6,938; sh: 25; makefile: 9
file content (138 lines) | stat: -rw-r--r-- 3,456 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env perl
use strict;
use warnings;

use File::Spec;
use lib File::Spec->catdir(qw(t lib));

BEGIN {
    lib->import('lib')
      if !-d 't';
}
use Test::More;
use Sereal::TestSet qw(:all);
use Sereal::Decoder qw(decode_sereal);
use Data::Dumper;

if ( !have_encoder_and_decoder() ) {
    plan skip_all => 'Did not find right version of encoder';
}

my $last_thaw_arg;
{

    package Matcher;

    sub new {
        my ( $class, $pattern ) = @_;
        bless { pattern => $pattern }, $class;
    }

    sub FREEZE {
        my ( $self, $serializer ) = @_;
        return [ $self->{pattern} ], "22";
    }

    sub THAW {
        my ( $class, $serializer, @data ) = @_;
        $last_thaw_arg = Data::Dumper::Dumper( \@data );
        return $class->new( $data[0][0] );
    }
}

my $encoder_plain  = Sereal::Encoder->new();
my $encoder_freeze = Sereal::Encoder->new( { freeze_callbacks => 1 } );

sub strip_white {
    my $s = $_[0];
    $s =~ s/\s+/ /g;
    $s =~ s/\A\s+//;
    $s =~ s/\s+\z//;
    return $s;
}

sub no_warnings(&) {
    my @warn;
    local $SIG{__WARN__} = sub { push @warn, $_[0] };
    $_[0]->();
    return @warn == 0;
}

my $string  = 'string';
my $blessed = bless {}, 'Bespoke';
my $qr      = qr/foo/;

my @test_data = (
    {
        data        => $string,
        description => 'Str'
    },
    {
        data        => $blessed,
        description => 'Object without FREEZE/THAW'
    },
    {
        data        => Matcher->new($qr),
        description => 'Object without FREEZE/THAW (regexp)'
    },
    {
        data        => Matcher->new($string),
        description => '(Object with FREEZE/THAW) containing a Str'
    },
    {
        data        => Matcher->new($blessed),
        description =>
          '(Object with FREEZE/THAW) containing an (Object without FREEZE/THAW)'
    },
);

for my $data (@test_data) {
    ok no_warnings {
        decode_sereal( $encoder_plain->encode( $data->{data} ) );
    }, "Plain round-trip on $data->{description}";

    ok no_warnings {
        my $encoded = $encoder_freeze->encode( $data->{data} );
        decode_sereal($encoded);
    }, "FREEZE/THAW round-trip on $data->{description}";
}
my $want = strip_white(<<'EOF_THAW_ARG');
$VAR1 = [
           [
             bless( {}, 'Bespoke' )
           ],
           '22'
         ];
EOF_THAW_ARG
is( strip_white($last_thaw_arg), $want, "Last Thaw Args look as expected" );

$want = strip_white(<<'EOF_STRUCT');
$VAR1 = bless(
     [  [ bless( {}, 'Bespoke' ) ], '22', 'Matcher' ],
    'Sereal::Decoder::THAW_args'
);
EOF_STRUCT
my $decoded = decode_sereal( $encoder_freeze->encode( $test_data[-1]{data} ),
    { no_thaw_objects => 1 } );
is( strip_white( Dumper($decoded) ),
    $want, "no_thaw_objects=1 worked as expected" );

$want = strip_white(<<'EOF_STRUCT');
$VAR1 = bless( { 'pattern' => {} }, 'Matcher' );
EOF_STRUCT
$decoded = decode_sereal(
    $encoder_freeze->encode( $test_data[-1]{data} ),
    { no_thaw_objects => 0, no_bless_objects => 1 }
);
is( strip_white( Dumper($decoded) ),
    $want, "no_thaw_objects=0 no_bless_objects worked as expected" );

$want = strip_white(<<'EOF_STRUCT');
$VAR1 = bless( [ [ {} ], '22', 'Matcher' ], 'Sereal::Decoder::THAW_args' );
EOF_STRUCT
$decoded = decode_sereal( $encoder_freeze->encode( $test_data[-1]{data} ),
    { no_bless_objects => 1 } );
is( strip_white( Dumper($decoded) ),
    $want, "no_bless_objects=1 worked as expected" );
done_testing;
__END__