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