Debian Codegraph @

t/mkopt.t

raw
1 #!perl
2 use strict;
3 use warnings;
4
5 =head1 TEST PURPOSE
6
7 These tests test option list cannonization (from an option list into a aref).
8
9 =cut
10
11 use Data::OptList;
12 use Sub::Install;
13 use Test::More 0.88;
14
15 # let's get a convenient copy to use:
16 Sub::Install::install_sub({
17 code => 'mkopt',
18 from => 'Data::OptList',
19 });
20
21 sub OPT {
22 # specifying moniker is tedious (also, these tests predate them)
23 splice @_, 1, 0, 'test' if @_ > 1;
24 mkopt(@_);
25 }
26
27 is_deeply(
28 OPT([]),
29 [],
30 "empty opt list expands properly",
31 );
32
33 is_deeply(
34 OPT(),
35 [],
36 "undef expands into []",
37 );
38
39 is_deeply(
40 OPT([ qw(foo bar baz) ]),
41 [ [ foo => undef ], [ bar => undef ], [ baz => undef ] ],
42 "opt list of just names expands",
43 );
44
45 {
46 my $options = OPT({ foo => undef, bar => 10, baz => [] });
47 $options = [ sort { $a->[0] cmp $b->[0] } @$options ];
48
49 is_deeply(
50 $options,
51 [ [ bar => undef ], [ baz => [] ], [ foo => undef ] ],
52 "hash opt list expands properly"
53 );
54 }
55
56 is_deeply(
57 OPT([ qw(foo bar baz) ], 0, "ARRAY"),
58 [ [ foo => undef ], [ bar => undef ], [ baz => undef ] ],
59 "opt list of just names expands with must_be",
60 );
61
62 is_deeply(
63 OPT([ qw(foo :bar baz) ]),
64 [ [ foo => undef ], [ ':bar' => undef ], [ baz => undef ] ],
65 "opt list of names expands with :group names",
66 );
67
68 is_deeply(
69 OPT([ foo => { a => 1 }, ':bar', 'baz' ]),
70 [ [ foo => { a => 1 } ], [ ':bar' => undef ], [ baz => undef ] ],
71 "opt list of names and values expands",
72 );
73
74 is_deeply(
75 OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, 'HASH'),
76 [ [ foo => { a => 1 } ], [ ':bar' => undef ], [ baz => undef ] ],
77 "opt list of names and values expands with must_be",
78 );
79
80 is_deeply(
81 OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, ['HASH']),
82 [ [ foo => { a => 1 } ], [ ':bar' => undef ], [ baz => undef ] ],
83 "opt list of names and values expands with [must_be]",
84 );
85
86 is_deeply(
87 OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, [qw< HASH CODE >]),
88 [ [ foo => { a => 1 } ], [ ':bar' => undef ], [ baz => undef ] ],
89 "opt list of names and values expands with [must_be]",
90 );
91
92 is_deeply(
93 OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, [qw< CODE HASH >]),
94 [ [ foo => { a => 1 } ], [ ':bar' => undef ], [ baz => undef ] ],
95 "opt list of names and values expands with [must_be]",
96 );
97
98 {
99 bless((my $object = {}), 'Test::DOL::Obj');
100 is_deeply(
101 OPT([ foo => $object, ':bar', 'baz' ], 0, 'Test::DOL::Obj'),
102 [ [ foo => $object ], [ ':bar' => undef ], [ baz => undef ] ],
103 "opt list of names and values expands with must_be, must_be object",
104 );
105
106 is_deeply(
107 OPT([ foo => $object, ':bar', 'baz' ], 0, ['Test::DOL::Obj']),
108 [ [ foo => $object ], [ ':bar' => undef ], [ baz => undef ] ],
109 "opt list of names and values expands with [must_be], must_be object",
110 );
111
112 is_deeply(
113 OPT([ foo => $object, ':bar', 'baz' ], 0, ['ARRAY', 'HASH', 'CODE', 'SCALAR', 'Test::DOL::Obj']),
114 [ [ foo => $object ], [ ':bar' => undef ], [ baz => undef ] ],
115 "opt list of names and values expands with [must_be], must_be object",
116 );
117
118 is_deeply(
119 OPT([ foo => $object, ':bar', 'baz' ], 0, ['Test::DOL::Obj', 'CODE']),
120 [ [ foo => $object ], [ ':bar' => undef ], [ baz => undef ] ],
121 "opt list of names and values expands with [must_be], must_be object",
122 );
123 }
124
125 eval { OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, 'ARRAY'); };
126 like($@, qr/HASH-ref values are not/, "exception tossed on invalid ref value");
127
128 eval { OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, ['ARRAY']); };
129 like($@, qr/HASH-ref values are not/, "exception tossed on invalid ref value");
130
131 eval { OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, ['ARRAY', 'CODE', 'SCALAR', 'Foo']); };
132 like($@, qr/HASH-ref values are not/, "exception tossed on invalid ref value");
133
134 eval {
135 mkopt(
136 [ foo => { a => 1 }, ':bar', 'baz' ],
137 {
138 moniker => 'test',
139 must_be => ['ARRAY'],
140 }
141 );
142 };
143 like($@, qr/HASH-ref values are not/, "exception tossed on invalid ref value");
144
145 eval { OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, 'Test::DOL::Obj'); };
146 like($@, qr/HASH-ref values are not/, "exception tossed on invalid ref value");
147
148 eval { OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, ['Test::DOL::Obj']); };
149 like($@, qr/HASH-ref values are not/, "exception tossed on invalid ref value");
150
151 eval { OPT([ foo => { a => 1 }, ':bar', 'baz' ], 0, ['CODE', 'Test::DOL::Obj']); };
152 like($@, qr/HASH-ref values are not/, "exception tossed on invalid ref value");
153
154 is_deeply(
155 OPT([ foo => { a => 1 }, ':bar' => undef, 'baz' ]),
156 [ [ foo => { a => 1 } ], [ ':bar' => undef ], [ baz => undef ] ],
157 "opt list of names and values expands, ignoring undef",
158 );
159
160 eval { OPT([ foo => { a => 1 }, ':bar' => undef, ':bar' ], 1); };
161 like($@, qr/multiple definitions/, "require_unique constraint catches repeat");
162
163 is_deeply(
164 OPT([ foo => { a => 1 }, ':bar' => undef, 'baz' ], 1),
165 [ [ foo => { a => 1 } ], [ ':bar' => undef ], [ baz => undef ] ],
166 "previously tested expansion OK with require_unique",
167 );
168
169 # This one is complicated. We defined name values as only non-references (the
170 # default) or arrayrefs, so arrayrefs are not turned into values as they
171 # usually are. (The subsequent test shows the default expectation.)
172 # -- rjbs, 2011-04-08
173 my @input =(
174 foo => { a => 1 },
175 bar => undef,
176 baz =>
177 xyz => [ 1, 2, 3 ],
178 );
179
180 is_deeply(
181 mkopt(
182 [ @input ],
183 {
184 moniker => 'test',
185 name_test => sub { ! ref $_[0] or Params::SomeUtil::_ARRAYLIKE($_[0]) },
186 },
187 ),
188 [
189 [ foo => { a => 1 } ],
190 [ bar => undef ],
191 [ baz => undef ],
192 [ xyz => undef ],
193 [ [ 1, 2, 3 ], undef ],
194 ],
195 );
196
197 is_deeply(
198 mkopt(
199 [ @input ],
200 {
201 moniker => 'test',
202 },
203 ),
204 [
205 [ foo => { a => 1 } ],
206 [ bar => undef ],
207 [ baz => undef ],
208 [ xyz => [ 1, 2, 3 ] ],
209 ],
210 );
211
212 done_testing;