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
|
#!/usr/bin/perl
# Formal testing for Devel::Dumpvar
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use Test::More tests => 10;
use File::Spec::Functions qw{:ALL};
use Devel::Dumpvar ();
my $RE_MEMORY_HEX = qr/(?<=\()0x[0-9a-f]+(?=\))/i;
# Create a test for deep structure dumps, which ignores memory locations
sub dump_is {
my $got = shift;
my $expected = shift;
# Remove any hex memory locations
$got =~ s/$RE_MEMORY_HEX/0x???????/go;
$expected =~ s/$RE_MEMORY_HEX/0x???????/go;
# Compare normally now
is( $got, $expected, @_ ? shift : () );
}
# Create a dumper for testing
my $dump = Devel::Dumpvar->new( to => 'return' );
isa_ok( $dump, 'Devel::Dumpvar' );
#####################################################################
# Basic Tests
# Do the basic series of dumps
my @tests = (
[], " empty array\n",
[ 1 ], "0 1\n",
[ 'foo' ], "0 'foo'\n",
);
while ( @tests ) {
my $test = shift @tests;
my $expected = shift @tests;
is( $dump->dump( @$test ), $expected, "Simple dump matches expected" );
}
#####################################################################
# Detailed Tests
# Basic nested arrays
dump_is( $dump->dump( [ [ [] ] ] ), <<'END_DUMP', 'Array in array in array works' );
0 ARRAY(0x8301124)
0 ARRAY(0x82ed9e4)
0 ARRAY(0x804c1b4)
empty array
END_DUMP
# Dump a new dumper as a single class and hash test
dump_is( $dump->dump( Devel::Dumpvar->new( to => 'return' ) ), <<'END_DUMP', 'Dumping a dumper' );
0 Devel::Dumpvar=HASH(0x83b440c)
to => 'return'
END_DUMP
# A basic REF dump checker
dump_is( $dump->dump( \\"foo" ), <<'END_DUMP', 'Testing REF and read-only scalar' );
0 REF(0x8300f98)
-> SCALAR(0x8300fa4)
-> 'foo'
END_DUMP
# A reasonably large, combination object
my $User = bless( {
HideDescriptions => bless( do{\(my $o = 1)}, 'Foo::Data::Boolean' ),
Passwd => bless( do{\(my $o = 'phlegm1!')}, 'Foo::Data::ShortString' ),
Id => bless( do{\(my $o = '1')}, 'Foo::Data::Integer' ),
_ID => '1',
RealName => bless( do{\(my $o = 'Adam Kennedy')}, 'Foo::Data::LongString' ),
OutputPath => bless( do{\(my $o = undef)}, 'Foo::Data::LongString' ),
OutputURL => bless( do{\(my $o = undef)}, 'Foo::Data::LongString' ),
Username => bless( do{\(my $o = 'adam')}, 'Foo::Data::ShortString' ),
Created => bless( do{\(my $o = bless( [
37, 32, 4, 19, 7, 103, 2, 230, 0, 1061231557, 1
], 'Foo::Time' ))}, 'Foo::Data::DateTime' ),
Email => bless( do{\(my $o = 'adam@ali.as')}, 'Foo::Data::LongString' ),
Modified => bless( do{\(my $o = bless( [
35, 42, 17, 18, 10, 103, 2, 321, 1, 1069137755, 1
], 'Foo::Time' ))}, 'Foo::Data::DateTime' )
}, 'Foo::Entity::User' );
dump_is( $dump->dump( $User ), <<'END_DUMP', "More complex dump worked" );
0 Foo::Entity::User=HASH(0x9e82358)
Created => Foo::Data::DateTime=REF(0x9e8a0d4)
-> Foo::Time=ARRAY(0x9ee62c4)
0 37
1 32
2 4
3 19
4 7
5 103
6 2
7 230
8 0
9 1061231557
10 1
Email => Foo::Data::LongString=SCALAR(0x9e8a098)
-> 'adam@ali.as'
HideDescriptions => Foo::Data::Boolean=SCALAR(0x9e8a0a4)
-> 1
Id => Foo::Data::Integer=SCALAR(0x88a52c4)
-> 1
Modified => Foo::Data::DateTime=REF(0x9e8c3b4)
-> Foo::Time=ARRAY(0x9e8c408)
0 35
1 42
2 17
3 18
4 10
5 103
6 2
7 321
8 1
9 1069137755
10 1
OutputPath => Foo::Data::LongString=SCALAR(0x9e89ffc)
-> undef
OutputURL => Foo::Data::LongString=SCALAR(0x9e8a0b0)
-> undef
Passwd => Foo::Data::ShortString=SCALAR(0x9e8a008)
-> 'phlegm1!'
RealName => Foo::Data::LongString=SCALAR(0x9e89f60)
-> 'Adam Kennedy'
Username => Foo::Data::ShortString=SCALAR(0x9e89df8)
-> 'adam'
_ID => 1
END_DUMP
# Circular references
my $c = [ 'foo', 'bar' ];
my $d = { 'a' => 1, 'b' => 'c' };
$c->[2] = $d;
$d->{d} = $c;
dump_is( $dump->dump( $c ), <<'END_DUMP', 'Circular references work' );
0 ARRAY(0x82ed9cc)
0 'foo'
1 'bar'
2 HASH(0x804c1b4)
a => 1
b => 'c'
d => ARRAY(0x82ed9cc)
-> REUSED_ADDRESS
END_DUMP
dump_is( $dump->dump( $d ), <<'END_DUMP', 'Circular references work' );
0 HASH(0x804c1b4)
a => 1
b => 'c'
d => ARRAY(0x82ed9cc)
0 'foo'
1 'bar'
2 HASH(0x804c1b4)
-> REUSED_ADDRESS
END_DUMP
|