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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
use strict;
use warnings;
# change 'tests => 1' to 'tests => last_test_to_print';
use Test::More qw(no_plan);
use Data::Dumper;
$Data::Dumper::Indent = 1;
sub _chomp {
my $s = shift;
chomp $s;
return $s;
}
use TM;
use TM::PSI;
sub _parse {
my $text = shift;
my $ms = new TM (baseuri => 'tm:');
my $p = new TM::LTM::Parser (store => $ms);
my $i = $p->parse ($text);
# use TM::Materialized::AsTMa;
# TM::Materialized::AsTMa::_assert_implicits ($ms, $i);
return $ms;
}
sub die_ok {
my $ltm = shift;
my $err = shift;
eval {
_parse ($ltm);
fail ("exc: expected $@");
};
chomp ($@);
my $verr = $@;
$verr =~ s/\n/\n /g; # create blanks/comments on multiline complaints
like ($@, qr/$err/, "exc: found '$verr'");
}
sub _q_players {
my $ms = shift;
my @res = $ms->match (TM->FORALL, @_);
# warn "res no filter ".Dumper \@res;
@res = grep ($_ !~ m|^tm:|, map { ref($_) ? ${$_} : $_ } map { @{$_->[TM->PLAYERS]} } $ms->match (TM->FORALL, @_));
# warn "res ".Dumper \@res;
return \@res;
}
my $warn = shift @ARGV;
unless ($warn) {
close STDERR;
open (STDERR, ">/dev/null");
select (STDERR); $| = 1;
}
#== TESTS ===========================================================================
require_ok( 'TM::Materialized::LTM' );
{
my $tm = new TM::Materialized::LTM (inline => '
');
ok ($tm->isa('TM::Materialized::Stream'), 'correct class');
ok ($tm->isa('TM::Materialized::LTM'), 'correct class');
}
{ # comments
my $ms = _parse (q|
[ aaa ]
/* some comment [ bbb ]
*/
[ ccc ]
|);
#warn Dumper $ms;
ok ($ms->tids ('aaa'), 'comment: outside');
ok (!$ms->tids ('bbb'), 'comment: inside');
ok ($ms->tids ('ccc'), 'comment: outside');
}
die_ok (q{
/* [ aaa ]
*/ */
}, 'unparseable', 'invalid comment nesting');
{ # encoding
my $ms = _parse (q|
@"utf-8"
[ aaa ]
|);
ok (1, 'encoding: ignored');
}
{ # topic address
my $ms = _parse (q|
[aaa % "urn:aaa" ]
|);
# warn Dumper $ms;
is ($ms->tids ('aaa'), $ms->tids ('urn:aaa'), 'reification: subject identifier ok');
}
{ # subject indicators
my $ms = _parse (q|
[aaa % "urn:aaa" @ "urn:xxx" @ "urn:yyy" ]
|);
# warn Dumper $ms;
ok (eq_set ($ms->midlet ($ms->tids ('urn:aaa'))->[TM->INDICATORS],
[ 'urn:xxx', 'urn:yyy' ]), 'indication: all found');
}
{ # topics types
my $ms = _parse (q|
[aaa: bbb ccc ]
|);
#warn Dumper $ms;
my @res = $ms->match (TM->FORALL, type => 'isa', irole => 'instance', iplayer => 'tm:aaa');
ok (eq_set ([ map { $_->[TM->PLAYERS]->[0] } @res ],
[ 'tm:bbb', 'tm:ccc' ]), 'topic: class values');
}
{ # topic basename
my $ms = _parse (q|
[aaa: bbb ccc = "AAA" ]
|);
#warn Dumper $ms;
ok (eq_set ([ map { $_->[0] } map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'name', iplayer => 'tm:aaa' ) ] ,
[ 'AAA' ]), 'topic: AAA basename');
}
{ # topic scoped basename
my $ms = _parse (q|
[aaa: bbb ccc = "AAAS" / sss ]
|);
#warn Dumper $ms;
ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, scope => 'tm:sss', type => 'name', iplayer => 'tm:aaa' ) ] ,
[ 'AAAS' ]), 'topic: AAA basename (scoped)');
ok (scalar $ms->match (TM->FORALL, type => 'isa', irole => 'instance', iplayer => 'tm:sss' ) == 1, 'scope isa scope');
}
{ # topic, basename, sortname
my $ms = _parse (q|
[aaa: bbb ccc = "AAA" ; "SORTAAA" ]
[xxx: yyy = "XXX"; "SORTXXX"; "DISPXXX" ]
[uuu = "UUU"; "SORTUUU"; "DISPUUU" ]
[vvv = "VVV"; "SORTVVV"; "DISPVVV" / sss ]
|);
#warn Dumper $ms;
ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'name', iplayer => 'tm:aaa' ) ] ,
[ 'AAA' ]), 'topic: AAA basename');
# ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'name', iplayer => 'tm:aaa' ) ] ,
# [ 'SORTAAA' ]), 'topic: SORTAAA basename');
}
{ # topic external occurrence (typed)
my $ms = _parse (q|
{aaa, bbb, "http://xxxt/" }
|);
#warn Dumper $ms;
ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'tm:bbb', iplayer => 'tm:aaa' ) ] ,
[ 'http://xxxt/' ]), 'topic: occurr typed');
ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'occurrence', iplayer => 'tm:aaa' ) ] ,
[ 'http://xxxt/' ]), 'topic: occurr (typed)');
}
# untyped is not allowed in LTM?
{ # topic internal occurrence
my $ms = _parse (q|
{aaa, bbb, [[http://xxxt/]] }
|);
#warn Dumper $ms;
ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'tm:bbb', iplayer => 'tm:aaa' ) ] ,
[ 'http://xxxt/' ]), 'topic: int occurr typed');
ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'occurrence', iplayer => 'tm:aaa' ) ] ,
[ 'http://xxxt/' ]), 'topic: int occurr (typed)');
}
{ # mix occurrences with topics
my $ms = _parse (q|
[ aaa : bbb ]
{ aaa, xxx, "http://xxx/" }
{ ccc, yyy, "http://yyy/" }
[ ccc : ddd ]
|);
#warn Dumper $ms;
ok (scalar $ms->match (TM->FORALL, type => 'isa', irole => 'instance', iplayer => 'tm:aaa' ) == 1, 'topic+occur: class');
ok (scalar $ms->match (TM->FORALL, type => 'isa', irole => 'instance', iplayer => 'tm:ccc' ) == 1, 'topic+occur: class');
ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'occurrence', iplayer => 'tm:aaa' ) ] ,
[ 'http://xxx/' ]), 'topic+occur: occurr');
ok (eq_set ([ map {$_->[0]} map { $_->[TM->PLAYERS]->[1] } $ms->match (TM->FORALL, type => 'occurrence', iplayer => 'tm:ccc' ) ] ,
[ 'http://yyy/' ]), 'topic+occur: occurr');
}
#-- assocs --------------
{
my $ms = _parse (q|
aaa (play1: role1, play2: role2)
bbb (play1: role1, play2: role2)
ccc (play1, play2: role2)
|);
#warn Dumper $ms;
my @res = $ms->match (TM->FORALL, type => 'tm:aaa');
ok (eq_set ([ map { @{$_->[TM->PLAYERS]} } @res ],
[ 'tm:play1', 'tm:play2' ]), 'assoc: players');
ok (eq_set ([ map { @{$_->[TM->ROLES]} } @res ],
[ 'tm:role1', 'tm:role2' ]), 'assoc: roles');
@res = $ms->match (TM->FORALL, type => 'tm:bbb');
ok (scalar @res == 1, 'assoc: separate');
@res = $ms->match (TM->FORALL, type => 'tm:ccc');
ok (eq_set ([ map { @{$_->[TM->PLAYERS]} } @res ],
[ 'tm:play1', 'tm:play2' ]), 'assoc: players');
ok (eq_set ([ map { @{$_->[TM->ROLES]} } @res ],
[ 'thing', 'tm:role2' ]), 'assoc: roles (default)');
}
{ # scoped assoc
my $ms = _parse (q|
aaa (play1: role1, play2: role2) / sss
aaa (play1: role1, play2: role2)
aaa (play1: role1, play2: role2) / ttt
|);
##warn Dumper $ms;
my @res = $ms->match (TM->FORALL, type => 'tm:aaa');
ok (scalar @res == 3, 'scoped mixed assoc: number');
ok (grep ($_->[TM->SCOPE] eq 'tm:ttt', @res), 'scoped mixed assoc: scoping');
ok (grep ($_->[TM->SCOPE] eq 'tm:sss', @res), 'scoped mixed assoc: scoping');
ok (grep ($_->[TM->SCOPE] eq 'us', @res), 'scoped mixed assoc: scoping');
foreach my $r (@res) {
ok (eq_set ([ @{$r->[TM->PLAYERS]} ],
[ 'tm:play1', 'tm:play2' ]), 'scoped mixed assoc: players');
ok (eq_set ([ @{$r->[TM->ROLES]} ],
[ 'tm:role1', 'tm:role2' ]), 'scoped mixed assoc: roles');
}
}
{ # assoc with nested topic
my $ms = _parse (q|
aaa ( [ play1: ccc ]: role1, play2: role2)
|);
#warn Dumper $ms;
my @res = $ms->match (TM->FORALL, type => 'tm:aaa');
ok (eq_set ([ map { @{$_->[TM->PLAYERS]} } @res ],
[ 'tm:play1', 'tm:play2' ]), 'assoc + embed: players');
ok (eq_set ([ map { @{$_->[TM->ROLES]} } @res ],
[ 'tm:role1', 'tm:role2' ]), 'assoc + embed: roles');
@res = $ms->match (TM->FORALL, type => 'isa', irole => 'instance', iplayer => 'tm:play1');
ok (eq_set ([ map { @{$_->[TM->PLAYERS]} } @res ],
[ 'tm:play1', 'tm:ccc' ]), 'assoc + embed: types');
}
# reifications
{ # reified assocs
my $ms = _parse (q|
aaa ( play1: role1, play2: role2) ~ xxx
|);
#warn Dumper $ms;
my ($a) = $ms->match (TM->FORALL, type => 'tm:aaa');
is ($a->[TM->LID], $ms->midlet ('tm:xxx')->[TM->ADDRESS], 'assoc reification');
}
{ # reified occurrence
my $ms = _parse (q|
{aaa, bbb, "http://xxxt/" } ~ xxx
|);
#warn Dumper $ms;
my ($a) = $ms->match (TM->FORALL, type => 'tm:bbb');
is ($a->[TM->LID], $ms->midlet ('tm:xxx')->[TM->ADDRESS], 'occurrence reification');
}
{ # reified basename
my $ms = _parse (q|
[ aaa = "AAA" ~ xxx ]
|);
#warn Dumper $ms;
my ($a) = $ms->match (TM->FORALL, type => 'name');
is ($a->[TM->LID], $ms->midlet ('tm:xxx')->[TM->ADDRESS], 'basename reification');
}
#== Directives ========================
{ # wrong VERSION format
die_ok (q|
#VERSION "123"
|, 'not supported');
}
{ # wrong VERSION
die_ok (q|
#VERSION "1.4"
|, 'not supported');
}
{ # VERSION
my $ms = _parse (q|
#VERSION "1.3"
[ aaa ]
|);
ok (1, 'version supported');
}
{ # TOPICMAP
die_ok (q|
#TOPICMAP ~ xxxx
|, 'use proper');
}
{ # INCLUDE
die_ok (q|
[aaa]
#INCLUDE "xyz:abc"
|, 'unable to load');
}
{ # INCLUDE
my $ms = _parse (q|
[ aaa ]
#INCLUDE "inline: [ bbb ]"
[ ccc ]
|);
# warn Dumper $ms;
ok ($ms->midlet ('tm:aaa'), 'include: topic');
ok ($ms->midlet ('tm:bbb'), 'include: topic');
ok ($ms->midlet ('tm:ccc'), 'include: topic');
}
{
die_ok (q|
aa:uuu (bbb:play: bbb:role)
|, 'unparseable');
}
{ # PREFIXES
my $ms = _parse (q|
#PREFIX aaa @ "http://xxxx/#"
#PREFIX bbb @ "http://yyyy/#"
aaa:uuu (play: bbb:role)
|);
# warn Dumper $ms;
my @res = $ms->match (TM->FORALL, type => $ms->tids ('http://xxxx/#uuu'));
ok (scalar @res == 1, 'prefixed assoc name: found');
ok (eq_set ([ map { @{$_->[TM->PLAYERS]} } @res ],
[ 'tm:play' ]), 'unprefixed player name');
my $id = $ms->tids ('http://yyyy/#role');
ok (eq_set ([ map { @{$_->[TM->ROLES]} } @res ],
[ $id ]), 'prefixed role name');
}
die_ok (q{
#MERGEMAP "inline: [ bbb ]" "rumsti"
}, 'unsupported', 'invalid TM format');
{ # MERGEMAP
my $ms = _parse (q|
#MERGEMAP "inline: [ bbb ]" "ltm"
[ aaa ]
[ ccc ]
|);
# warn Dumper $ms;
TODO: {
local $TODO = "merging";
ok ($ms->tids ('aaa'), 'merge: topic');
ok ($ms->tids ('bbb'), 'merge: topic');
ok ($ms->tids ('ccc'), 'merge: topic');
}
}
{ # MERGEMAP (default
my $ms = _parse (q|
#MERGEMAP "inline: [ bbb ]"
[ aaa ]
[ ccc ]
|);
# warn Dumper $ms;
TODO: {
local $TODO = "merging (default)";
ok ($ms->tids ('aaa'), 'merge: topic');
ok ($ms->tids ('bbb'), 'merge: topic');
ok ($ms->tids ('ccc'), 'merge: topic');
}
}
__END__
__END__
die_ok (q{
format-for ([ ltm ] : standard, topic-maps )
@"abssfsdf"
}, 1, 'invalid encoding');
$tm = new XTM (tie => new XTM::LTM ( text => q{
@"iso8859-1"
{ ltm , test , [[Ich chan Glaas sse, das tuet mir nd weeh]] }
}));
like ($tm->topic ('ltm')->occurrences->[0]->resource->data, qr/\x{E4}sse/, 'encoding from iso8859-1');
die_ok (q{
format-for ([ ltm ] : standard, topic-maps )
xxxx
{ ltm , test , "http://rumsti/" }
}, 1, 'unknown keyword');
die_ok (q{
format-for ([ ltm ] : standard, topic-maps
}, 1, 'missing terminator 1');
die_ok (q{
[ ltm : format <= "The linear topic map notation" @ "http://something1/" @ "http://something2/" ]
}, 1, 'invalid terminator 1');
die_ok ('
{ ltm , test , "http://rumsti/" '
, 1, 'missing terminator 2');
die_ok ('
{ ltm , test , "http://rumsti/" } abc'
, 1, 'additional nonparsable text');
$tm = new XTM (tie => new XTM::LTM ( text => q{
[ ltm ]
{ ltm , test , [[http://rumsti/
ramsti romsti ]] }
}));
is (@{$tm->topics('occurrence regexps /rumsti/')}, 1, 'occurrence with topic');
is (@{$tm->topics('occurrence regexps /romsti/')}, 1, 'occurrence with topic, multiline');
#print Dumper $tm;
$tm = new XTM (tie => new XTM::LTM ( text => q{
[ ltm ]
{ ltm , test , "http://rumsti/" }
}));
is (@{$tm->topics('occurrence regexps /rumsti/')}, 1, 'occurrence with topic');
is (@{$tm->topics()}, 2, 'occurrence with topic, 2');
#print Dumper $tm;
$tm = new XTM (tie => new XTM::LTM ( text => q{
{ ltm , test , "http://rumsti/" }
{ ltm , test2 , "http://ramsti/" }
{ ltm2, test , "http://rumsti/" }
}));
is (@{$tm->topics('occurrence regexps /rumsti/')}, 2, 'occurrence wo topic');
$tm = new XTM (tie => new XTM::LTM ( text => q{
[ ltm : format = "The linear topic map notation" @ "http://something1/" @ "http://something2/" ]
}));
is (@{$tm->topics('indicates regexps /something1/')}, 1, 'subject indication1');
is (@{$tm->topics('indicates regexps /something2/')}, 1, 'subject indication2');
$tm = new XTM (tie => new XTM::LTM ( text => q{
[ ltm : format = "The linear topic map notation" % "http://something/" ]
}));
is (@{$tm->topics('reifies regexps /something/')}, 1, 'subject reification');
#__END__
$tm = new XTM (tie => new XTM::LTM ( text => q{
[ ltm : format = "The linear topic map notation" ]
}));
is (@{$tm->topics('baseName regexps /linear/')}, 1, 'basename wo scope');
$tm = new XTM (tie => new XTM::LTM ( text => q{
[ ltm : format = "The linear topic map notation / scope1" ]
}));
is (@{$tm->topics('baseName regexps /linear/')}, 1, 'basename with scope');
#__END__
# with types
my @types = qw(format1 format2 format3);
$tm = new XTM (tie => new XTM::LTM ( text => q{
[ ltm : }.join (" ", @types).q{ ]
}));
is (@{$tm->topics()}, 4, 'topic with types');
foreach my $t (@types) {
is (@{$tm->topics("is-a $t")}, 1, "finding $t");
}
|