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
|
#!/usr/bin/perl -w
use strict;
use Test::More tests => 20;
use_ok("Tangram::Dump");
use Data::Dumper;
use lib "t/springfield";
use Springfield;
use Set::Object qw(is_overloaded blessed);
use Tangram::Type::Dump qw(flatten unflatten);
my $homer_id;
{
my $storage = Springfield::connect_empty();
my $homer =
NaturalPerson->new( firstName => 'Homer',
name => 'Simpson',
"ih_opinions" =>
{ work => Opinion->new(statement => 'bad'),
food => Opinion->new(statement => 'good'),
beer => Opinion->new(statement => 'better') },
);
my $marge = NaturalPerson->new( firstName => 'Marge',
name => 'Simpson' );
$homer->{partner} = $marge;
$marge->{partner} = $homer;
$homer_id = $storage->insert($homer);
# now, make a data structure...
my $structure = {
hello => $homer,
#foo => "bar",
#baz => [ qw(frop quux), $homer ],
#cheese => \\$marge,
#bananas => Set::Object->new($homer, $marge),
};
flatten($storage, $structure);
is(ref $structure->{hello}, "Tangram::Memento",
"blessed object removed - 1");
unflatten($storage, $structure);
is($structure->{hello}, $homer, "unflatten - 1");
$structure = {
hello => $homer,
foo => "bar",
baz => [ qw(frop quux), $homer ],
#cheese => \\$marge,
#bananas => Set::Object->new($homer, $marge),
};
flatten($storage, $structure);
is(ref $structure->{hello}, "Tangram::Memento",
"blessed object removed - 2a");
is(ref $structure->{baz}->[2], "Tangram::Memento",
"blessed object removed - 2b");
unflatten($storage, $structure);
is($structure->{hello}, $homer, "unflatten - 2a");
is($structure->{baz}->[2], $homer, "unflatten - 2b");
$structure = {
hello => $homer,
foo => "bar",
baz => [ qw(frop quux), $homer ],
cheese => \\$marge,
};
flatten($storage, $structure);
is(ref $structure->{hello}, "Tangram::Memento",
"blessed object removed - 3a");
is(ref $structure->{baz}->[2], "Tangram::Memento",
"blessed object removed - 3b");
is(ref ${${$structure->{cheese}}}, "Tangram::Memento",
"blessed object removed - 3c");
unflatten($storage, $structure);
is($structure->{hello}, $homer, "unflatten - 3a");
is($structure->{baz}->[2], $homer, "unflatten - 3b");
is(${${$structure->{cheese}}}, $marge, "unflatten - 3c");
$structure = {
hello => $homer,
foo => "bar",
baz => [ qw(frop quux), $homer ],
cheese => \\$marge,
bananas => Set::Object->new($homer, $marge),
};
flatten($storage, $structure);
isnt(ref $structure->{bananas}, "Set::Object",
"Set::Object's replaced");
###my $x = dispel_overload($structure->{bananas});
#isnt($x, 1, "no AMAGIC bits leaked");
unflatten($storage, $structure);
is(ref $structure->{bananas}, "Set::Object",
"unflatten Set::Object (container)");
is($structure->{bananas}->size, 2,
"unflatten Set::Object (contents 1)");
is_deeply([ sort { $a->{firstName} cmp $b->{firstName} }
$structure->{bananas}->members ],
[ $homer, $marge ],
"unflatten Set::Object (contents 2)");
$Data::Dumper::Indent = 1;
#%$structure = ();
delete $homer->{partner};
}
is(leaked, 0, "leaktest");
# now test putting it in the database...
{
my $storage = Springfield::connect;
my $homer = $storage->load($homer_id);
$homer->{brains} = {
me => $homer,
marge => $homer->{partner},
#beer => "good",
#beer_from => "fridge",
#beer_fetched_by => \\$homer->{partner},
#family => Set::Object->new($homer,
#$homer->{partner})
};
$storage->update($homer);
delete $homer->{partner};
delete $homer->{brains};
}
is(leaked, 0, "leaktest");
{
my $storage = Springfield::connect;
my $homer = $storage->load($homer_id);
my $marge = $homer->{partner};
is($homer->{brains}->{marge}, $marge,
"PerlDump can store Tangram objects!");
}
|