This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Uncomment and fix up tests at the end of Storable's blessed.t
[perl5.git] / dist / Storable / t / store.t
1 #!./perl
2 #
3 #  Copyright (c) 1995-2000, Raphael Manfredi
4 #  
5 #  You may redistribute only under the same terms as Perl 5, as specified
6 #  in the README file that comes with the distribution.
7 #
8
9 sub BEGIN {
10     unshift @INC, 't';
11     require Config; import Config;
12     if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
13         print "1..0 # Skip: Storable was not built\n";
14         exit 0;
15     }
16     require 'st-dump.pl';
17 }
18
19 use Storable qw(store retrieve store_fd nstore_fd fd_retrieve);
20
21 print "1..20\n";
22
23 $a = 'toto';
24 $b = \$a;
25 $c = bless {}, CLASS;
26 $c->{attribute} = 'attrval';
27 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
28 @a = ('first', undef, 3, -4, -3.14159, 456, 4.5,
29         $b, \$a, $a, $c, \$c, \%a);
30
31 print "not " unless defined store(\@a, 'store');
32 print "ok 1\n";
33
34 $dumped = &dump(\@a);
35 print "ok 2\n";
36
37 $root = retrieve('store');
38 print "not " unless defined $root;
39 print "ok 3\n";
40
41 $got = &dump($root);
42 print "ok 4\n";
43
44 print "not " unless $got eq $dumped; 
45 print "ok 5\n";
46
47 1 while unlink 'store';
48
49 package FOO; @ISA = qw(Storable);
50
51 sub make {
52         my $self = bless {};
53         $self->{key} = \%main::a;
54         return $self;
55 };
56
57 package main;
58
59 $foo = FOO->make;
60 print "not " unless $foo->store('store');
61 print "ok 6\n";
62
63 print "not " unless open(OUT, '>>store');
64 print "ok 7\n";
65 binmode OUT;
66
67 print "not " unless defined store_fd(\@a, ::OUT);
68 print "ok 8\n";
69 print "not " unless defined nstore_fd($foo, ::OUT);
70 print "ok 9\n";
71 print "not " unless defined nstore_fd(\%a, ::OUT);
72 print "ok 10\n";
73
74 print "not " unless close(OUT);
75 print "ok 11\n";
76
77 print "not " unless open(OUT, 'store');
78 binmode OUT;
79
80 $r = fd_retrieve(::OUT);
81 print "not " unless defined $r;
82 print "ok 12\n";
83 print "not " unless &dump($foo) eq &dump($r);
84 print "ok 13\n";
85
86 $r = fd_retrieve(::OUT);
87 print "not " unless defined $r;
88 print "ok 14\n";
89 print "not " unless &dump(\@a) eq &dump($r);
90 print "ok 15\n";
91
92 $r = fd_retrieve(main::OUT);
93 print "not " unless defined $r;
94 print "ok 16\n";
95 print "not " unless &dump($foo) eq &dump($r);
96 print "ok 17\n";
97
98 $r = fd_retrieve(::OUT);
99 print "not " unless defined $r;
100 print "ok 18\n";
101 print "not " unless &dump(\%a) eq &dump($r);
102 print "ok 19\n";
103
104 eval { $r = fd_retrieve(::OUT); };
105 print "not " unless $@;
106 print "ok 20\n";
107
108 close OUT or die "Could not close: $!";
109 END { 1 while unlink 'store' }