fix relationship test data; remove print from tab test
[scpubgit/stemmatology.git] / t / text_tradition_collation_relationshipstore.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 $| = 1;
6
7
8
9 # =begin testing
10 {
11 use Text::Tradition;
12 use TryCatch;
13
14 use_ok( 'Text::Tradition::Collation::RelationshipStore' );
15
16 # Add some relationships, and delete them
17
18 my $cxfile = 't/data/Collatex-16.xml';
19 my $t = Text::Tradition->new( 
20     'name'  => 'inline', 
21     'input' => 'CollateX',
22     'file'  => $cxfile,
23     );
24 my $c = $t->collation;
25
26 my @v1 = $c->add_relationship( 'n21', 'n22', { 'type' => 'lexical' } );
27 is( scalar @v1, 1, "Added a single relationship" );
28 is( $v1[0]->[0], 'n21', "Got correct node 1" );
29 is( $v1[0]->[1], 'n22', "Got correct node 2" );
30 my @v2 = $c->add_relationship( 'n24', 'n23', 
31         { 'type' => 'spelling', 'scope' => 'global' } );
32 is( scalar @v2, 2, "Added a global relationship with two instances" );
33 @v1 = $c->del_relationship( 'n22', 'n21' );
34 is( scalar @v1, 1, "Deleted first relationship" );
35 @v2 = $c->del_relationship( 'n12', 'n13' );
36 is( scalar @v2, 2, "Deleted second global relationship" );
37 my @v3 = $c->del_relationship( 'n1', 'n2' );
38 is( scalar @v3, 0, "Nothing deleted on non-existent relationship" );
39 }
40
41
42
43 # =begin testing
44 {
45 use Text::Tradition;
46 use TryCatch;
47
48 my $t1 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
49 # Test 1: try to equate nodes that are prevented with an intermediate collation
50 ok( $t1, "Parsed test fragment file" );
51 my $c1 = $t1->collation;
52 my $trel = $c1->get_relationship( '9,2', '9,3' );
53 is( ref( $trel ), 'Text::Tradition::Collation::Relationship',
54         "Troublesome relationship exists" );
55 is( $trel->type, 'collated', "Troublesome relationship is a collation" );
56
57 # Try to make the link we want
58 try {
59         $c1->add_relationship( '8,6', '10,3', { 'type' => 'orthographic' } );
60         ok( 1, "Added cross-collation relationship as expected" );
61 } catch {
62         ok( 0, "Existing collation blocked equivalence relationship" );
63 }
64
65 try {
66         $c1->calculate_ranks();
67         ok( 1, "Successfully calculated ranks" );
68 } catch {
69         ok( 0, "Collation now has a cycle" );
70 }
71
72 # Test 2: try to equate nodes that are prevented with a real intermediate
73 # equivalence
74 my $t2 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
75 my $c2 = $t2->collation;
76 $c2->add_relationship( '9,2', '9,3', { 'type' => 'lexical' } );
77 my $trel2 = $c2->get_relationship( '9,2', '9,3' );
78 is( ref( $trel2 ), 'Text::Tradition::Collation::Relationship',
79         "Created blocking relationship" );
80 is( $trel2->type, 'lexical', "Blocking relationship is not a collation" );
81 # This time the link ought to fail
82 try {
83         $c2->add_relationship( '8,6', '10,3', { 'type' => 'orthographic' } );
84         ok( 0, "Added cross-equivalent bad relationship" );
85 } catch {
86         ok( 1, "Existing equivalence blocked crossing relationship" );
87 }
88
89 try {
90         $c2->calculate_ranks();
91         ok( 1, "Successfully calculated ranks" );
92 } catch {
93         ok( 0, "Collation now has a cycle" );
94 }
95
96 # Test 3: make a straightforward pair of transpositions.
97 my $t3 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/lf2.xml' );
98 # Test 1: try to equate nodes that are prevented with an intermediate collation
99 my $c3 = $t3->collation;
100 try {
101         $c3->add_relationship( '36,4', '38,3', { 'type' => 'transposition' } );
102         ok( 1, "Added straightforward transposition" );
103 } catch {
104         ok( 0, "Failed to add normal transposition" );
105 }
106 try {
107         $c3->add_relationship( '36,3', '38,2', { 'type' => 'transposition' } );
108         ok( 1, "Added straightforward transposition complement" );
109 } catch {
110         ok( 0, "Failed to add normal transposition complement" );
111 }
112
113 # Test 4: try to make a transposition that could be a parallel.
114 try {
115         $c3->add_relationship( '28,2', '29,2', { 'type' => 'transposition' } );
116         ok( 0, "Added bad colocated transposition" );
117 } catch {
118         ok( 1, "Prevented bad colocated transposition" );
119 }
120
121 # Test 5: make the parallel, and then make the transposition again.
122 try {
123         $c3->add_relationship( '28,3', '29,3', { 'type' => 'orthographic' } );
124         ok( 1, "Equated identical readings for transposition" );
125 } catch {
126         ok( 0, "Failed to equate identical readings" );
127 }
128 try {
129         $c3->add_relationship( '28,2', '29,2', { 'type' => 'transposition' } );
130         ok( 1, "Added straightforward transposition complement" );
131 } catch {
132         ok( 0, "Failed to add normal transposition complement" );
133 }
134 }
135
136
137
138
139 1;