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
|
# -*-perl-*-
use strict;
use Data::Dumper;
use Test::More tests => 59;
require_ok( 'XTM::Memory');
{
use XTM::AsTMa;
my $tm = new XTM (tie => new XTM::AsTMa (auto_complete => 0, text => '
aaa (bbb ccc)
bbb (ccc ddd)
ccc (eee)
ddd (eee)
'));
my $tree = $tm->induced_assoc_tree (topic => 'aaa',
assoc_type => $XTM::PSI::xtm{'class-instance'},
a_role => $XTM::PSI::xtm{'instance'},
depth => undef);
# print Dumper $tree;
is ($tree->{tid}, 'aaa', 'found root');
is ($tree->{children}->[0]->{tid}, 'bbb', 'bbb');
is ($tree->{children}->[1], $tree->{children}->[0]->{children}->[0], 'ccc clone');
is ($tree->{children}->[0]->{children}->[0]->{tid}, 'ccc', 'ccc');
}
##__END__
{
use XTM::AsTMa;
my $tm = new XTM (tie => new XTM::AsTMa (auto_complete => 0, text => '
aaa (bbb)
bbb (ccc ddd)
xxx (aaa)
yyy (xxx zzz)
vvv (xxx)
uuu (yyy)
'));
my @uptests = ([ 'aaa', 4, undef ],
[ 'xxx', 3, undef ],
[ 'yyy', 1, undef ],
[ 'aaa', 1, 1 ],
[ 'aaa', 3, 2 ],
[ 'aaa', 4, 100 ],
[ 'xxx', 3, undef ],
[ 'yyy', 1, undef ],
);
foreach my $test (@uptests) {
is ( scalar @{$tm->induced_assoc_tree (topic => $test->[0],
assoc_type => $XTM::PSI::xtm{'class-instance'},
a_role => $XTM::PSI::xtm{'class'},
$test->[2] ? ( depth => $test->[2]) : ())
->{'children*'}},
$test->[1],
"children of $test->[0] : $test->[1] (depth ".($test->[2] || '-').")");
}
my @downtests = (
[ 'aaa', 3, undef ],
[ 'aaa', 1, 1 ],
[ 'bbb', 2, 1 ],
[ 'ccc', 0, undef ],
);
foreach my $test (@downtests) {
is ( scalar @{$tm->induced_assoc_tree (topic => $test->[0],
assoc_type => $XTM::PSI::xtm{'class-instance'},
a_role => $XTM::PSI::xtm{'instance'},
$test->[2] ? ( depth => $test->[2]) : ())
->{'children*'}},
$test->[1],
"children of $test->[0] : $test->[1] (depth ".($test->[2] || '-').")");
}
}
##__END__
my $tm = new XTM::Memory;
is (@{$tm->topics()}, 0, 'tabula rasa'); # do not want any topic here
ok (! $tm->is_topic('123'), 'no topic 123');
my $t1 = new XTM::topic (id => '123');
$tm->add ($t1);
is (@{$tm->topics()}, 1, 'have one'); # now we want one
is (@{$tm->topics( 'id regexps /^123$/' )}, 1, 'and the one we had added');
is (@{$tm->topics( 'id regexps /^124$/' )}, 0, 'but no other');
my $t2 = new XTM::topic (id => '124');
$tm->add ($t1, $t2);
is (@{$tm->topics()}, 2, 'we have 2'); # now we want two
is (@{$tm->topics( 'id regexps /^123$/' )}, 1, 'found 123');
is (@{$tm->topics( 'id regexps /^124$/' )}, 1, 'found 124');
my $t3 = new XTM::topic (id => '125');
$tm->add ($t3, $t2);
is (@{$tm->topics( 'id regexps /^12/' )}, 3, 'one more');
foreach my $id ( @{$tm->topics ()}) {
ok ($tm->is_topic($id));
}
ok ($tm->topic ('123'), 'got topic handle');
is ((eval { $tm->topic ('asksdfasdlkj'); }, $@ ? 0 : 1), 0, 'raised exception');
is (@{$tm->associations()}, 0, 'still no assocs');
my $a1 = new XTM::association (id => '999');
my $a2 = new XTM::association (id => '998');
my $a3 = new XTM::association ();
$tm->add ($a1);
is (@{$tm->topics('id regexps /^12/')}, 3, 'found added topics');
is (@{$tm->associations()}, 1, 'found added assoc');
$tm->add ($a2, $a3);
is (@{$tm->associations()}, 3, 'found more assocs');
is (@{$tm->associations('id regexps /^99/')}, 2, 'found specific assocs');
foreach my $id ( @{$tm->associations ()}) {
ok ($tm->is_association($id));
}
ok ($tm->association ('999'));
is ((eval { $tm->association ('asksdfasdlkj'); }, $@ ? 0 : 1), 0, 'unknown raised exception');
# adding a map
my $tm2 = new XTM::Memory;
$tm2->add (new XTM::topic (id => '222'));
$tm2->add (new XTM::association (id => '888'));
$tm->add ($tm2);
is (@{$tm->topics()}, 4, 'got all topics');
is (@{$tm->associations()}, 4, 'got all assocs');
# removing
foreach my $id ( @{$tm->topics()} ) {
my $doomed = $tm->remove ($id);
ok ($doomed->[0]->id eq $id);
ok (!$tm->is_topic($id));
}
foreach my $id ( @{$tm->associations ()}) {
my $doomed = $tm->remove ($id);
ok ($doomed->[0]->id eq $id);
ok (!$tm->is_association($id));
}
__END__
exit;
print Dumper $tm;
|