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