File: hash_flatten.t

package info (click to toggle)
libhash-flatten-perl 1.19-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 128 kB
  • sloc: perl: 658; makefile: 2
file content (499 lines) | stat: -rw-r--r-- 10,941 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/usr/local/bin/perl -w

###############################################################################
# Purpose : Unit test for Hash::Flatten
# Author  : John Alden
# Created : Feb 2002
# CVS     : $Header: /home/cvs/software/cvsroot/hash_flatten/t/hash_flatten.t,v 1.21 2009/05/09 12:42:02 jamiel Exp $
###############################################################################
# -t : trace
# -T : deep trace into modules
###############################################################################

use strict;
use Test::Assertions qw(test);
use Getopt::Std;
use Log::Trace;

use vars qw($opt_t $opt_T);
getopts("tT");

plan tests;

#Compile the code
chdir($1) if($0 =~ /(.*)(\/|\\)(.*)/);
unshift @INC, "./lib", "../lib";

#Override warn() first, then compile
my $buf;
{
	BEGIN {$^W = 0}
	*CORE::GLOBAL::warn = sub {$buf = shift()};
	require Hash::Flatten;
}
ASSERT($INC{'Hash/Flatten.pm'}, 'loaded');

import Log::Trace qw(print) if ($opt_t);
deep_import Log::Trace qw(print) if ($opt_T);

#############################################################
#
# Nested hashes
#
#############################################################

my $data =
{
	'x' => 1,
	'y' => {
		'a' => 2,
		'b' => {
			'p' => 3,
			'q' => 4
		},
	}
};

my $flat_data = {
	'x' => 1,
	'y.a' => 2,
	'y.b.p' => 3,
	'y.b.q' => 4
};

my $flat = Hash::Flatten::flatten($data);
DUMP($flat);
ASSERT EQUAL($flat, $flat_data), 'nested hashes';

my $unflat = Hash::Flatten::unflatten($flat);
DUMP($unflat);
ASSERT EQUAL($unflat, $data), 'nested hashes unflattened';

#############################################################
#
# Nested hashes with weird values
#
#############################################################

my $data =
{
	'x' => 1,
	'0' => {
		'1' => 2,
		'' => {
			'' => 3,
			'q' => 4
		},
	},
	'a' => [1,2,3],
	'' => [4,5,6],
};

my $flat_data = {
	'x' => 1,
	'0.1' => 2,
	'0..' => 3,
	'0..q' => 4,
	'a:0' => 1,
	'a:1' => 2,
	'a:2' => 3,
	':0' => 4,
	':1' => 5,
	':2' => 6,
};

my $flat = Hash::Flatten::flatten($data);
DUMP($flat);
ASSERT EQUAL($flat, $flat_data), 'nested hashes with weird values';

my $unflat = Hash::Flatten::unflatten($flat);
DUMP($unflat);
ASSERT EQUAL($unflat, $data), 'nested hashes with weird values unflattened';

#############################################################
#
# Mixed hashes/arrays
#
#############################################################

my $foo = 'hello';
$data =
{
	'x' => 1,
	'ay' => {
		'a' => 2,
		'b' => {
			'p' => 3,
			'q' => 4
		},
	},
	's' => \\\$foo,
	'y' => [
		'a', 2,
		{
			'baz' => 'bum',
		},
	]
};

$flat_data = {
	'ay*b*p' => 3,
	'ay*b*q' => 4,
	's' => 'hello',
	'ay*a' => 2,
	'y%2*baz' => 'bum',
	'x' => 1,
	'y%0' => 'a',
	'y%1' => 2
};

$flat = Hash::Flatten::flatten($data, {'HashDelimiter' => '*', 'ArrayDelimiter' => '%'});
DUMP($flat);
ASSERT EQUAL($flat, $flat_data), 'heterogeneous structure';

$unflat = Hash::Flatten::unflatten($flat, {'HashDelimiter' => '*', 'ArrayDelimiter' => '%'});
DUMP($unflat);
ASSERT EQUAL($unflat,
	{ ### NB we can't compare to $data here because we flatten out scalar refs
		'x' => 1,
		'y' => [
			'a',
			2,
			{
				'baz' => 'bum'
			}
		],
		'ay' => {
			'a' => 2,
			'b' => {
				 'p' => 3,
				 'q' => 4
			}
		},
		's' => 'hello'
	}
), 'heterogeneous structure unflattened';

#############################################################
#
# Deeply nested arrays
#
#############################################################

$data =
{
	'x' => 1,
	'y' => [
		[
			'a', 'fool', 'is',
		],
		[
			'easily', [ 'parted', 'from' ], 'his'
		],
		'money',
	]
};

$flat_data = {
	'y:1:2' => 'his',
	'x' => 1,
	'y:1:1:0' => 'parted',
	'y:1:1:1' => 'from',
	'y:2' => 'money',
	'y:0:0' => 'a',
	'y:1:0' => 'easily',
	'y:0:1' => 'fool',
	'y:0:2' => 'is'
};

$flat = Hash::Flatten::flatten($data);
DUMP($flat);
ASSERT EQUAL($flat, $flat_data), 'nested arrays';

$unflat = Hash::Flatten::unflatten($flat);
DUMP($unflat);
ASSERT EQUAL($unflat, $data), 'nested arrays unflattened';

#############################################################
#
# Trivial cases
#
#############################################################

$data = {
};

$flat_data = {
};

$flat = Hash::Flatten::flatten($data);
DUMP($flat);
ASSERT EQUAL($flat, $flat_data), 'empty hash';

$unflat = Hash::Flatten::unflatten($flat);
DUMP($unflat);
ASSERT EQUAL($unflat, $data), 'empty hash unflattened';

$data = {
	'x' => 1,
};

$flat_data = {
	'x' => 1,
};

$flat = Hash::Flatten::flatten($data);
DUMP($flat);
ASSERT EQUAL($flat, $flat_data), '1 key';

$unflat = Hash::Flatten::unflatten($flat);
DUMP($unflat);
ASSERT EQUAL($unflat, $data), '1 key unflattened';

#############################################################
#
# Very long delimiters
#
###########################################################

$data =
{
	'x' => 1,
	'ay' => {
		'a' => 2,
		'b' => {
			'p' => 3,
			'q' => 4
		},
	},
	's' => 'hey',
	'y' => [
		'a', 2,	{
			'baz' => 'bum',
		},
	]
};

$flat_data = {
	'x' => 1,
	's' => 'hey',
	'ay*Issa Hash*a' => 2,
	'y%This Is an Array!!%%2*Issa Hash*baz' => 'bum',
	'ay*Issa Hash*b*Issa Hash*p' => 3,
	'y%This Is an Array!!%%0' => 'a',
	'ay*Issa Hash*b*Issa Hash*q' => 4,
	'y%This Is an Array!!%%1' => 2
};

$flat = Hash::Flatten::flatten($data, {'HashDelimiter' => '*Issa Hash*', 'ArrayDelimiter' => '%This Is an Array!!%%'});
DUMP($flat);
ASSERT EQUAL($flat, $flat_data), 'long delimiters';

$unflat = Hash::Flatten::unflatten($flat, {'HashDelimiter' => '*Issa Hash*', 'ArrayDelimiter' => '%This Is an Array!!%%'});
DUMP($unflat);
ASSERT EQUAL($unflat, $data), 'long delimiters unflattened';

###########################################################
#
# Scalar refs, blessed refs etc
#
###########################################################

my $scal = 'scalar';
my $again = 'again!';
$data = bless({
	'x' => bless({'foo'=>'bar'}, 'Foo::Hash'),
	'y' => bless(['f', 'g'], 'Bar::Array'),
	'z' => bless(\$scal, 'Qux:Scalar'),
	'r' => bless(\\\\\$again, 'Qux Ref'),
	'rina' => [\$scal, \\$again],
	'gref' => \*FH,
}, 'Template');

DUMP($data);
$flat_data = {
	'z' => 'scalar',
	'r' => 'again!',
	'x.foo' => 'bar',
	'y:0' => 'f',
	'y:1' => 'g',
	'rina:0' => 'scalar',
	'rina:1' => 'again!',
	'gref' => \*FH,
};

$flat = Hash::Flatten::flatten($data);
DUMP($flat);
ASSERT EQUAL($flat, $flat_data), 'blessed references';

$unflat = Hash::Flatten::unflatten($flat);
DUMP($unflat);
ASSERT EQUAL($unflat, {
	'x' => {
		'foo' => 'bar'
	},
	'y' => [
		'f',
		'g'
	],
	'r' => 'again!',
	'z' => 'scalar',
	'rina' => ['scalar', 'again!'],
	'gref' => \*FH,
}), 'objects and blessed refs unflattened';

###########################################################
#
# OO Interface and callbacks
#
###########################################################

my $counter = 0;
my $o = new Hash::Flatten({
	'OnRefRef' => sub {
		my $v = shift;
		$counter++;
		return $$v; #follow	
	},
	'OnRefScalar' => sub {
		my $v = shift;
		$counter--;
		return $$v; #follow	
	},
	'OnRefGlob' => sub {
		my $v = shift;
		$counter--;
		return "A-GLOB";
	}
});

# Test coderef for handling refs
$flat = $o->flatten({a => \\\\\"x"});
DUMP($flat);
ASSERT($counter == 3, "coderef called $counter times");
$flat = $o->flatten({a => \*FH});
DUMP($flat);
ASSERT($counter == 2 && $flat->{a} eq 'A-GLOB', "globref callback");

###########################################################
#
# Escaping
#
###########################################################

my $orig = {
	a => ['1.1', '1.2', '2.1'],
	'b:c' => {e => '3.1'}	
};
$flat = Hash::Flatten::flatten($orig);
DUMP($flat);
$unflat = Hash::Flatten::unflatten($flat);
DUMP($unflat, $orig);
ASSERT(EQUAL($orig, $unflat), "escaping");

$orig = {'a' => {'A[ESC]B' => 'c[ESC]', 'C.D' => 'd:e'}};
$flat = Hash::Flatten::flatten($orig, {EscapeSequence => '[ESC]'});
DUMP($flat);
$unflat = Hash::Flatten::unflatten($flat, {EscapeSequence => '[ESC]'});
DUMP($unflat, $orig);
ASSERT(EQUAL($orig, $unflat), "custom escape seq");

###########################################################
#
# Error checking
#
###########################################################

ASSERT(DIED( sub{ Hash::Flatten::flatten([1,2,3]) } ) && scalar $@ =~ /1st arg must be a hashref/, "type check in flatten");
ASSERT(DIED( sub{ Hash::Flatten::unflatten([1,2,3]) } ) && scalar $@ =~ /1st arg must be a hashref/, "type check in unflatten");

ASSERT(
	DIED( sub{ Hash::Flatten::flatten({}, {EscapeSequence => '.'}) })
	&& scalar $@ =~ /Hash delimiter cannot contain escape sequence/
, "check hash delim for esc seq");

ASSERT(
	DIED( sub{ Hash::Flatten::flatten({}, {EscapeSequence => ':'}) })
	&& scalar $@ =~ /Array delimiter cannot contain escape sequence/
, "check array delim for esc seq");

$data = {
		'y' => {
			'a' => 2,
			'b' => 3
		},
};
$data->{'y'}->{'c'} = $data;
DUMP($data);
ASSERT( DIED( sub { Hash::Flatten::flatten( $data ) } ), 'recursive data structure detected in hashref');

$data = {
		'y' => {
			'a' => 2,
			'b' => 3
		},
};
$data->{y}->{c} = \$data;
DUMP($data);
ASSERT( DIED( sub { Hash::Flatten::flatten( $data ) } ), "recursive data structure detected in ref-ref");

$data = {
		'y' => {
			'a' => 2,
			'b' => 3
		},
};
$data->{'y'}->{'c'} = [1];
push @{$data->{'y'}->{'c'}}, $data->{'y'}->{'c'};
DUMP($data);
ASSERT( DIED( sub { Hash::Flatten::flatten( $data ) } ), "recursive data structure detected in arrayref");

ASSERT(
	DIED( sub{ Hash::Flatten::flatten({a => \[1,2]}, {OnRefRef => "die"}) })
	&& scalar $@ =~ /is a REF/
, "check ref to ref raises exception");

ASSERT(
	DIED( sub{ Hash::Flatten::flatten({a => \"x"}, {OnRefScalar => "die"}) })
	&& scalar $@ =~ /is a SCALAR/
, "check ref to scalar raises exception");

ASSERT(
	DIED( sub{ Hash::Flatten::flatten({a => \*FH}, {OnRefGlob => "die"}) })
	&& scalar $@ =~ /is a GLOB/
, "check ref to glob raises exception");

my $rv = Hash::Flatten::flatten({a => \[1,2]}, {OnRefRef => "warn"});
DUMP($rv);
TRACE($buf);
ASSERT(scalar $buf =~ /is a REF and will be followed/ && EQUAL($rv, {
	'a:0' => 1,
	'a:1' => 2
}), "warn mode works as expected");

$rv = Hash::Flatten::flatten({a=>"m:o.o", "o:i.n:k" => {a=>1}},{EscapeSequence => "#", DisableEscapes => 0});
DUMP($rv);
ASSERT(
	EQUAL($rv,{a => 'm:o.o','o#:i#.n#:k.a' => 1}),
	"Escapes on, returned escaped hash"
);    
$rv = Hash::Flatten::unflatten({a => 'm:o.o','o#:i#.n#:k.a' => 1},{EscapeSequence => "#", DisableEscapes => 0});
DUMP($rv);
ASSERT(
	EQUAL($rv,{a=>"m:o.o", "o:i.n:k" => {a=>1}}),
	"Escapes on, unescaped hash correctly"
);    

$rv = Hash::Flatten::flatten({a=>"m:o.o", "o:i.n:k" => {a=>1}},{EscapeSequence => "#", DisableEscapes => 1});
DUMP($rv);
ASSERT(
	EQUAL($rv,{a => 'm:o.o','o:i.n:k.a' => 1}),
	"Escapes off, returned nonsense"
);    
$rv = Hash::Flatten::unflatten({a => 'm:o.o','o#:i#.n#:k.a' => 1},{EscapeSequence => "#", DisableEscapes => 1});
DUMP($rv);
ASSERT(
	EQUAL($rv,{a => 'm:o.o','o#' => [{'n#' => [{a => 1}]}]}),
	"Escapes off, didn't unescape hash"
);