New data for Unicode on older versions, thanks to Nicholas
[p5sagit/p5-mst-13.2.git] / lib / DBM_Filter / t / 01error.t
CommitLineData
0e9b1cbd 1
2use strict;
3use warnings;
4use Carp;
5
6use lib '.';
7our $db ;
8
9{
10 chdir 't' if -d 't';
11 if ( ! -d 'DBM_Filter')
12 {
13 mkdir 'DBM_Filter', 0777
9aedf6d8 14 or die "Cannot create directory 'DBM_Filter': $!\n" ;
0e9b1cbd 15 }
16}
17
9aedf6d8 18END { rmdir 'DBM_Filter' }
19
0e9b1cbd 20sub writeFile
21{
22 my $filename = shift ;
23 my $content = shift;
9aedf6d8 24 open F, ">$filename" or croak "Cannot open $filename: $!" ;
0e9b1cbd 25 print F $content ;
26 close F;
27}
28
29sub runFilter
30{
31 my $name = shift ;
32 my $filter = shift ;
33
34print "# runFilter $name\n" ;
35 my $filename = "DBM_Filter/$name.pm";
36 $filter = "package DBM_Filter::$name ;\n$filter"
37 unless $filter =~ /^\s*package/ ;
38
39 writeFile($filename, $filter);
40 eval { $db->Filter_Push($name) };
41 unlink $filename;
42 return $@;
43}
44
45use Test::More tests => 21;
46
47BEGIN { use_ok('DBM_Filter') };
e8ebc68a 48my $db_file;
49BEGIN {
50 use Config;
51 foreach (qw/ODBM_File SDBM_File NDBM_File GDBM_File DB_File/) {
52 if ($Config{extensions} =~ /\b$_\b/) {
53 $db_file = $_;
54 last;
55 }
56 }
57 use_ok($db_file);
58};
0e9b1cbd 59BEGIN { use_ok('Fcntl') };
60
61unlink <Op_dbmx*>;
62END { unlink <Op_dbmx*>; }
63
64my %h1 = () ;
65my %h2 = () ;
e8ebc68a 66$db = tie(%h1, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
0e9b1cbd 67
e8ebc68a 68ok $db, "tied to $db_file ok";
0e9b1cbd 69
70
71# Error cases
72
73eval { $db->Filter_Push() ; };
74like $@, qr/^Filter_Push: no parameters present/,
75 "croak if not parameters passed to Filter_Push";
76
77eval { $db->Filter_Push("unknown_class") ; };
78like $@, qr/^Filter_Push: Cannot Load DBM Filter 'DBM_Filter::unknown_class'/,
79 "croak on unknown class" ;
80
81eval { $db->Filter_Push("Some::unknown_class") ; };
82like $@, qr/^Filter_Push: Cannot Load DBM Filter 'Some::unknown_class'/,
83 "croak on unknown fully qualified class" ;
84
85eval { $db->Filter_Push('Store') ; };
86like $@, qr/^Filter_Push: not even params/,
87 "croak if not passing even number or params to Filter_Push";
88
89runFilter('bad1', <<'EOM');
90 package DBM_Filter::bad1 ;
91 1;
92EOM
93
94like $@, qr/^Filter_Push: No methods \(Filter, Fetch or Store\) found in class 'DBM_Filter::bad1'/,
95 "croak if none of Filter/Fetch/Store in filter" ;
96
97
98runFilter('bad2', <<'EOM');
99 package DBM_Filter::bad2 ;
100
101 sub Filter
102 {
103 return 2;
104 }
105
106 1;
107EOM
108
109like $@, qr/^Filter_Push: 'DBM_Filter::bad2::Filter' did not return a hash reference./,
110 "croak if Filter doesn't return hash reference" ;
111
112runFilter('bad3', <<'EOM');
113 package DBM_Filter::bad3 ;
114
115 sub Filter
116 {
117 return { BadKey => sub { } } ;
118
119 }
120
121 1;
122EOM
123
124like $@, qr/^Filter_Push: Unknown key 'BadKey'/,
125 "croak if bad keyword returned from Filter";
126
127runFilter('bad4', <<'EOM');
128 package DBM_Filter::bad4 ;
129
130 sub Filter
131 {
132 return { Store => "abc" } ;
133 }
134
135 1;
136EOM
137
138like $@, qr/^Filter_Push: value associated with key 'Store' is not a code reference/,
139 "croak if not a code reference";
140
141runFilter('bad5', <<'EOM');
142 package DBM_Filter::bad5 ;
143
144 sub Filter
145 {
146 return { } ;
147 }
148
149 1;
150EOM
151
152like $@, qr/^Filter_Push: expected both Store & Fetch - got neither/,
153 "croak if neither fetch or store is present";
154
155runFilter('bad6', <<'EOM');
156 package DBM_Filter::bad6 ;
157
158 sub Filter
159 {
160 return { Store => sub {} } ;
161 }
162
163 1;
164EOM
165
166like $@, qr/^Filter_Push: expected both Store & Fetch - got Store/,
167 "croak if store is present but fetch isn't";
168
169runFilter('bad7', <<'EOM');
170 package DBM_Filter::bad7 ;
171
172 sub Filter
173 {
174 return { Fetch => sub {} } ;
175 }
176
177 1;
178EOM
179
180like $@, qr/^Filter_Push: expected both Store & Fetch - got Fetch/,
181 "croak if fetch is present but store isn't";
182
183runFilter('bad8', <<'EOM');
184 package DBM_Filter::bad8 ;
185
186 sub Filter {}
187 sub Store {}
188 sub Fetch {}
189
190 1;
191EOM
192
193like $@, qr/^Filter_Push: Can't mix Filter with Store and Fetch in class 'DBM_Filter::bad8'/,
194 "croak if Fetch, Store and Filter";
195
196runFilter('bad9', <<'EOM');
197 package DBM_Filter::bad9 ;
198
199 sub Filter {}
200 sub Store {}
201
202 1;
203EOM
204
205like $@, qr/^Filter_Push: Can't mix Filter with Store and Fetch in class 'DBM_Filter::bad9'/,
206 "croak if Store and Filter";
207
208runFilter('bad10', <<'EOM');
209 package DBM_Filter::bad10 ;
210
211 sub Filter {}
212 sub Fetch {}
213
214 1;
215EOM
216
217like $@, qr/^Filter_Push: Can't mix Filter with Store and Fetch in class 'DBM_Filter::bad10'/,
218 "croak if Fetch and Filter";
219
220runFilter('bad11', <<'EOM');
221 package DBM_Filter::bad11 ;
222
223 sub Fetch {}
224
225 1;
226EOM
227
228like $@, qr/^Filter_Push: Missing method 'Store' in class 'DBM_Filter::bad11'/,
229 "croak if Fetch but no Store";
230
231runFilter('bad12', <<'EOM');
232 package DBM_Filter::bad12 ;
233
234 sub Store {}
235
236 1;
237EOM
238
239like $@, qr/^Filter_Push: Missing method 'Fetch' in class 'DBM_Filter::bad12'/,
240 "croak if Store but no Fetch";
241
242undef $db;
243{
244 use warnings FATAL => 'untie';
245 eval { untie %h1 };
246 is $@, '', "untie without inner references" ;
247}
248