1 # $Id: 10-options.t,v 1.1 2005-10-18 17:01:55 mike Exp $
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 10-options.t'
8 use Test::More tests => 51;
9 BEGIN { use_ok('Net::Z3950::ZOOM') };
12 my $val2 = "$val1\0bar";
14 my $o1 = Net::Z3950::ZOOM::options_create();
15 Net::Z3950::ZOOM::options_set($o1, surname => "Taylor");
16 Net::Z3950::ZOOM::options_set($o1, firstname => "Mike");
17 ok(Net::Z3950::ZOOM::options_get($o1, "surname") eq "Taylor", "get 1");
18 ok(Net::Z3950::ZOOM::options_get($o1, "firstname") eq "Mike", "get 2");
20 my ($len, $val) = (29168);
22 Net::Z3950::ZOOM::options_set($o1, xyz => $val2);
23 $val = Net::Z3950::ZOOM::options_getl($o1, "xyz", $len);
25 "set/getl treats values as NUL-terminated, val='$val' len=$len");
27 Net::Z3950::ZOOM::options_setl($o1, xyz => $val2, length($val2));
28 $val = Net::Z3950::ZOOM::options_get($o1, "xyz");
30 "setl/get treats values as NUL-terminated, val='$val'");
32 Net::Z3950::ZOOM::options_setl($o1, xyz => $val2, length($val2));
33 $val = Net::Z3950::ZOOM::options_getl($o1, "xyz", $len);
35 "setl/getl treats values as opaque, val='$val' len=$len");
37 my $o2 = Net::Z3950::ZOOM::options_create_with_parent($o1);
38 ok(Net::Z3950::ZOOM::options_get($o2, "surname") eq "Taylor",
40 ok(Net::Z3950::ZOOM::options_get($o2, "firstname") eq "Mike",
43 Net::Z3950::ZOOM::options_set($o1, surname => "Parrish");
44 ok(Net::Z3950::ZOOM::options_get($o2, "surname") eq "Parrish",
45 "get via parent after replacement");
46 Net::Z3950::ZOOM::options_set($o2, surname => "Taylor");
47 ok(Net::Z3950::ZOOM::options_get($o2, "surname") eq "Taylor",
48 "get via parent after overwrite");
49 ok(Net::Z3950::ZOOM::options_get($o1, "surname") eq "Parrish",
50 "get from parent after child overwrite");
52 my $o3 = Net::Z3950::ZOOM::options_create();
53 Net::Z3950::ZOOM::options_set($o3, firstname => "Fiona");
55 my $o4 = Net::Z3950::ZOOM::options_create_with_parent2($o3, $o2);
56 $val = Net::Z3950::ZOOM::options_get($o4, "firstname");
58 "get via first parent overrides second '$val'");
59 ok(Net::Z3950::ZOOM::options_get($o4, "surname") eq "Taylor",
60 "get via first parent");
61 Net::Z3950::ZOOM::options_set($o1, initial => "P");
62 ok(Net::Z3950::ZOOM::options_get($o4, "initial") eq "P",
63 "get via grandparent");
65 Net::Z3950::ZOOM::options_destroy($o1);
66 ok(1, "grandparent destroyed");
67 $val = Net::Z3950::ZOOM::options_get($o4, "initial");
68 ok($val eq "P", "referenced object survived destruction");
70 Net::Z3950::ZOOM::options_destroy($o4);
71 ok(1, "grandchild destroyed");
72 Net::Z3950::ZOOM::options_destroy($o3);
73 ok(1, "first parent destroyed");
74 Net::Z3950::ZOOM::options_destroy($o2);
75 ok(1, "second parent destroyed");
77 $o1 = Net::Z3950::ZOOM::options_create();
78 # Strange but true: only "T" and "1" are considered true.
79 check_bool($o1, y => 0);
80 check_bool($o1, Y => 0);
81 check_bool($o1, t => 0);
82 check_bool($o1, T => 1);
83 check_bool($o1, n => 0);
84 check_bool($o1, N => 0);
85 check_bool($o1, 0 => 0);
86 check_bool($o1, 1 => 1);
87 check_bool($o1, 2 => 0);
88 check_bool($o1, 3 => 0);
89 check_bool($o1, yes => 0);
90 check_bool($o1, YES => 0);
91 check_bool($o1, true => 0);
92 check_bool($o1, TRUE => 0);
93 ok(Net::Z3950::ZOOM::options_get_bool($o1, "undefined", 1),
94 "get_bool() defaulted to true");
95 ok(!Net::Z3950::ZOOM::options_get_bool($o1, "undefined", 0),
96 "get_bool() defaulted to false");
99 my($o, $val, $truep) = @_;
100 Net::Z3950::ZOOM::options_set($o, x => $val);
101 ok(Net::Z3950::ZOOM::options_get_bool($o, "x", 1) eq $truep,
102 "get_bool() considers $val to be " . ($truep ? "true" : "false"));
105 check_int($o1, 0 => 0);
106 check_int($o1, 1 => 1);
107 check_int($o1, 2 => 2);
108 check_int($o1, 3 => 3);
109 check_int($o1, -17 => -17);
110 check_int($o1, "012" => 12);
111 check_int($o1, "0000003" => 3);
112 check_int($o1, " 3" => 3);
113 check_int($o1, " 34" => 34);
114 check_int($o1, " 3 4" => 3);
115 check_int($o1, " 3,456" => 3);
116 ok(Net::Z3950::ZOOM::options_get_int($o1, "undefined", 42) == 42,
117 "get_int() defaulted to 42");
120 my($o, $val, $expected) = @_;
121 Net::Z3950::ZOOM::options_set($o, x => $val);
122 my $nval = Net::Z3950::ZOOM::options_get_int($o, "x", 1);
123 ok($nval == $expected,
124 "get_int() considers $val to be $nval, expected $expected");
127 check_set_int($o1, 0 => 0);
128 check_set_int($o1, 3 => 3);
129 check_set_int($o1, -17 => -17);
130 check_set_int($o1, " 34" => 34);
133 my($o, $val, $expected) = @_;
134 Net::Z3950::ZOOM::options_set_int($o, x => $val);
135 my $nval = Net::Z3950::ZOOM::options_get_int($o, "x", 1);
136 ok($nval == $expected,
137 "get_int() considers $val to be $nval, expected $expected");