12 if ( ! -d 'DBM_Filter')
14 mkdir 'DBM_Filter', 0777
15 or die "Cannot create directory 'DBM_Filter': $!\n" ;
19 END { rmdir 'DBM_Filter' }
23 my $filename = shift ;
25 open F, ">DBM_Filter/$filename.pm" or croak "Cannot open $filename: $!" ;
28 $files{"DBM_Filter/$filename.pm"} ++;
31 END { unlink keys %files if keys %files }
33 use Test::More tests => 189;
35 BEGIN { use_ok('DBM_Filter') };
39 foreach (qw/SDBM_File ODBM_File NDBM_File GDBM_File DB_File/) {
40 if ($Config{extensions} =~ /\b$_\b/) {
47 BEGIN { use_ok('Fcntl') };
50 END { unlink <Op_dbmx*>; }
52 writeFile('times_ten', <<'EOM');
53 package DBM_Filter::times_ten;
54 sub Store { $_ *= 10 }
55 sub Fetch { $_ /= 10 }
59 writeFile('append_A', <<'EOM');
60 package DBM_Filter::append_A;
61 sub Store { $_ .= 'A' }
66 writeFile('append_B', <<'EOM');
67 package DBM_Filter::append_B;
68 sub Store { $_ .= 'B' }
73 writeFile('append_C', <<'EOM');
74 package DBM_Filter::append_C;
75 sub Store { $_ .= 'C' }
80 writeFile('append_D', <<'EOM');
81 package DBM_Filter::append_D;
82 sub Store { $_ .= 'D' }
87 writeFile('append', <<'EOM');
88 package DBM_Filter::append;
93 Store => sub { $_ .= $string },
94 Fetch => sub { s/${string}$// }
100 writeFile('double', <<'EOM');
101 package DBM_Filter::double;
102 sub Store { $_ *= 2 }
103 sub Fetch { $_ /= 2 }
107 writeFile('uc', <<'EOM');
108 package DBM_Filter::uc;
109 sub Store { $_ = uc $_ }
110 sub Fetch { $_ = lc $_ }
114 writeFile('reverse', <<'EOM');
115 package DBM_Filter::reverse;
116 sub Store { $_ = reverse $_ }
117 sub Fetch { $_ = reverse $_ }
138 ok $$h{"abc"} eq "def", "read eq written" ;
139 ok $$h{"123"} eq "456", "read eq written" ;
147 no warnings 'uninitialized';
148 $$h{undef()} = undef();
149 $$h{"alpha"} = "beta";
150 $$h{"green"} = "blue";
151 ok $$h{""} eq "", "read eq written" ;
152 ok $$h{"green"} eq "blue", "read eq written" ;
153 ok $$h{"green"} eq "blue", "read eq written" ;
159 my $filename = shift ;
163 # read the dbm file without the filter
164 ok tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640), "tied to $db_file";
167 while (my ($k, $v) = each %h) {
168 if ( defined $expected{$k} && $expected{$k} eq $v ) {
169 delete $expected{$k} ;
175 ok keys(%expected) + keys(%bad) == 0, "Raw hash is ok";
177 if ( keys(%expected) + keys(%bad) ) {
178 my $bad = "Expected does not match actual\nExpected:\n" ;
179 while (my ($k, $v) = each %expected) {
180 $bad .= "\t'$k' =>\t'$v'\n";
183 while (my ($k, $v) = each %bad) {
184 $bad .= "\t'$k' =>\t'$v'\n";
190 use warnings FATAL => 'untie';
192 is $@, '', "untie without inner references" ;
198 #diag "Test Set: Key and Value Filter, no stacking, no closure";
201 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
202 ok $db, "tied to $db_file";
206 eval { $db->Filter_Push('append_A') };
207 is $@, '', "push 'append_A' filter" ;
213 use warnings FATAL => 'untie';
215 is $@, '', "untie without inner references" ;
228 #diag "Test Set: Key Only Filter, no stacking, no closure";
231 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
232 ok $db, "tied to $db_file";
236 eval { $db->Filter_Key_Push('append_A') };
237 is $@, '', "push 'append_A' filter" ;
243 use warnings FATAL => 'untie';
245 is $@, '', "untie without inner references" ;
258 #diag "Test Set: Value Only Filter, no stacking, no closure";
261 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
262 ok $db, "tied to $db_file";
266 eval { $db->Filter_Value_Push('append_A') };
267 is $@, '', "push 'append_A' filter" ;
273 use warnings FATAL => 'untie';
275 is $@, '', "untie without inner references" ;
288 #diag "Test Set: Key and Value Filter, with stacking, no closure";
291 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
292 ok $db, "tied to $db_file";
296 eval { $db->Filter_Push('append_A') };
297 is $@, '', "push 'append_A' filter" ;
299 eval { $db->Filter_Push('append_B') };
300 is $@, '', "push 'append_B' filter" ;
306 use warnings FATAL => 'untie';
308 is $@, '', "untie without inner references" ;
315 'alphaAB' => 'betaAB',
316 'greenAB' => 'blueAB';
321 #diag "Test Set: Key Filter != Value Filter, with stacking, no closure";
324 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
325 ok $db, "tied to $db_file";
329 eval { $db->Filter_Value_Push('append_A') };
330 is $@, '', "push 'append_A' filter" ;
332 eval { $db->Filter_Key_Push('append_B') };
333 is $@, '', "push 'append_B' filter" ;
335 eval { $db->Filter_Value_Push('append_C') };
336 is $@, '', "push 'append_C' filter" ;
338 eval { $db->Filter_Key_Push('append_D') };
339 is $@, '', "push 'append_D' filter" ;
345 use warnings FATAL => 'untie';
347 is $@, '', "untie without inner references" ;
354 'alphaBD' => 'betaAC',
355 'greenBD' => 'blueAC';
360 #diag "Test Set: Key only Filter, with stacking, no closure";
363 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
364 ok $db, "tied to $db_file";
368 eval { $db->Filter_Key_Push('append_B') };
369 is $@, '', "push 'append_B' filter" ;
371 eval { $db->Filter_Key_Push('append_D') };
372 is $@, '', "push 'append_D' filter" ;
378 use warnings FATAL => 'untie';
380 is $@, '', "untie without inner references" ;
393 #diag "Test Set: Value only Filter, with stacking, no closure";
396 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
397 ok $db, "tied to $db_file";
401 eval { $db->Filter_Value_Push('append_A') };
402 is $@, '', "push 'append_A' filter" ;
404 eval { $db->Filter_Value_Push('append_C') };
405 is $@, '', "push 'append_C' filter" ;
411 use warnings FATAL => 'untie';
413 is $@, '', "untie without inner references" ;
426 #diag "Test Set: Combination Key/Value + Key Filter != Value Filter, with stacking, no closure";
429 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
430 ok $db, "tied to $db_file";
434 eval { $db->Filter_Push('append_A') };
435 is $@, '', "push 'append_A' filter" ;
437 eval { $db->Filter_Value_Push('append_C') };
438 is $@, '', "push 'append_C' filter" ;
440 eval { $db->Filter_Key_Push('append_D') };
441 is $@, '', "push 'append_D' filter" ;
447 use warnings FATAL => 'untie';
449 is $@, '', "untie without inner references" ;
456 'alphaAD' => 'betaAC',
457 'greenAD' => 'blueAC';
462 #diag "Test Set: Combination Key/Value + Key + Key/Value, no closure";
465 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
466 ok $db, "tied to $db_file";
470 eval { $db->Filter_Push('append_A') };
471 is $@, '', "push 'append_A' filter" ;
473 eval { $db->Filter_Key_Push('append_B') };
474 is $@, '', "push 'append_B' filter" ;
476 eval { $db->Filter_Push('append_C') };
477 is $@, '', "push 'append_C' filter" ;
483 use warnings FATAL => 'untie';
485 is $@, '', "untie without inner references" ;
492 'alphaABC' => 'betaAC',
493 'greenABC' => 'blueAC';
498 #diag "Test Set: Combination Key/Value + Key + Key/Value, with closure";
501 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
502 ok $db, "tied to $db_file";
506 eval { $db->Filter_Push('append' => 'A') };
507 is $@, '', "push 'append_A' filter" ;
509 eval { $db->Filter_Key_Push('append' => 'B') };
510 is $@, '', "push 'append_B' filter" ;
512 eval { $db->Filter_Push('append' => 'C') };
513 is $@, '', "push 'append_C' filter" ;
519 use warnings FATAL => 'untie';
521 is $@, '', "untie without inner references" ;
528 'alphaABC' => 'betaAC',
529 'greenABC' => 'blueAC';
534 #diag "Test Set: Combination Key/Value + Key + Key/Value, immediate";
537 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
538 ok $db, "tied to $db_file";
544 Store => sub { $_ .= 'A' },
545 Fetch => sub { s/A$// }) };
546 is $@, '', "push 'append_A' filter" ;
549 $db->Filter_Key_Push(
550 Store => sub { $_ .= 'B' },
551 Fetch => sub { s/B$// }) };
552 is $@, '', "push 'append_B' filter" ;
556 Store => sub { $_ .= 'C' },
557 Fetch => sub { s/C$// }) };
558 is $@, '', "push 'append_C' filter" ;
564 use warnings FATAL => 'untie';
566 is $@, '', "untie without inner references" ;
573 'alphaABC' => 'betaAC',
574 'greenABC' => 'blueAC';
579 #diag "Test Set: Combination Key/Value + Key + Key/Value, immediate, closure";
582 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
583 ok $db, "tied to $db_file";
589 Store => sub { $_ .= 'A' },
590 Fetch => sub { s/A$// }) };
591 is $@, '', "push 'append_A' filter" ;
593 eval { $db->Filter_Key_Push('append_B') };
594 is $@, '', "push 'append_B' filter" ;
596 eval { $db->Filter_Push('append' => 'C') };
597 is $@, '', "push 'append_C' filter" ;
603 use warnings FATAL => 'untie';
605 is $@, '', "untie without inner references" ;
612 'alphaABC' => 'betaAC',
613 'greenABC' => 'blueAC';
618 #diag "Test Set: Filtered & Filter_Pop";
621 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
622 ok $db, "tied to $db_file";
626 ok ! $db->Filtered, "not filtered" ;
630 Store => sub { $_ .= 'A' },
631 Fetch => sub { s/A$// }) };
632 is $@, '', "push 'append_A' filter" ;
634 ok $db->Filtered, "is filtered" ;
636 eval { $db->Filter_Key_Push('append_B') };
637 is $@, '', "push 'append_B' filter" ;
639 ok $db->Filtered, "is filtered" ;
641 eval { $db->Filter_Push('append' => 'C') };
642 is $@, '', "push 'append_C' filter" ;
644 ok $db->Filtered, "is filtered" ;
648 eval { $db->Filter_Pop() };
649 is $@, '', "Filter_Pop";
651 ok $db->Filtered, "is filtered" ;
653 $h{'after'} = 'noon';
654 is $h{'after'}, 'noon', "read eq written";
656 eval { $db->Filter_Pop() };
657 is $@, '', "Filter_Pop";
659 ok $db->Filtered, "is filtered" ;
661 $h{'morning'} = 'after';
662 is $h{'morning'}, 'after', "read eq written";
664 eval { $db->Filter_Pop() };
665 is $@, '', "Filter_Pop";
667 ok ! $db->Filtered, "not filtered" ;
669 $h{'and'} = 'finally';
670 is $h{'and'}, 'finally', "read eq written";
672 eval { $db->Filter_Pop() };
673 is $@, '', "Filter_Pop";
677 use warnings FATAL => 'untie';
679 is $@, '', "untie without inner references" ;
686 'alphaABC' => 'betaAC',
687 'greenABC' => 'blueAC',
688 'afterAB' => 'noonA',
689 'morningA' => 'afterA',
695 #diag "Test Set: define the filter package in-line";
698 package DBM_Filter::append_X;
700 sub Store { $_ .= 'X' }
705 my $db = tie(%h, $db_file,'Op_dbmx', O_RDWR|O_CREAT, 0640) ;
706 ok $db, "tied to $db_file";
710 eval { $db->Filter_Push('append_X') };
711 is $@, '', "push 'append_X' filter" ;
717 use warnings FATAL => 'untie';
719 is $@, '', "untie without inner references" ;