This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make Data::Dumper XS ignore Freezer return value
[perl5.git] / dist / Data-Dumper / t / freezer.t
CommitLineData
c5f7c514
ST
1#!./perl -w
2#
3# test a few problems with the Freezer option, not a complete Freezer
4# test suite yet
5
6BEGIN {
f8e2702e
NC
7 require Config; import Config;
8 no warnings 'once';
9 if ($Config{'extensions'} !~ /\bData\/Dumper\b/) {
10 print "1..0 # Skip: Data::Dumper was not built\n";
11 exit 0;
c5f7c514
ST
12 }
13}
14
15use strict;
16use Test::More qw(no_plan);
17use Data::Dumper;
18$Data::Dumper::Freezer = 'freeze';
19
20# test for seg-fault bug when freeze() returns a non-ref
21my $foo = Test1->new("foo");
22my $dumped_foo = Dumper($foo);
23ok($dumped_foo,
24 "Use of freezer sub which returns non-ref worked.");
25like($dumped_foo, qr/frozed/,
26 "Dumped string has the key added by Freezer.");
27
be022d23
AC
28# test that list-context freeze return doesn't contain the freezer's return
29# value; RT #116364
30like(join(" ", Dumper($foo)), qr/\A\$VAR1 = /,
31 "Dumped list doesn't begin with Freezer's return value");
32
c5f7c514
ST
33# run the same tests with useperl. this always worked
34{
35 local $Data::Dumper::Useperl = 1;
36 my $foo = Test1->new("foo");
37 my $dumped_foo = Dumper($foo);
38 ok($dumped_foo,
39 "Use of freezer sub which returns non-ref worked with useperl");
40 like($dumped_foo, qr/frozed/,
41 "Dumped string has the key added by Freezer with useperl.");
be022d23
AC
42 like(join(" ", Dumper($foo)), qr/\A\$VAR1 = /,
43 "Dumped list doesn't begin with Freezer's return value with useperl");
c5f7c514
ST
44}
45
46# test for warning when an object doesn't have a freeze()
47{
48 my $warned = 0;
49 local $SIG{__WARN__} = sub { $warned++ };
50 my $bar = Test2->new("bar");
51 my $dumped_bar = Dumper($bar);
52 is($warned, 0, "A missing freeze() shouldn't warn.");
53}
54
55
56# run the same test with useperl, which always worked
57{
58 local $Data::Dumper::Useperl = 1;
59 my $warned = 0;
60 local $SIG{__WARN__} = sub { $warned++ };
61 my $bar = Test2->new("bar");
62 my $dumped_bar = Dumper($bar);
63 is($warned, 0, "A missing freeze() shouldn't warn with useperl");
64}
65
66# a freeze() which die()s should still trigger the warning
67{
68 my $warned = 0;
69 local $SIG{__WARN__} = sub { $warned++; };
70 my $bar = Test3->new("bar");
71 my $dumped_bar = Dumper($bar);
72 is($warned, 1, "A freeze() which die()s should warn.");
73}
74
75# the same should work in useperl
76{
77 local $Data::Dumper::Useperl = 1;
78 my $warned = 0;
79 local $SIG{__WARN__} = sub { $warned++; };
80 my $bar = Test3->new("bar");
81 my $dumped_bar = Dumper($bar);
82 is($warned, 1, "A freeze() which die()s should warn with useperl.");
83}
84
85# a package with a freeze() which returns a non-ref
86package Test1;
87sub new { bless({name => $_[1]}, $_[0]) }
88sub freeze {
89 my $self = shift;
90 $self->{frozed} = 1;
91}
92
93# a package without a freeze()
94package Test2;
95sub new { bless({name => $_[1]}, $_[0]) }
96
97# a package with a freeze() which dies
98package Test3;
99sub new { bless({name => $_[1]}, $_[0]) }
c4a6f826 100sub freeze { die "freeze() is broken" }