4 use Test::More 'no_plan';
14 my $cxfile = 't/data/Collatex-16.xml';
15 my $t = Text::Tradition->new(
17 'input' => 'CollateX',
20 my $c = $t->collation;
22 my $rno = scalar $c->readings;
23 # Split n21 ('unto') for testing purposes
24 my $new_r = $c->add_reading( { 'id' => 'n21p0', 'text' => 'un', 'join_next' => 1 } );
25 my $old_r = $c->reading( 'n21' );
26 $old_r->alter_text( 'to' );
27 $c->del_path( 'n20', 'n21', 'A' );
28 $c->add_path( 'n20', 'n21p0', 'A' );
29 $c->add_path( 'n21p0', 'n21', 'A' );
30 $c->add_relationship( 'n21', 'n22', { type => 'collated', scope => 'local' } );
32 ok( $c->reading( 'n21p0' ), "New reading exists" );
33 is( scalar $c->readings, $rno, "Reading add offset by flatten_ranks" );
35 # Combine n3 and n4 ( with his )
36 $c->merge_readings( 'n3', 'n4', 1 );
37 ok( !$c->reading('n4'), "Reading n4 is gone" );
38 is( $c->reading('n3')->text, 'with his', "Reading n3 has both words" );
40 # Collapse n9 and n10 ( rood / root )
41 $c->merge_readings( 'n9', 'n10' );
42 ok( !$c->reading('n10'), "Reading n10 is gone" );
43 is( $c->reading('n9')->text, 'rood', "Reading n9 has an unchanged word" );
45 # Try to combine n21 and n21p0. This should break.
46 my $remaining = $c->reading('n21');
47 $remaining ||= $c->reading('n22'); # one of these should still exist
49 $c->merge_readings( 'n21p0', $remaining, 1 );
50 ok( 0, "Bad reading merge changed the graph" );
51 } catch( Text::Tradition::Error $e ) {
52 like( $e->message, qr/neither concatenated nor collated/, "Expected exception from bad concatenation" );
54 ok( 0, "Unexpected error on bad reading merge: $@" );
58 $c->calculate_ranks();
59 ok( 1, "Graph is still evidently whole" );
60 } catch( Text::Tradition::Error $e ) {
61 ok( 0, "Caught a rank exception: " . $e->message );
75 $t = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
76 } [qr/Cannot set relationship on a meta reading/],
77 "Got expected relationship drop warning on parse";
79 my $c = $t->collation;
82 map { $rdg_ids{$_} = 1 } $c->readings;
83 $c->merge_related( 'orthographic' );
84 is( scalar( $c->readings ), keys( %rdg_ids ) - 9,
85 "Successfully collapsed orthographic variation" );
86 map { $rdg_ids{$_} = undef } qw/ r13.3 r11.4 r8.5 r8.2 r7.7 r7.5 r7.4 r7.3 r7.1 /;
87 foreach my $rid ( keys %rdg_ids ) {
88 my $exp = $rdg_ids{$rid};
89 is( !$c->reading( $rid ), !$exp, "Reading $rid correctly " .
90 ( $exp ? "retained" : "removed" ) );
92 ok( $c->linear, "Graph is still linear" );
94 $c->calculate_ranks; # This should succeed
95 ok( 1, "Can still calculate ranks on the new graph" );
97 ok( 0, "Rank calculation on merged graph failed: $@" );
100 # Now add some transpositions
101 $c->add_relationship( 'r8.4', 'r10.4', { type => 'transposition' } );
102 $c->merge_related( 'transposition' );
103 is( scalar( $c->readings ), keys( %rdg_ids ) - 10,
104 "Transposed relationship is merged away" );
105 ok( !$c->reading('r8.4'), "Correct transposed reading removed" );
106 ok( !$c->linear, "Graph is no longer linear" );
108 $c->calculate_ranks; # This should fail
109 ok( 0, "Rank calculation happened on nonlinear graph?!" );
110 } catch ( Text::Tradition::Error $e ) {
111 is( $e->message, 'Cannot calculate ranks on a non-linear graph',
112 "Rank calculation on merged graph threw an error" );
120 use Test::More::UTF8;
124 my $st = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/collatecorr.xml' );
125 is( ref( $st ), 'Text::Tradition', "Got a tradition from test file" );
126 ok( $st->has_witness('Ba96'), "Tradition has the affected witness" );
128 my $sc = $st->collation;
130 ok( $sc->reading('n131'), "Tradition has the affected reading" );
131 is( scalar( $sc->readings ), $numr, "There are $numr readings in the graph" );
132 is( $sc->end->rank, 14, "There are fourteen ranks in the graph" );
134 # Detach the erroneously collated reading
135 my( $newr, @del_rdgs ) = $sc->duplicate_reading( 'n131', 'Ba96' );
136 ok( $newr, "New reading was created" );
137 ok( $sc->reading('n131_0'), "Detached the bad collation with a new reading" );
138 is( scalar( $sc->readings ), $numr + 1, "A reading was added to the graph" );
139 is( $sc->end->rank, 10, "There are now only ten ranks in the graph" );
140 my $csucc = $sc->common_successor( 'n131', 'n131_0' );
141 is( $csucc->id, 'n136', "Found correct common successor to duped reading" );
143 # Check that the bad transposition is gone
144 is( scalar @del_rdgs, 1, "Deleted reading was returned by API call" );
145 is( $sc->get_relationship( 'n130', 'n135' ), undef, "Bad transposition relationship is gone" );
147 # The collation should not be fixed
148 my @pairs = $sc->identical_readings();
149 is( scalar @pairs, 0, "Not re-collated yet" );
151 ok( $sc->merge_readings( 'n124', 'n131_0' ), "Collated the readings correctly" );
152 @pairs = $sc->identical_readings( start => 'n124', end => $csucc->id );
153 is( scalar @pairs, 3, "Found three more identical readings" );
154 is( $sc->end->rank, 11, "The ranks shifted appropriately" );
155 $sc->flatten_ranks();
156 is( scalar( $sc->readings ), $numr - 3, "Now we are collated correctly" );
158 # Check that we can't "duplicate" a reading with no wits or with all wits
160 my( $badr, @del_rdgs ) = $sc->duplicate_reading( 'n124' );
161 ok( 0, "Reading duplication without witnesses throws an error" );
162 } catch( Text::Tradition::Error $e ) {
163 like( $e->message, qr/Must specify one or more witnesses/,
164 "Reading duplication without witnesses throws the expected error" );
166 ok( 0, "Reading duplication without witnesses threw the wrong error" );
170 my( $badr, @del_rdgs ) = $sc->duplicate_reading( 'n124', 'Ba96', 'Mü11475' );
171 ok( 0, "Reading duplication with all witnesses throws an error" );
172 } catch( Text::Tradition::Error $e ) {
173 like( $e->message, qr/Cannot join all witnesses/,
174 "Reading duplication with all witnesses throws the expected error" );
176 ok( 0, "Reading duplication with all witnesses threw the wrong error" );
180 $sc->calculate_ranks();
181 ok( 1, "Graph is still evidently whole" );
182 } catch( Text::Tradition::Error $e ) {
183 ok( 0, "Caught a rank exception: " . $e->message );
197 my $datafile = 't/data/florilegium_tei_ps.xml';
198 my $tradition = Text::Tradition->new( 'input' => 'TEI',
203 ok( $tradition, "Got a tradition object" );
204 is( scalar $tradition->witnesses, 13, "Found all witnesses" );
205 ok( $tradition->collation, "Tradition has a collation" );
207 my $c = $tradition->collation;
208 is( scalar $c->readings, $READINGS, "Collation has all readings" );
209 is( scalar $c->paths, $PATHS, "Collation has all paths" );
210 is( scalar $c->relationships, 0, "Collation has all relationships" );
212 # Add a few relationships
213 $c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
214 $c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
215 $c->add_relationship( 'w257', 'w262', { 'type' => 'transposition',
216 'is_significant' => 'yes' } );
218 # Now write it to GraphML and parse it again.
220 my $graphml = $c->as_graphml;
221 my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
222 is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
223 is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
224 is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
225 my $sigrel = $st->collation->get_relationship( 'w257', 'w262' );
226 is( $sigrel->is_significant, 'yes', "Ternary attribute value was restored" );
228 # Now add a stemma, write to GraphML, and look at the output.
230 skip "Analysis module not present", 3 unless $tradition->can( 'add_stemma' );
231 my $stemma = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
232 is( ref( $stemma ), 'Text::Tradition::Stemma', "Parsed dotfile into stemma" );
233 is( $tradition->stemmata, 1, "Tradition now has the stemma" );
234 $graphml = $c->as_graphml;
235 like( $graphml, qr/digraph/, "Digraph declaration exists in GraphML" );
251 my $datafile = 't/data/florilegium_tei_ps.xml';
252 my $tradition = Text::Tradition->new( 'input' => 'TEI',
257 my $c = $tradition->collation;
258 # Export the thing to CSV
259 my $csvstr = $c->as_csv();
261 my $csv = Text::CSV->new({ sep_char => ',', binary => 1 });
262 my @lines = split(/\n/, $csvstr );
263 ok( $csv->parse( $lines[0] ), "Successfully parsed first line of CSV" );
264 is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
265 my @q_ac = grep { $_ eq 'Q'.$c->ac_label } $csv->fields;
266 ok( @q_ac, "Found a layered witness" );
268 my $t2 = Text::Tradition->new( input => 'Tabular',
272 is( scalar $t2->collation->readings, $READINGS, "Reparsed CSV collation has all readings" );
273 is( scalar $t2->collation->paths, $PATHS, "Reparsed CSV collation has all paths" );
276 my $tsvstr = $c->as_tsv();
277 my $t3 = Text::Tradition->new( input => 'Tabular',
281 is( scalar $t3->collation->readings, $READINGS, "Reparsed TSV collation has all readings" );
282 is( scalar $t3->collation->paths, $PATHS, "Reparsed TSV collation has all paths" );
284 my $table = $c->alignment_table;
285 my $noaccsv = $c->as_csv({ noac => 1 });
286 my @noaclines = split(/\n/, $noaccsv );
287 ok( $csv->parse( $noaclines[0] ), "Successfully parsed first line of no-ac CSV" );
288 is( scalar( $csv->fields ), $WITS, "CSV has correct number of witness columns" );
289 is( $c->alignment_table, $table, "Request for CSV did not alter the alignment table" );
291 my $safecsv = $c->as_csv({ safe_ac => 1});
292 my @safelines = split(/\n/, $safecsv );
293 ok( $csv->parse( $safelines[0] ), "Successfully parsed first line of safe CSV" );
294 is( scalar( $csv->fields ), $WITS + $WITAC, "CSV has correct number of witness columns" );
295 @q_ac = grep { $_ eq 'Q__L' } $csv->fields;
296 ok( @q_ac, "Found a sanitized layered witness" );
297 is( $c->alignment_table, $table, "Request for CSV did not alter the alignment table" );
306 my $cxfile = 't/data/Collatex-16.xml';
307 my $t = Text::Tradition->new(
309 'input' => 'CollateX',
312 my $c = $t->collation;
315 my $table = $c->alignment_table;
316 ok( $c->has_cached_table, "Alignment table was cached" );
317 is( $c->alignment_table, $table, "Cached table returned upon second call" );
319 is( $c->alignment_table, $table, "Cached table retained with no rank change" );
320 $c->add_relationship( 'n13', 'n23', { type => 'repetition' } );
321 is( $c->alignment_table, $table, "Alignment table unchanged after non-colo relationship add" );
322 $c->add_relationship( 'n24', 'n23', { type => 'spelling' } );
323 isnt( $c->alignment_table, $table, "Alignment table changed after colo relationship add" );
332 my $cxfile = 't/data/Collatex-16.xml';
333 my $t = Text::Tradition->new(
335 'input' => 'CollateX',
338 my $c = $t->collation;
340 my @common = $c->calculate_common_readings();
341 is( scalar @common, 8, "Found correct number of common readings" );
342 my @marked = sort $c->common_readings();
343 is( scalar @common, 8, "All common readings got marked as such" );
344 my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
345 is_deeply( \@marked, \@expected, "Found correct list of common readings" );
354 my $cxfile = 't/data/Collatex-16.xml';
355 my $t = Text::Tradition->new(
357 'input' => 'CollateX',
360 my $c = $t->collation;
362 is( $c->common_predecessor( 'n24', 'n23' )->id,
363 'n20', "Found correct common predecessor" );
364 is( $c->common_successor( 'n24', 'n23' )->id,
365 '__END__', "Found correct common successor" );
367 is( $c->common_predecessor( 'n19', 'n17' )->id,
368 'n16', "Found correct common predecessor for readings on same path" );
369 is( $c->common_successor( 'n21', 'n10' )->id,
370 '__END__', "Found correct common successor for readings on same path" );