File: overload.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 (69 lines) | stat: -rw-r--r-- 1,962 bytes parent folder | download | duplicates (4)
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
#!/usr/local/bin/perl -w

###############################################################################
# Purpose : Unit test for Hash::Flatten with overload
# Author  : John Alden based on a bug report from Marcel Grnauer
# Created : Oct 2005
# CVS     : $Header: /home/cvs/software/cvsroot/hash_flatten/t/overload.t,v 1.3 2006/04/11 13:43:30 mattheww 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";
require Hash::Flatten;

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

my $expected = {
	'x.bar.value' => 1,
	'x.baz.value' => 2
};

#Package without overload
package PkgSansOverload;
$PkgSansOverload::Counter=0;
sub new { bless { value => ++$PkgSansOverload::Counter }, shift }

#Package with overloaded string (if overload is available)
package PkgWithOverload;
$PkgWithOverload::Counter=0;
eval {
	require overload;
	import overload '""' => sub { 'blah' };
};
sub new { bless { value => ++$PkgWithOverload::Counter }, shift }

#########################################
# The tests
#########################################

package main;

my $data = {'x' => {}};
$data->{'x'}{'bar'} = PkgSansOverload->new;
$data->{'x'}{'baz'} = PkgSansOverload->new;
my $flat = Hash::Flatten::flatten($data);
DUMP($flat);
ASSERT(EQUAL($flat, $expected), "expected value without overload");

$data = {'x' => {}};
$data->{'x'}{'bar'} = PkgWithOverload->new;
$data->{'x'}{'baz'} = PkgWithOverload->new;
$flat = Hash::Flatten::flatten($data);
DUMP($flat);
ASSERT(EQUAL($flat, $expected), "same value with overloaded stringify");