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') };
36 BEGIN { use_ok('SDBM_File') };
37 BEGIN { use_ok('Fcntl') };
40 END { unlink <Op_dbmx*>; }
42 writeFile('times_ten', <<'EOM');
43 package DBM_Filter::times_ten;
44 sub Store { $_ *= 10 }
45 sub Fetch { $_ /= 10 }
49 writeFile('append_A', <<'EOM');
50 package DBM_Filter::append_A;
51 sub Store { $_ .= 'A' }
56 writeFile('append_B', <<'EOM');
57 package DBM_Filter::append_B;
58 sub Store { $_ .= 'B' }
63 writeFile('append_C', <<'EOM');
64 package DBM_Filter::append_C;
65 sub Store { $_ .= 'C' }
70 writeFile('append_D', <<'EOM');
71 package DBM_Filter::append_D;
72 sub Store { $_ .= 'D' }
77 writeFile('append', <<'EOM');
78 package DBM_Filter::append;
83 Store => sub { $_ .= $string },
84 Fetch => sub { s/${string}$// }
90 writeFile('double', <<'EOM');
91 package DBM_Filter::double;
97 writeFile('uc', <<'EOM');
98 package DBM_Filter::uc;
99 sub Store { $_ = uc $_ }
100 sub Fetch { $_ = lc $_ }
104 writeFile('reverse', <<'EOM');
105 package DBM_Filter::reverse;
106 sub Store { $_ = reverse $_ }
107 sub Fetch { $_ = reverse $_ }
128 ok $$h{"abc"} eq "def", "read eq written" ;
129 ok $$h{"123"} eq "456", "read eq written" ;
137 no warnings 'uninitialized';
138 $$h{undef()} = undef();
139 $$h{"alpha"} = "beta";
140 $$h{"green"} = "blue";
141 ok $$h{""} eq "", "read eq written" ;
142 ok $$h{"green"} eq "blue", "read eq written" ;
143 ok $$h{"green"} eq "blue", "read eq written" ;
149 my $filename = shift ;
153 # read the dbm file without the filter
154 ok tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640), "tied to SDBM_File";
157 while (my ($k, $v) = each %h) {
158 if ( defined $expected{$k} && $expected{$k} eq $v ) {
159 delete $expected{$k} ;
165 ok keys(%expected) + keys(%bad) == 0, "Raw hash is ok";
167 if ( keys(%expected) + keys(%bad) ) {
168 my $bad = "Expected does not match actual\nExpected:\n" ;
169 while (my ($k, $v) = each %expected) {
170 $bad .= "\t'$k' =>\t'$v'\n";
173 while (my ($k, $v) = each %bad) {
174 $bad .= "\t'$k' =>\t'$v'\n";
180 use warnings FATAL => 'untie';
182 is $@, '', "untie without inner references" ;
188 #diag "Test Set: Key and Value Filter, no stacking, no closure";
191 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
192 ok $db, "tied to SDBM_File";
196 eval { $db->Filter_Push('append_A') };
197 is $@, '', "push 'append_A' filter" ;
203 use warnings FATAL => 'untie';
205 is $@, '', "untie without inner references" ;
218 #diag "Test Set: Key Only Filter, no stacking, no closure";
221 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
222 ok $db, "tied to SDBM_File";
226 eval { $db->Filter_Key_Push('append_A') };
227 is $@, '', "push 'append_A' filter" ;
233 use warnings FATAL => 'untie';
235 is $@, '', "untie without inner references" ;
248 #diag "Test Set: Value Only Filter, no stacking, no closure";
251 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
252 ok $db, "tied to SDBM_File";
256 eval { $db->Filter_Value_Push('append_A') };
257 is $@, '', "push 'append_A' filter" ;
263 use warnings FATAL => 'untie';
265 is $@, '', "untie without inner references" ;
278 #diag "Test Set: Key and Value Filter, with stacking, no closure";
281 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
282 ok $db, "tied to SDBM_File";
286 eval { $db->Filter_Push('append_A') };
287 is $@, '', "push 'append_A' filter" ;
289 eval { $db->Filter_Push('append_B') };
290 is $@, '', "push 'append_B' filter" ;
296 use warnings FATAL => 'untie';
298 is $@, '', "untie without inner references" ;
305 'alphaAB' => 'betaAB',
306 'greenAB' => 'blueAB';
311 #diag "Test Set: Key Filter != Value Filter, with stacking, no closure";
314 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
315 ok $db, "tied to SDBM_File";
319 eval { $db->Filter_Value_Push('append_A') };
320 is $@, '', "push 'append_A' filter" ;
322 eval { $db->Filter_Key_Push('append_B') };
323 is $@, '', "push 'append_B' filter" ;
325 eval { $db->Filter_Value_Push('append_C') };
326 is $@, '', "push 'append_C' filter" ;
328 eval { $db->Filter_Key_Push('append_D') };
329 is $@, '', "push 'append_D' filter" ;
335 use warnings FATAL => 'untie';
337 is $@, '', "untie without inner references" ;
344 'alphaBD' => 'betaAC',
345 'greenBD' => 'blueAC';
350 #diag "Test Set: Key only Filter, with stacking, no closure";
353 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
354 ok $db, "tied to SDBM_File";
358 eval { $db->Filter_Key_Push('append_B') };
359 is $@, '', "push 'append_B' filter" ;
361 eval { $db->Filter_Key_Push('append_D') };
362 is $@, '', "push 'append_D' filter" ;
368 use warnings FATAL => 'untie';
370 is $@, '', "untie without inner references" ;
383 #diag "Test Set: Value only Filter, with stacking, no closure";
386 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
387 ok $db, "tied to SDBM_File";
391 eval { $db->Filter_Value_Push('append_A') };
392 is $@, '', "push 'append_A' filter" ;
394 eval { $db->Filter_Value_Push('append_C') };
395 is $@, '', "push 'append_C' filter" ;
401 use warnings FATAL => 'untie';
403 is $@, '', "untie without inner references" ;
416 #diag "Test Set: Combination Key/Value + Key Filter != Value Filter, with stacking, no closure";
419 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
420 ok $db, "tied to SDBM_File";
424 eval { $db->Filter_Push('append_A') };
425 is $@, '', "push 'append_A' filter" ;
427 eval { $db->Filter_Value_Push('append_C') };
428 is $@, '', "push 'append_C' filter" ;
430 eval { $db->Filter_Key_Push('append_D') };
431 is $@, '', "push 'append_D' filter" ;
437 use warnings FATAL => 'untie';
439 is $@, '', "untie without inner references" ;
446 'alphaAD' => 'betaAC',
447 'greenAD' => 'blueAC';
452 #diag "Test Set: Combination Key/Value + Key + Key/Value, no closure";
455 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
456 ok $db, "tied to SDBM_File";
460 eval { $db->Filter_Push('append_A') };
461 is $@, '', "push 'append_A' filter" ;
463 eval { $db->Filter_Key_Push('append_B') };
464 is $@, '', "push 'append_B' filter" ;
466 eval { $db->Filter_Push('append_C') };
467 is $@, '', "push 'append_C' filter" ;
473 use warnings FATAL => 'untie';
475 is $@, '', "untie without inner references" ;
482 'alphaABC' => 'betaAC',
483 'greenABC' => 'blueAC';
488 #diag "Test Set: Combination Key/Value + Key + Key/Value, with closure";
491 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
492 ok $db, "tied to SDBM_File";
496 eval { $db->Filter_Push('append' => 'A') };
497 is $@, '', "push 'append_A' filter" ;
499 eval { $db->Filter_Key_Push('append' => 'B') };
500 is $@, '', "push 'append_B' filter" ;
502 eval { $db->Filter_Push('append' => 'C') };
503 is $@, '', "push 'append_C' filter" ;
509 use warnings FATAL => 'untie';
511 is $@, '', "untie without inner references" ;
518 'alphaABC' => 'betaAC',
519 'greenABC' => 'blueAC';
524 #diag "Test Set: Combination Key/Value + Key + Key/Value, immediate";
527 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
528 ok $db, "tied to SDBM_File";
534 Store => sub { $_ .= 'A' },
535 Fetch => sub { s/A$// }) };
536 is $@, '', "push 'append_A' filter" ;
539 $db->Filter_Key_Push(
540 Store => sub { $_ .= 'B' },
541 Fetch => sub { s/B$// }) };
542 is $@, '', "push 'append_B' filter" ;
546 Store => sub { $_ .= 'C' },
547 Fetch => sub { s/C$// }) };
548 is $@, '', "push 'append_C' filter" ;
554 use warnings FATAL => 'untie';
556 is $@, '', "untie without inner references" ;
563 'alphaABC' => 'betaAC',
564 'greenABC' => 'blueAC';
569 #diag "Test Set: Combination Key/Value + Key + Key/Value, immediate, closure";
572 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
573 ok $db, "tied to SDBM_File";
579 Store => sub { $_ .= 'A' },
580 Fetch => sub { s/A$// }) };
581 is $@, '', "push 'append_A' filter" ;
583 eval { $db->Filter_Key_Push('append_B') };
584 is $@, '', "push 'append_B' filter" ;
586 eval { $db->Filter_Push('append' => 'C') };
587 is $@, '', "push 'append_C' filter" ;
593 use warnings FATAL => 'untie';
595 is $@, '', "untie without inner references" ;
602 'alphaABC' => 'betaAC',
603 'greenABC' => 'blueAC';
608 #diag "Test Set: Filtered & Filter_Pop";
611 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
612 ok $db, "tied to SDBM_File";
616 ok ! $db->Filtered, "not filtered" ;
620 Store => sub { $_ .= 'A' },
621 Fetch => sub { s/A$// }) };
622 is $@, '', "push 'append_A' filter" ;
624 ok $db->Filtered, "is filtered" ;
626 eval { $db->Filter_Key_Push('append_B') };
627 is $@, '', "push 'append_B' filter" ;
629 ok $db->Filtered, "is filtered" ;
631 eval { $db->Filter_Push('append' => 'C') };
632 is $@, '', "push 'append_C' filter" ;
634 ok $db->Filtered, "is filtered" ;
638 eval { $db->Filter_Pop() };
639 is $@, '', "Filter_Pop";
641 ok $db->Filtered, "is filtered" ;
643 $h{'after'} = 'noon';
644 is $h{'after'}, 'noon', "read eq written";
646 eval { $db->Filter_Pop() };
647 is $@, '', "Filter_Pop";
649 ok $db->Filtered, "is filtered" ;
651 $h{'morning'} = 'after';
652 is $h{'morning'}, 'after', "read eq written";
654 eval { $db->Filter_Pop() };
655 is $@, '', "Filter_Pop";
657 ok ! $db->Filtered, "not filtered" ;
659 $h{'and'} = 'finally';
660 is $h{'and'}, 'finally', "read eq written";
662 eval { $db->Filter_Pop() };
663 is $@, '', "Filter_Pop";
667 use warnings FATAL => 'untie';
669 is $@, '', "untie without inner references" ;
676 'alphaABC' => 'betaAC',
677 'greenABC' => 'blueAC',
678 'afterAB' => 'noonA',
679 'morningA' => 'afterA',
685 #diag "Test Set: define the filter package in-line";
688 package DBM_Filter::append_X;
690 sub Store { $_ .= 'X' }
695 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
696 ok $db, "tied to SDBM_File";
700 eval { $db->Filter_Push('append_X') };
701 is $@, '', "push 'append_X' filter" ;
707 use warnings FATAL => 'untie';
709 is $@, '', "untie without inner references" ;