improvements to cat / feature listing
[scpubgit/stemmatology.git] / t / text_tradition_collation_relationshipstore.t
CommitLineData
b0b4421a 1#!/usr/bin/perl -w
2
3use strict;
4use Test::More 'no_plan';
5$| = 1;
6
7
8
9# =begin testing
10{
11use Text::Tradition;
ee801e17 12use TryCatch;
b0b4421a 13
14use_ok( 'Text::Tradition::Collation::RelationshipStore' );
ee801e17 15
16# Add some relationships, and delete them
17
18my $cxfile = 't/data/Collatex-16.xml';
19my $t = Text::Tradition->new(
20 'name' => 'inline',
21 'input' => 'CollateX',
22 'file' => $cxfile,
23 );
24my $c = $t->collation;
25
f8331a4d 26my @v1 = $c->add_relationship( 'n21', 'n22', { 'type' => 'lexical' } );
ee801e17 27is( scalar @v1, 1, "Added a single relationship" );
28is( $v1[0]->[0], 'n21', "Got correct node 1" );
29is( $v1[0]->[1], 'n22', "Got correct node 2" );
679f17e1 30my @v2 = $c->add_relationship( 'n24', 'n23',
ee801e17 31 { 'type' => 'spelling', 'scope' => 'global' } );
32is( scalar @v2, 2, "Added a global relationship with two instances" );
33@v1 = $c->del_relationship( 'n22', 'n21' );
34is( scalar @v1, 1, "Deleted first relationship" );
679f17e1 35@v2 = $c->del_relationship( 'n12', 'n13' );
ee801e17 36is( scalar @v2, 2, "Deleted second global relationship" );
681893aa 37my @v3 = $c->del_relationship( 'n1', 'n2' );
38is( scalar @v3, 0, "Nothing deleted on non-existent relationship" );
b0b4421a 39}
40
41
42
6d381462 43# =begin testing
44{
45use Text::Tradition;
46use TryCatch;
47
48my $t1 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
176badfe 49# Test 1.1: try to equate nodes that are prevented with an intermediate collation
6d381462 50ok( $t1, "Parsed test fragment file" );
51my $c1 = $t1->collation;
10e4b1ac 52my $trel = $c1->get_relationship( 'r9.2', 'r9.3' );
6d381462 53is( ref( $trel ), 'Text::Tradition::Collation::Relationship',
54 "Troublesome relationship exists" );
55is( $trel->type, 'collated', "Troublesome relationship is a collation" );
56
57# Try to make the link we want
58try {
10e4b1ac 59 $c1->add_relationship( 'r8.6', 'r10.3', { 'type' => 'orthographic' } );
6d381462 60 ok( 1, "Added cross-collation relationship as expected" );
176badfe 61} catch( Text::Tradition::Error $e ) {
62 ok( 0, "Existing collation blocked equivalence relationship: " . $e->message );
6d381462 63}
64
65try {
66 $c1->calculate_ranks();
67 ok( 1, "Successfully calculated ranks" );
176badfe 68} catch ( Text::Tradition::Error $e ) {
69 ok( 0, "Collation now has a cycle: " . $e->message );
6d381462 70}
71
176badfe 72# Test 1.2: attempt merge of an identical reading
359944f7 73try {
10e4b1ac 74 $c1->merge_readings( 'r9.3', 'r11.5' );
359944f7 75 ok( 1, "Successfully merged reading 'pontifex'" );
76} catch ( Text::Tradition::Error $e ) {
77 ok( 0, "Merge of mergeable readings failed: $e->message" );
78
79}
80
176badfe 81# Test 1.3: attempt relationship with a meta reading (should fail)
82try {
10e4b1ac 83 $c1->add_relationship( 'r8.1', 'r9.2', { 'type' => 'collated' } );
176badfe 84 ok( 0, "Allowed a meta-reading to be used in a relationship" );
85} catch ( Text::Tradition::Error $e ) {
86 is( $e->message, 'Cannot set relationship on a meta reading',
87 "Relationship link prevented for a meta reading" );
88}
89
90# Test 2.1: try to equate nodes that are prevented with a real intermediate
6d381462 91# equivalence
6d381462 92my $t2 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
6d381462 93my $c2 = $t2->collation;
10e4b1ac 94$c2->add_relationship( 'r9.2', 'r9.3', { 'type' => 'lexical' } );
95my $trel2 = $c2->get_relationship( 'r9.2', 'r9.3' );
6d381462 96is( ref( $trel2 ), 'Text::Tradition::Collation::Relationship',
97 "Created blocking relationship" );
98is( $trel2->type, 'lexical', "Blocking relationship is not a collation" );
99# This time the link ought to fail
100try {
10e4b1ac 101 $c2->add_relationship( 'r8.6', 'r10.3', { 'type' => 'orthographic' } );
cc31ebaa 102 ok( 0, "Added cross-equivalent bad relationship" );
176badfe 103} catch ( Text::Tradition::Error $e ) {
104 like( $e->message, qr/witness loop/,
105 "Existing equivalence blocked crossing relationship" );
6d381462 106}
107
108try {
109 $c2->calculate_ranks();
110 ok( 1, "Successfully calculated ranks" );
176badfe 111} catch ( Text::Tradition::Error $e ) {
112 ok( 0, "Collation now has a cycle: " . $e->message );
6d381462 113}
cc31ebaa 114
176badfe 115# Test 3.1: make a straightforward pair of transpositions.
cc31ebaa 116my $t3 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/lf2.xml' );
117# Test 1: try to equate nodes that are prevented with an intermediate collation
118my $c3 = $t3->collation;
119try {
10e4b1ac 120 $c3->add_relationship( 'r36.4', 'r38.3', { 'type' => 'transposition' } );
cc31ebaa 121 ok( 1, "Added straightforward transposition" );
176badfe 122} catch ( Text::Tradition::Error $e ) {
123 ok( 0, "Failed to add normal transposition: " . $e->message );
cc31ebaa 124}
125try {
10e4b1ac 126 $c3->add_relationship( 'r36.3', 'r38.2', { 'type' => 'transposition' } );
cc31ebaa 127 ok( 1, "Added straightforward transposition complement" );
176badfe 128} catch ( Text::Tradition::Error $e ) {
129 ok( 0, "Failed to add normal transposition complement: " . $e->message );
cc31ebaa 130}
131
176badfe 132# Test 3.2: try to make a transposition that could be a parallel.
cc31ebaa 133try {
10e4b1ac 134 $c3->add_relationship( 'r28.2', 'r29.2', { 'type' => 'transposition' } );
cc31ebaa 135 ok( 0, "Added bad colocated transposition" );
176badfe 136} catch ( Text::Tradition::Error $e ) {
137 like( $e->message, qr/Readings appear to be colocated/,
138 "Prevented bad colocated transposition" );
cc31ebaa 139}
140
176badfe 141# Test 3.3: make the parallel, and then make the transposition again.
cc31ebaa 142try {
10e4b1ac 143 $c3->add_relationship( 'r28.3', 'r29.3', { 'type' => 'orthographic' } );
cc31ebaa 144 ok( 1, "Equated identical readings for transposition" );
176badfe 145} catch ( Text::Tradition::Error $e ) {
146 ok( 0, "Failed to equate identical readings: " . $e->message );
cc31ebaa 147}
148try {
10e4b1ac 149 $c3->add_relationship( 'r28.2', 'r29.2', { 'type' => 'transposition' } );
cc31ebaa 150 ok( 1, "Added straightforward transposition complement" );
176badfe 151} catch ( Text::Tradition::Error $e ) {
152 ok( 0, "Failed to add normal transposition complement: " . $e->message );
cc31ebaa 153}
6d381462 154}
155
156
157
b0b4421a 158
1591;