12 if ( ! -d 'DBM_Filter')
14 mkdir 'DBM_Filter', 0777
15 || die "Cannot create directory 'DBM_Filter': $!\n" ;
22 my $filename = shift ;
24 open F, ">DBM_Filter/$filename.pm" || croak "Cannot open $filename: $!" ;
27 $files{"DBM_Filter/$filename.pm"} ++;
30 END { unlink keys %files if keys %files }
32 use Test::More tests => 189;
34 BEGIN { use_ok('DBM_Filter') };
35 BEGIN { use_ok('SDBM_File') };
36 BEGIN { use_ok('Fcntl') };
39 END { unlink <Op_dbmx*>; }
41 writeFile('times_ten', <<'EOM');
42 package DBM_Filter::times_ten;
43 sub Store { $_ *= 10 }
44 sub Fetch { $_ /= 10 }
48 writeFile('append_A', <<'EOM');
49 package DBM_Filter::append_A;
50 sub Store { $_ .= 'A' }
55 writeFile('append_B', <<'EOM');
56 package DBM_Filter::append_B;
57 sub Store { $_ .= 'B' }
62 writeFile('append_C', <<'EOM');
63 package DBM_Filter::append_C;
64 sub Store { $_ .= 'C' }
69 writeFile('append_D', <<'EOM');
70 package DBM_Filter::append_D;
71 sub Store { $_ .= 'D' }
76 writeFile('append', <<'EOM');
77 package DBM_Filter::append;
82 Store => sub { $_ .= $string },
83 Fetch => sub { s/${string}$// }
89 writeFile('double', <<'EOM');
90 package DBM_Filter::double;
96 writeFile('uc', <<'EOM');
97 package DBM_Filter::uc;
98 sub Store { $_ = uc $_ }
99 sub Fetch { $_ = lc $_ }
103 writeFile('reverse', <<'EOM');
104 package DBM_Filter::reverse;
105 sub Store { $_ = reverse $_ }
106 sub Fetch { $_ = reverse $_ }
127 ok $$h{"abc"} eq "def", "read eq written" ;
128 ok $$h{"123"} eq "456", "read eq written" ;
136 no warnings 'uninitialized';
137 $$h{undef()} = undef();
138 $$h{"alpha"} = "beta";
139 $$h{"green"} = "blue";
140 ok $$h{""} eq "", "read eq written" ;
141 ok $$h{"green"} eq "blue", "read eq written" ;
142 ok $$h{"green"} eq "blue", "read eq written" ;
148 my $filename = shift ;
152 # read the dbm file without the filter
153 ok tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640), "tied to SDBM_File";
156 while (my ($k, $v) = each %h) {
157 if ( defined $expected{$k} && $expected{$k} eq $v ) {
158 delete $expected{$k} ;
164 ok keys(%expected) + keys(%bad) == 0, "Raw hash is ok";
166 if ( keys(%expected) + keys(%bad) ) {
167 my $bad = "Expected does not match actual\nExpected:\n" ;
168 while (my ($k, $v) = each %expected) {
169 $bad .= "\t'$k' =>\t'$v'\n";
172 while (my ($k, $v) = each %bad) {
173 $bad .= "\t'$k' =>\t'$v'\n";
179 use warnings FATAL => 'untie';
181 is $@, '', "untie without inner references" ;
187 #diag "Test Set: Key and Value Filter, no stacking, no closure";
190 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
191 ok $db, "tied to SDBM_File";
195 eval { $db->Filter_Push('append_A') };
196 is $@, '', "push 'append_A' filter" ;
202 use warnings FATAL => 'untie';
204 is $@, '', "untie without inner references" ;
217 #diag "Test Set: Key Only Filter, no stacking, no closure";
220 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
221 ok $db, "tied to SDBM_File";
225 eval { $db->Filter_Key_Push('append_A') };
226 is $@, '', "push 'append_A' filter" ;
232 use warnings FATAL => 'untie';
234 is $@, '', "untie without inner references" ;
247 #diag "Test Set: Value Only Filter, no stacking, no closure";
250 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
251 ok $db, "tied to SDBM_File";
255 eval { $db->Filter_Value_Push('append_A') };
256 is $@, '', "push 'append_A' filter" ;
262 use warnings FATAL => 'untie';
264 is $@, '', "untie without inner references" ;
277 #diag "Test Set: Key and Value Filter, with stacking, no closure";
280 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
281 ok $db, "tied to SDBM_File";
285 eval { $db->Filter_Push('append_A') };
286 is $@, '', "push 'append_A' filter" ;
288 eval { $db->Filter_Push('append_B') };
289 is $@, '', "push 'append_B' filter" ;
295 use warnings FATAL => 'untie';
297 is $@, '', "untie without inner references" ;
304 'alphaAB' => 'betaAB',
305 'greenAB' => 'blueAB';
310 #diag "Test Set: Key Filter != Value Filter, with stacking, no closure";
313 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
314 ok $db, "tied to SDBM_File";
318 eval { $db->Filter_Value_Push('append_A') };
319 is $@, '', "push 'append_A' filter" ;
321 eval { $db->Filter_Key_Push('append_B') };
322 is $@, '', "push 'append_B' filter" ;
324 eval { $db->Filter_Value_Push('append_C') };
325 is $@, '', "push 'append_C' filter" ;
327 eval { $db->Filter_Key_Push('append_D') };
328 is $@, '', "push 'append_D' filter" ;
334 use warnings FATAL => 'untie';
336 is $@, '', "untie without inner references" ;
343 'alphaBD' => 'betaAC',
344 'greenBD' => 'blueAC';
349 #diag "Test Set: Key only Filter, with stacking, no closure";
352 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
353 ok $db, "tied to SDBM_File";
357 eval { $db->Filter_Key_Push('append_B') };
358 is $@, '', "push 'append_B' filter" ;
360 eval { $db->Filter_Key_Push('append_D') };
361 is $@, '', "push 'append_D' filter" ;
367 use warnings FATAL => 'untie';
369 is $@, '', "untie without inner references" ;
382 #diag "Test Set: Value only Filter, with stacking, no closure";
385 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
386 ok $db, "tied to SDBM_File";
390 eval { $db->Filter_Value_Push('append_A') };
391 is $@, '', "push 'append_A' filter" ;
393 eval { $db->Filter_Value_Push('append_C') };
394 is $@, '', "push 'append_C' filter" ;
400 use warnings FATAL => 'untie';
402 is $@, '', "untie without inner references" ;
415 #diag "Test Set: Combination Key/Value + Key Filter != Value Filter, with stacking, no closure";
418 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
419 ok $db, "tied to SDBM_File";
423 eval { $db->Filter_Push('append_A') };
424 is $@, '', "push 'append_A' filter" ;
426 eval { $db->Filter_Value_Push('append_C') };
427 is $@, '', "push 'append_C' filter" ;
429 eval { $db->Filter_Key_Push('append_D') };
430 is $@, '', "push 'append_D' filter" ;
436 use warnings FATAL => 'untie';
438 is $@, '', "untie without inner references" ;
445 'alphaAD' => 'betaAC',
446 'greenAD' => 'blueAC';
451 #diag "Test Set: Combination Key/Value + Key + Key/Value, no closure";
454 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
455 ok $db, "tied to SDBM_File";
459 eval { $db->Filter_Push('append_A') };
460 is $@, '', "push 'append_A' filter" ;
462 eval { $db->Filter_Key_Push('append_B') };
463 is $@, '', "push 'append_B' filter" ;
465 eval { $db->Filter_Push('append_C') };
466 is $@, '', "push 'append_C' filter" ;
472 use warnings FATAL => 'untie';
474 is $@, '', "untie without inner references" ;
481 'alphaABC' => 'betaAC',
482 'greenABC' => 'blueAC';
487 #diag "Test Set: Combination Key/Value + Key + Key/Value, with closure";
490 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
491 ok $db, "tied to SDBM_File";
495 eval { $db->Filter_Push('append' => 'A') };
496 is $@, '', "push 'append_A' filter" ;
498 eval { $db->Filter_Key_Push('append' => 'B') };
499 is $@, '', "push 'append_B' filter" ;
501 eval { $db->Filter_Push('append' => 'C') };
502 is $@, '', "push 'append_C' filter" ;
508 use warnings FATAL => 'untie';
510 is $@, '', "untie without inner references" ;
517 'alphaABC' => 'betaAC',
518 'greenABC' => 'blueAC';
523 #diag "Test Set: Combination Key/Value + Key + Key/Value, immediate";
526 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
527 ok $db, "tied to SDBM_File";
533 Store => sub { $_ .= 'A' },
534 Fetch => sub { s/A$// }) };
535 is $@, '', "push 'append_A' filter" ;
538 $db->Filter_Key_Push(
539 Store => sub { $_ .= 'B' },
540 Fetch => sub { s/B$// }) };
541 is $@, '', "push 'append_B' filter" ;
545 Store => sub { $_ .= 'C' },
546 Fetch => sub { s/C$// }) };
547 is $@, '', "push 'append_C' filter" ;
553 use warnings FATAL => 'untie';
555 is $@, '', "untie without inner references" ;
562 'alphaABC' => 'betaAC',
563 'greenABC' => 'blueAC';
568 #diag "Test Set: Combination Key/Value + Key + Key/Value, immediate, closure";
571 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
572 ok $db, "tied to SDBM_File";
578 Store => sub { $_ .= 'A' },
579 Fetch => sub { s/A$// }) };
580 is $@, '', "push 'append_A' filter" ;
582 eval { $db->Filter_Key_Push('append_B') };
583 is $@, '', "push 'append_B' filter" ;
585 eval { $db->Filter_Push('append' => 'C') };
586 is $@, '', "push 'append_C' filter" ;
592 use warnings FATAL => 'untie';
594 is $@, '', "untie without inner references" ;
601 'alphaABC' => 'betaAC',
602 'greenABC' => 'blueAC';
607 #diag "Test Set: Filtered & Filter_Pop";
610 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
611 ok $db, "tied to SDBM_File";
615 ok ! $db->Filtered, "not filtered" ;
619 Store => sub { $_ .= 'A' },
620 Fetch => sub { s/A$// }) };
621 is $@, '', "push 'append_A' filter" ;
623 ok $db->Filtered, "is filtered" ;
625 eval { $db->Filter_Key_Push('append_B') };
626 is $@, '', "push 'append_B' filter" ;
628 ok $db->Filtered, "is filtered" ;
630 eval { $db->Filter_Push('append' => 'C') };
631 is $@, '', "push 'append_C' filter" ;
633 ok $db->Filtered, "is filtered" ;
637 eval { $db->Filter_Pop() };
638 is $@, '', "Filter_Pop";
640 ok $db->Filtered, "is filtered" ;
642 $h{'after'} = 'noon';
643 is $h{'after'}, 'noon', "read eq written";
645 eval { $db->Filter_Pop() };
646 is $@, '', "Filter_Pop";
648 ok $db->Filtered, "is filtered" ;
650 $h{'morning'} = 'after';
651 is $h{'morning'}, 'after', "read eq written";
653 eval { $db->Filter_Pop() };
654 is $@, '', "Filter_Pop";
656 ok ! $db->Filtered, "not filtered" ;
658 $h{'and'} = 'finally';
659 is $h{'and'}, 'finally', "read eq written";
661 eval { $db->Filter_Pop() };
662 is $@, '', "Filter_Pop";
666 use warnings FATAL => 'untie';
668 is $@, '', "untie without inner references" ;
675 'alphaABC' => 'betaAC',
676 'greenABC' => 'blueAC',
677 'afterAB' => 'noonA',
678 'morningA' => 'afterA',
684 #diag "Test Set: define the filter package in-line";
687 package DBM_Filter::append_X;
689 sub Store { $_ .= 'X' }
694 my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640) ;
695 ok $db, "tied to SDBM_File";
699 eval { $db->Filter_Push('append_X') };
700 is $@, '', "push 'append_X' filter" ;
706 use warnings FATAL => 'untie';
708 is $@, '', "untie without inner references" ;