1#!perl -T
2use strict;
3use Test::More tests => 1+6*26;
4use Data::Dumper;
5
6BEGIN {
7	use_ok( 'Hash::WithDefaults' );
8}
9
10diag( "Testing Hash::WithDefaults $Hash::WithDefaults::VERSION, Perl $], $^X" );
11
12{ # lower
13	my %h;
14	ok(
15		tie( %h, 'Hash::WithDefaults', 'lower'),
16		"Tied with case=lower"
17	);
18
19	is( ref(tied %h), 'Hash::WithDefaults::lower', "Tied to the right class");
20
21	$h{low} = 'value';
22
23	is( $h{low}, 'value', '$h{low}');
24	is( $h{Low}, 'value', '$h{Low}');
25	is( $h{LOW}, 'value', '$h{LOW}');
26
27	$h{Mix} = 'other';
28
29	is( $h{mix}, 'other', '$h{mix}');
30	is( $h{Mix}, 'other', '$h{Mix}');
31	is( $h{MIX}, 'other', '$h{MIX}');
32
33	$h{HI} = 'Some';
34
35	is( $h{hi}, 'Some', '$h{hi}');
36	is( $h{Hi}, 'Some', '$h{Hi}');
37	is( $h{HI}, 'Some', '$h{HI}');
38
39	my @got = sort keys %h;
40	my @good = qw(hi low mix);
41
42	is_deeply(\@got, \@good, "List of keys");
43
44	@got = ();
45	while (my ($key, $val) = each %h) {
46		push @got, "$key=$val";
47	}
48	@got = sort @got;
49	@good = ('hi=Some', 'low=value', 'mix=other');
50	is_deeply(\@got, \@good, "each() returns both keys and values");
51
52	is( scalar( keys %h), 3, "Number of keys");
53
54	delete $h{Low};
55	is( scalar( keys %h), 2, "Number of keys after delete");
56	is( $h{low}, undef, '$h{low}');
57	is( $h{Low}, undef, '$h{Low}');
58	is( $h{LOW}, undef, '$h{LOW}');
59
60	delete $h{Mix};
61	is( scalar( keys %h), 1, "Number of keys after delete");
62	is( $h{mix}, undef, '$h{mix}');
63	is( $h{Mix}, undef, '$h{Mix}');
64	is( $h{MIX}, undef, '$h{MIX}');
65
66	%h = ();
67	is( scalar( keys %h), 0, "Number of keys after clear");
68	is_deeply( [keys %h], [], "There are no keys");
69
70	$h{a} = 1; $h{B} = 2;
71	is( scalar( keys %h), 2, "Number of keys after refil");
72	is_deeply( [sort keys %h], [qw(a b)], "There are two keys");
73
74}
75
76{ # upper
77	my %h;
78	ok(
79		tie( %h, 'Hash::WithDefaults', 'upper'),
80		"Tied with case=upper"
81	);
82
83	is( ref(tied %h), 'Hash::WithDefaults::upper', "Tied to the right class");
84
85	$h{low} = 'value';
86
87	is( $h{low}, 'value', '$h{low}');
88	is( $h{Low}, 'value', '$h{Low}');
89	is( $h{LOW}, 'value', '$h{LOW}');
90
91	$h{Mix} = 'other';
92
93	is( $h{mix}, 'other', '$h{mix}');
94	is( $h{Mix}, 'other', '$h{Mix}');
95	is( $h{MIX}, 'other', '$h{MIX}');
96
97	$h{HI} = 'Some';
98
99	is( $h{hi}, 'Some', '$h{hi}');
100	is( $h{Hi}, 'Some', '$h{Hi}');
101	is( $h{HI}, 'Some', '$h{HI}');
102
103	my @got = sort keys %h;
104	my @good = qw(HI LOW MIX);
105
106	is_deeply(\@got, \@good, "List of keys");
107
108	@got = ();
109	while (my ($key, $val) = each %h) {
110		push @got, "$key=$val";
111	}
112	@got = sort @got;
113	@good = ('HI=Some', 'LOW=value', 'MIX=other');
114	is_deeply(\@got, \@good, "each() returns both keys and values");
115
116	is( scalar( keys %h), 3, "Number of keys");
117
118	delete $h{Low};
119	is( scalar( keys %h), 2, "Number of keys after delete");
120	is( $h{low}, undef, '$h{low}');
121	is( $h{Low}, undef, '$h{Low}');
122	is( $h{LOW}, undef, '$h{LOW}');
123
124	delete $h{Mix};
125	is( scalar( keys %h), 1, "Number of keys after delete");
126	is( $h{mix}, undef, '$h{mix}');
127	is( $h{Mix}, undef, '$h{Mix}');
128	is( $h{MIX}, undef, '$h{MIX}');
129
130	%h = ();
131	is( scalar( keys %h), 0, "Number of keys after clear");
132	is_deeply( [keys %h], [], "There are no keys");
133
134	$h{a} = 1; $h{B} = 2;
135	is( scalar( keys %h), 2, "Number of keys after refil");
136	is_deeply( [sort keys %h], [qw(A B)], "There are two keys");
137
138}
139
140{ # preserve
141	my %h;
142	ok(
143		tie( %h, 'Hash::WithDefaults', 'preserve'),
144		"Tied with case=preserve"
145	);
146
147	is( ref(tied %h), 'Hash::WithDefaults::preserve', "Tied to the right class");
148
149	$h{low} = 'value';
150
151	is( $h{low}, 'value', '$h{low}');
152	is( $h{Low}, 'value', '$h{Low}');
153	is( $h{LOW}, 'value', '$h{LOW}');
154
155	$h{Mix} = 'other';
156
157	is( $h{mix}, 'other', '$h{mix}');
158	is( $h{Mix}, 'other', '$h{Mix}');
159	is( $h{MIX}, 'other', '$h{MIX}');
160
161	$h{HI} = 'Some';
162
163	is( $h{hi}, 'Some', '$h{hi}');
164	is( $h{Hi}, 'Some', '$h{Hi}');
165	is( $h{HI}, 'Some', '$h{HI}');
166
167	my @got = sort keys %h;
168	my @good = qw(HI Mix low);
169
170	is_deeply(\@got, \@good, "List of keys");
171
172	@got = ();
173	while (my ($key, $val) = each %h) {
174		push @got, "$key=$val";
175	}
176	@got = sort @got;
177	@good = ('HI=Some', 'Mix=other', 'low=value');
178	is_deeply(\@got, \@good, "each() returns both keys and values");
179
180	is( scalar( keys %h), 3, "Number of keys");
181
182	delete $h{Low};
183	is( scalar( keys %h), 2, "Number of keys after delete");
184	is( $h{low}, undef, '$h{low}');
185	is( $h{Low}, undef, '$h{Low}');
186	is( $h{LOW}, undef, '$h{LOW}');
187
188	delete $h{Mix};
189	is( scalar( keys %h), 1, "Number of keys after delete");
190	is( $h{mix}, undef, '$h{mix}');
191	is( $h{Mix}, undef, '$h{Mix}');
192	is( $h{MIX}, undef, '$h{MIX}');
193
194	%h = ();
195	is( scalar( keys %h), 0, "Number of keys after clear");
196	is_deeply( [keys %h], [], "There are no keys");
197
198	$h{a} = 1; $h{B} = 2;
199	is( scalar( keys %h), 2, "Number of keys after refil");
200	is_deeply( [sort keys %h], [qw(B a)], "There are two keys");
201
202}
203
204
205{ # tolower
206	my %h;
207	ok(
208		tie( %h, 'Hash::WithDefaults', 'tolower'),
209		"Tied with case=tolower"
210	);
211
212	is( ref(tied %h), 'Hash::WithDefaults::tolower', "Tied to the right class");
213
214	$h{low} = 'value';
215
216	is( $h{low}, 'value', '$h{low}');
217	is( $h{Low}, undef, '$h{Low}');
218	is( $h{LOW}, undef, '$h{LOW}');
219
220	$h{Mix} = 'other';
221
222	is( $h{mix}, 'other', '$h{mix}');
223	is( $h{Mix}, undef, '$h{Mix}');
224	is( $h{MIX}, undef, '$h{MIX}');
225
226	$h{HI} = 'Some';
227
228	is( $h{hi}, 'Some', '$h{hi}');
229	is( $h{Hi}, undef, '$h{Hi}');
230	is( $h{HI}, undef, '$h{HI}');
231
232	my @got = sort keys %h;
233	my @good = qw(hi low mix);
234
235	is_deeply(\@got, \@good, "List of keys");
236
237	@got = ();
238	while (my ($key, $val) = each %h) {
239		push @got, "$key=$val";
240	}
241	@got = sort @got;
242	@good = ('hi=Some', 'low=value', 'mix=other');
243	is_deeply(\@got, \@good, "each() returns both keys and values");
244
245	is( scalar( keys %h), 3, "Number of keys");
246
247	delete $h{low};
248	is( scalar( keys %h), 2, "Number of keys after delete");
249	is( $h{low}, undef, '$h{low}');
250	is( $h{Low}, undef, '$h{Low}');
251	is( $h{LOW}, undef, '$h{LOW}');
252
253	delete $h{Mix};
254	is( scalar( keys %h), 2, "Number of keys after delete");
255	is( $h{mix}, 'other', '$h{mix}');
256	is( $h{Mix}, undef, '$h{Mix}');
257	is( $h{MIX}, undef, '$h{MIX}');
258
259	%h = ();
260	is( scalar( keys %h), 0, "Number of keys after clear");
261	is_deeply( [keys %h], [], "There are no keys");
262
263	$h{a} = 1; $h{B} = 2;
264	is( scalar( keys %h), 2, "Number of keys after refil");
265	is_deeply( [sort keys %h], [qw(a b)], "There are two keys");
266
267}
268
269{ # toupper
270	my %h;
271	ok(
272		tie( %h, 'Hash::WithDefaults', 'toupper'),
273		"Tied with case=toupper"
274	);
275
276	is( ref(tied %h), 'Hash::WithDefaults::toupper', "Tied to the right class");
277
278	$h{low} = 'value';
279
280	is( $h{low}, undef, '$h{low}');
281	is( $h{Low}, undef, '$h{Low}');
282	is( $h{LOW}, 'value', '$h{LOW}');
283
284	$h{Mix} = 'other';
285
286	is( $h{mix}, undef, '$h{mix}');
287	is( $h{Mix}, undef, '$h{Mix}');
288	is( $h{MIX}, 'other', '$h{MIX}');
289
290	$h{HI} = 'Some';
291
292	is( $h{hi}, undef, '$h{hi}');
293	is( $h{Hi}, undef, '$h{Hi}');
294	is( $h{HI}, 'Some', '$h{HI}');
295
296	my @got = sort keys %h;
297	my @good = qw(HI LOW MIX);
298
299	is_deeply(\@got, \@good, "List of keys");
300
301	@got = ();
302	while (my ($key, $val) = each %h) {
303		push @got, "$key=$val";
304	}
305	@got = sort @got;
306	@good = ('HI=Some', 'LOW=value', 'MIX=other');
307	is_deeply(\@got, \@good, "each() returns both keys and values");
308
309	is( scalar( keys %h), 3, "Number of keys");
310
311	delete $h{LOW};
312	is( scalar( keys %h), 2, "Number of keys after delete");
313	is( $h{low}, undef, '$h{low}');
314	is( $h{Low}, undef, '$h{Low}');
315	is( $h{LOW}, undef, '$h{LOW}');
316
317	delete $h{Mix};
318	is( scalar( keys %h), 2, "Number of keys after delete");
319	is( $h{mix}, undef, '$h{mix}');
320	is( $h{Mix}, undef, '$h{Mix}');
321	is( $h{MIX}, 'other', '$h{MIX}');
322
323	%h = ();
324	is( scalar( keys %h), 0, "Number of keys after clear");
325	is_deeply( [keys %h], [], "There are no keys");
326
327	$h{a} = 1; $h{B} = 2;
328	is( scalar( keys %h), 2, "Number of keys after refil");
329	is_deeply( [sort keys %h], [qw(A B)], "There are two keys");
330
331}
332
333{ # sensitive
334	my %h;
335	ok(
336		tie( %h, 'Hash::WithDefaults', 'sensitive'),
337		"Tied with case=sensitive"
338	);
339
340	is( ref(tied %h), 'Hash::WithDefaults::sensitive', "Tied to the right class");
341
342	$h{low} = 'value';
343
344	is( $h{low}, 'value', '$h{low}');
345	is( $h{Low}, undef, '$h{Low}');
346	is( $h{LOW}, undef, '$h{LOW}');
347
348	$h{Mix} = 'other';
349
350	is( $h{mix}, undef, '$h{mix}');
351	is( $h{Mix}, 'other', '$h{Mix}');
352	is( $h{MIX}, undef, '$h{MIX}');
353
354	$h{HI} = 'Some';
355
356	is( $h{hi}, undef, '$h{hi}');
357	is( $h{Hi}, undef, '$h{Hi}');
358	is( $h{HI}, 'Some', '$h{HI}');
359
360	my @got = sort keys %h;
361	my @good = qw(HI Mix low);
362
363	is_deeply(\@got, \@good, "List of keys");
364
365	@got = ();
366	while (my ($key, $val) = each %h) {
367		push @got, "$key=$val";
368	}
369	@got = sort @got;
370	@good = ('HI=Some', 'Mix=other', 'low=value');
371	is_deeply(\@got, \@good, "each() returns both keys and values");
372
373	is( scalar( keys %h), 3, "Number of keys");
374
375	delete $h{low};
376	is( scalar( keys %h), 2, "Number of keys after delete");
377	is( $h{low}, undef, '$h{low}');
378	is( $h{Low}, undef, '$h{Low}');
379	is( $h{LOW}, undef, '$h{LOW}');
380
381	delete $h{MIX};
382	is( scalar( keys %h), 2, "Number of keys after delete");
383	is( $h{mix}, undef, '$h{mix}');
384	is( $h{Mix}, 'other', '$h{Mix}');
385	is( $h{MIX}, undef, '$h{MIX}');
386
387	%h = ();
388	is( scalar( keys %h), 0, "Number of keys after clear");
389	is_deeply( [keys %h], [], "There are no keys");
390
391	$h{a} = 1; $h{B} = 2;
392	is( scalar( keys %h), 2, "Number of keys after refil");
393	is_deeply( [sort keys %h], [qw(B a)], "There are two keys");
394
395}
396