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