This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
don't recurse infinitely in Data::Dumper
[perl5.git] / dist / Data-Dumper / t / freezer_useperl.t
1 #!./perl -w
2 #
3 # test a few problems with the Freezer option, not a complete Freezer
4 # test suite yet
5
6 BEGIN {
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;
12     }
13 }
14
15 use strict;
16 use Test::More tests =>  7;
17 use Data::Dumper;
18 use lib qw( ./t/lib );
19 use Testing qw( _dumptostr );
20
21 local $Data::Dumper::Useperl = 1;
22
23 {
24     local $Data::Dumper::Freezer = 'freeze';
25
26     # test for seg-fault bug when freeze() returns a non-ref
27     {
28         my $foo = Test1->new("foo");
29         my $dumped_foo = Dumper($foo);
30         ok($dumped_foo,
31            "Use of freezer sub which returns non-ref worked.");
32         like($dumped_foo, qr/frozed/,
33              "Dumped string has the key added by Freezer with useperl.");
34         like(join(" ", Dumper($foo)), qr/\A\$VAR1 = /,
35              "Dumped list doesn't begin with Freezer's return value with useperl");
36     }
37
38     # test for warning when an object does not have a freeze()
39     {
40         my $warned = 0;
41         local $SIG{__WARN__} = sub { $warned++ };
42         my $bar = Test2->new("bar");
43         my $dumped_bar = Dumper($bar);
44         is($warned, 0, "A missing freeze() shouldn't warn.");
45     }
46
47     # a freeze() which die()s should still trigger the warning
48     {
49         my $warned = 0;
50         local $SIG{__WARN__} = sub { $warned++; };
51         my $bar = Test3->new("bar");
52         my $dumped_bar = Dumper($bar);
53         is($warned, 1, "A freeze() which die()s should warn.");
54     }
55
56 }
57
58 {
59     my ($obj, %dumps);
60     my $foo = Test1->new("foo");
61
62     local $Data::Dumper::Freezer = '';
63     $obj = Data::Dumper->new( [ $foo ] );
64     $dumps{'ddfemptystr'} = _dumptostr($obj);
65
66     local $Data::Dumper::Freezer = undef;
67     $obj = Data::Dumper->new( [ $foo ] );
68     $dumps{'ddfundef'} = _dumptostr($obj);
69
70     is($dumps{'ddfundef'}, $dumps{'ddfemptystr'},
71         "\$Data::Dumper::Freezer same with empty string or undef");
72 }
73
74 {
75     my ($obj, %dumps);
76     my $foo = Test1->new("foo");
77
78     $obj = Data::Dumper->new( [ $foo ] );
79     $obj->Freezer('');
80     $dumps{'objemptystr'} = _dumptostr($obj);
81
82     $obj = Data::Dumper->new( [ $foo ] );
83     $obj->Freezer(undef);
84     $dumps{'objundef'} = _dumptostr($obj);
85
86     is($dumps{'objundef'}, $dumps{'objemptystr'},
87         "Freezer() same with empty string or undef");
88 }
89
90
91 # a package with a freeze() which returns a non-ref
92 package Test1;
93 sub new { bless({name => $_[1]}, $_[0]) }
94 sub freeze {
95     my $self = shift;
96     $self->{frozed} = 1;
97 }
98
99 # a package without a freeze()
100 package Test2;
101 sub new { bless({name => $_[1]}, $_[0]) }
102
103 # a package with a freeze() which dies
104 package Test3;
105 sub new { bless({name => $_[1]}, $_[0]) }
106 sub freeze { die "freeze() is broken" }