return the new reading generated on duplication
[scpubgit/stemmatology.git] / base / t / text_tradition_collation.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
13 my $cxfile = 't/data/Collatex-16.xml';
14 my $t = Text::Tradition->new( 
15     'name'  => 'inline', 
16     'input' => 'CollateX',
17     'file'  => $cxfile,
18     );
19 my $c = $t->collation;
20
21 my $rno = scalar $c->readings;
22 # Split n21 for testing purposes
23 my $new_r = $c->add_reading( { 'id' => 'n21p0', 'text' => 'un', 'join_next' => 1 } );
24 my $old_r = $c->reading( 'n21' );
25 $old_r->alter_text( 'to' );
26 $c->del_path( 'n20', 'n21', 'A' );
27 $c->add_path( 'n20', 'n21p0', 'A' );
28 $c->add_path( 'n21p0', 'n21', 'A' );
29 $c->flatten_ranks();
30 ok( $c->reading( 'n21p0' ), "New reading exists" );
31 is( scalar $c->readings, $rno, "Reading add offset by flatten_ranks" );
32
33 # Combine n3 and n4 ( with his )
34 $c->merge_readings( 'n3', 'n4', 1 );
35 ok( !$c->reading('n4'), "Reading n4 is gone" );
36 is( $c->reading('n3')->text, 'with his', "Reading n3 has both words" );
37
38 # Collapse n9 and n10 ( rood / root )
39 $c->merge_readings( 'n9', 'n10' );
40 ok( !$c->reading('n10'), "Reading n10 is gone" );
41 is( $c->reading('n9')->text, 'rood', "Reading n9 has an unchanged word" );
42
43 # Combine n21 and n21p0
44 my $remaining = $c->reading('n21');
45 $remaining ||= $c->reading('n22');  # one of these should still exist
46 $c->merge_readings( 'n21p0', $remaining, 1 );
47 ok( !$c->reading('n21'), "Reading $remaining is gone" );
48 is( $c->reading('n21p0')->text, 'unto', "Reading n21p0 merged correctly" );
49 }
50
51
52
53 # =begin testing
54 {
55 use Text::Tradition;
56
57 my $st = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/collatecorr.xml' );
58 is( ref( $st ), 'Text::Tradition', "Got a tradition from test file" );
59 ok( $st->has_witness('Ba96'), "Tradition has the affected witness" );
60
61 my $sc = $st->collation;
62 my $numr = 17;
63 ok( $sc->reading('n131'), "Tradition has the affected reading" );
64 is( scalar( $sc->readings ), $numr, "There are $numr readings in the graph" );
65 is( $sc->end->rank, 14, "There are fourteen ranks in the graph" );
66
67 # Detach the erroneously collated reading
68 my $newr = $sc->duplicate_reading( 'n131', 'Ba96' );
69 ok( $newr, "New reading was created" );
70 ok( $sc->reading('n131_0'), "Detached the bad collation with a new reading" );
71 is( scalar( $sc->readings ), $numr + 1, "A reading was added to the graph" );
72 is( $sc->end->rank, 10, "There are now only ten ranks in the graph" );
73
74 # Check that the bad transposition is gone
75 is( $sc->get_relationship( 'n130', 'n135' ), undef, "Bad transposition relationship is gone" );
76
77 # Fix the collation
78 ok( $sc->add_relationship( 'n124', 'n131_0', { type => 'collated', scope => 'local' } ),
79         "Collated the readings correctly" );
80 $sc->calculate_ranks();
81 $sc->flatten_ranks();
82 is( $sc->end->rank, 11, "The ranks shifted appropriately" );
83 is( scalar( $sc->readings ), $numr - 3, "Now we are collated correctly" );
84 }
85
86
87
88 # =begin testing
89 {
90 use Text::Tradition;
91 use TryCatch;
92
93 my $READINGS = 311;
94 my $PATHS = 361;
95
96 my $datafile = 't/data/florilegium_tei_ps.xml';
97 my $tradition = Text::Tradition->new( 'input' => 'TEI',
98                                       'name' => 'test0',
99                                       'file' => $datafile,
100                                       'linear' => 1 );
101
102 ok( $tradition, "Got a tradition object" );
103 is( scalar $tradition->witnesses, 13, "Found all witnesses" );
104 ok( $tradition->collation, "Tradition has a collation" );
105
106 my $c = $tradition->collation;
107 is( scalar $c->readings, $READINGS, "Collation has all readings" );
108 is( scalar $c->paths, $PATHS, "Collation has all paths" );
109 is( scalar $c->relationships, 0, "Collation has all relationships" );
110
111 # Add a few relationships
112 $c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
113 $c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
114 $c->add_relationship( 'w257', 'w262', { 'type' => 'transposition' } );
115
116 # Now write it to GraphML and parse it again.
117
118 my $graphml = $c->as_graphml;
119 my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
120 is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
121 is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
122 is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
123
124 # Now add a stemma, write to GraphML, and look at the output.
125 SKIP: {
126         skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' );
127         my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
128         is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" );
129         is( $tradition->stemmata, 1, "Tradition now has the stemma" );
130         $graphml = $c->as_graphml;
131         like( $graphml, qr/digraph/, "Digraph declaration exists in GraphML" );
132 }
133 }
134
135
136
137 # =begin testing
138 {
139 use Text::Tradition;
140
141 my $cxfile = 't/data/Collatex-16.xml';
142 my $t = Text::Tradition->new( 
143     'name'  => 'inline', 
144     'input' => 'CollateX',
145     'file'  => $cxfile,
146     );
147 my $c = $t->collation;
148
149 # Make an svg
150 my $table = $c->alignment_table;
151 ok( $c->has_cached_table, "Alignment table was cached" );
152 is( $c->alignment_table, $table, "Cached table returned upon second call" );
153 $c->calculate_ranks;
154 is( $c->alignment_table, $table, "Cached table retained with no rank change" );
155 $c->add_relationship( 'n13', 'n23', { type => 'repetition' } );
156 is( $c->alignment_table, $table, "Alignment table unchanged after non-colo relationship add" );
157 $c->add_relationship( 'n24', 'n23', { type => 'spelling' } );
158 isnt( $c->alignment_table, $table, "Alignment table changed after colo relationship add" );
159 }
160
161
162
163 # =begin testing
164 {
165 use Text::Tradition;
166
167 my $cxfile = 't/data/Collatex-16.xml';
168 my $t = Text::Tradition->new( 
169     'name'  => 'inline', 
170     'input' => 'CollateX',
171     'file'  => $cxfile,
172     );
173 my $c = $t->collation;
174
175 my @common = $c->calculate_common_readings();
176 is( scalar @common, 8, "Found correct number of common readings" );
177 my @marked = sort $c->common_readings();
178 is( scalar @common, 8, "All common readings got marked as such" );
179 my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
180 is_deeply( \@marked, \@expected, "Found correct list of common readings" );
181 }
182
183
184
185 # =begin testing
186 {
187 use Text::Tradition;
188
189 my $cxfile = 't/data/Collatex-16.xml';
190 my $t = Text::Tradition->new( 
191     'name'  => 'inline', 
192     'input' => 'CollateX',
193     'file'  => $cxfile,
194     );
195 my $c = $t->collation;
196
197 is( $c->common_predecessor( 'n24', 'n23' )->id, 
198     'n20', "Found correct common predecessor" );
199 is( $c->common_successor( 'n24', 'n23' )->id, 
200     '__END__', "Found correct common successor" );
201
202 is( $c->common_predecessor( 'n19', 'n17' )->id, 
203     'n16', "Found correct common predecessor for readings on same path" );
204 is( $c->common_successor( 'n21', 'n10' )->id, 
205     '__END__', "Found correct common successor for readings on same path" );
206 }
207
208
209
210
211 1;