5eb94af29bcc68469f354b8695f4fc78dccf99ca
[ZOOM-Perl-moved-to-github.git] / t / 21-option-callback.t
1 # $Id: 21-option-callback.t,v 1.1 2005-10-24 16:42:31 mike Exp $
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 21-option-calback.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 19;
9 BEGIN { use_ok('ZOOM') };
10
11 # This callback function provides values only options whose names
12 # begin with consonants, in which case the value is the option name
13 # concatenated with a hyphen and the value of the user-data that was
14 # lodged along with the callback.
15 #
16 sub f_option {
17     my($udata, $name) = @_;
18
19     return undef if $name =~ /^[aeiou]/;
20     return "$name-$udata";
21 }
22
23 my $o1 = new ZOOM::Options();
24 $o1->set_callback("main::f_option", "xyz");
25 $o1->option(isisaurus => "was titanosaurus");
26
27 check($o1, "apatosaurus", undef);
28 check($o1, "brachiosaurus", "brachiosaurus-xyz");
29 check($o1, "camarasaurus", "camarasaurus-xyz");
30 check($o1, "diplodocus", "diplodocus-xyz");
31 check($o1, "euhelopus", undef);
32 check($o1, "futalognkosaurus", "futalognkosaurus-xyz");
33 check($o1, "gigantosaurus", "gigantosaurus-xyz");
34 check($o1, "haplocanthosaurus", "haplocanthosaurus-xyz");
35 check($o1, "isisaurus", "was titanosaurus");
36 check($o1, "janenschia", "janenschia-xyz");
37
38 my $o2 = new ZOOM::Options();
39 $o2->set_callback("main::f_option", "abc");
40 check($o2, "apatosaurus", undef);
41 check($o2, "brachiosaurus", "brachiosaurus-abc");
42 check($o2, "kxxxxxxxxxxxxx", "kxxxxxxxxxxxxx-abc");
43 check($o2, "limaysaurus", "limaysaurus-abc");
44 check($o2, "mamenchisaurus", "mamenchisaurus-abc");
45 check($o2, "nurosaurus", "nurosaurus-abc");
46 check($o2, "omeisaurus", undef);
47 check($o2, "patagosaurus", "patagosaurus-abc");
48
49 sub check {
50     my($opts, $key, $expected) = @_;
51
52     my $val = $opts->option($key);
53     #print "$opts($key) ", (defined $val ? "= '$val'" : "undefined"), "\n";
54     if (defined $expected) {
55         ok ($val eq $expected, "value for '$key' is '$val'");
56     } else {
57         ok (!defined $val, "no value for '$key'");
58     }
59 }