3 use strict; use warnings;
8 use XML::LibXML::XPathContext;
10 plan skip_all => 'Need Graphviz installed to test graphs'
11 unless File::Which::which( 'dot' );
13 my $datafile = 't/data/Collatex-16.xml';
15 my $tradition = Text::Tradition->new(
17 'input' => 'CollateX',
20 my $collation = $tradition->collation;
22 # Test the svg creation
23 my $parser = XML::LibXML->new();
24 $parser->load_ext_dtd( 0 );
25 my $svg = $parser->parse_string( $collation->as_svg() );
26 is( $svg->documentElement->nodeName(), 'svg', 'Got an svg document' );
28 # Test for the correct number of nodes in the SVG
29 my $svg_xpc = XML::LibXML::XPathContext->new( $svg->documentElement() );
30 $svg_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
31 my @svg_nodes = $svg_xpc->findnodes( '//svg:g[@class="node"]' );
32 is( scalar @svg_nodes, 26, "Correct number of nodes in the graph" );
34 # Test for the correct number of edges
35 my @svg_edges = $svg_xpc->findnodes( '//svg:g[@class="edge"]' );
36 is( scalar @svg_edges, 32, "Correct number of edges in the graph" );
38 # Test svg creation for a subgraph
39 my $part_svg = $parser->parse_string( $collation->as_svg( { from => 15 } ) ); # start, no end
40 is( $part_svg->documentElement->nodeName(), 'svg', "Got an svg subgraph to end" );
41 my $part_xpc = XML::LibXML::XPathContext->new( $part_svg->documentElement() );
42 $part_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
43 @svg_nodes = $part_xpc->findnodes( '//svg:g[@class="node"]' );
44 is( scalar( @svg_nodes ), 9,
45 "Correct number of nodes in the subgraph" );
46 @svg_edges = $part_xpc->findnodes( '//svg:g[@class="edge"]' );
47 is( scalar( @svg_edges ), 10,
48 "Correct number of edges in the subgraph" );
50 $part_svg = $parser->parse_string( $collation->as_svg( { from => 10, to => 13 } ) ); # start, no end
51 is( $part_svg->documentElement->nodeName(), 'svg', "Got an svg subgraph in the middle" );
52 $part_xpc = XML::LibXML::XPathContext->new( $part_svg->documentElement() );
53 $part_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
54 @svg_nodes = $part_xpc->findnodes( '//svg:g[@class="node"]' );
55 is( scalar( @svg_nodes ), 9,
56 "Correct number of nodes in the subgraph" );
57 @svg_edges = $part_xpc->findnodes( '//svg:g[@class="edge"]' );
58 is( scalar( @svg_edges ), 11,
59 "Correct number of edges in the subgraph" );
62 $part_svg = $parser->parse_string( $collation->as_svg( { to => 5 } ) ); # start, no end
63 is( $part_svg->documentElement->nodeName(), 'svg', "Got an svg subgraph from start" );
64 $part_xpc = XML::LibXML::XPathContext->new( $part_svg->documentElement() );
65 $part_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
66 @svg_nodes = $part_xpc->findnodes( '//svg:g[@class="node"]' );
67 is( scalar( @svg_nodes ), 7,
68 "Correct number of nodes in the subgraph" );
69 @svg_edges = $part_xpc->findnodes( '//svg:g[@class="edge"]' );
70 is( scalar( @svg_edges ), 7,
71 "Correct number of edges in the subgraph" );
73 # Test a right-to-left graph
74 my $arabic = Text::Tradition->new(
79 file => 't/data/arabic_snippet.csv' );
80 my $rl_svg = $parser->parse_string( $arabic->collation->as_svg() );
81 is( $rl_svg->documentElement->nodeName(), 'svg', "Got an svg subgraph from start" );
82 my $rl_xpc = XML::LibXML::XPathContext->new( $rl_svg->documentElement() );
83 $rl_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
85 foreach my $node ( $rl_xpc->findnodes( '//svg:g[@class="node"]' ) ) {
86 my $nid = $node->getAttribute('id');
87 $node_cx{$nid} = $rl_xpc->findvalue( './svg:ellipse/@cx', $node );
89 my @sorted = sort { $node_cx{$a} <=> $node_cx{$b} } keys( %node_cx );
90 is( $sorted[0], '__END__', "End node is the leftmost" );
91 is( $sorted[$#sorted], '__START__', "Start node is the rightmost" );
95 <g id="__END__" class="node"><title>__END__</title>
96 <ellipse fill="white" stroke="black" cx="38.3466" cy="-47" rx="38.1938" ry="18"/>
97 <text text-anchor="middle" x="38.3466" y="-43.3" font-family="Times,serif" font-size="14.00">#END#</text>
100 <g id="__START__" class="node"><title>__START__</title>
101 <ellipse fill="white" stroke="black" cx="1366.52" cy="-47" rx="48.9926" ry="18"/
103 <text text-anchor="middle" x="1366.52" y="-43.3" font-family="Times,serif" font-
104 size="14.00">#START#</text>
111 skip "lemmatization disabled for now", 1;
112 # Test for the correct common nodes
113 my @common_nodes = ( '#START#' );
114 push( @common_nodes, qw/ n1 n5 n6 n7 n12 n16 n19 n20 n27 / );
115 my @expected_nodes = map { [ $_, 1 ] } @common_nodes;
116 foreach my $idx ( qw/2 3 4 8 10 11 13 16 17 18/ ) {
117 splice( @expected_nodes, $idx, 0, [ "node_null", undef ] );
119 my @active_nodes = $collation->lemma_readings();
120 subtest 'Initial common points' => \&compare_active;
121 my $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced ... ... ... #';
122 is( make_text( @active_nodes ), $string, "Got the right starting text" );
125 is( scalar( @active_nodes ), scalar ( @expected_nodes ),
126 "Arrays are same length" );
128 foreach ( 0 .. scalar(@active_nodes)-1 ) {
129 is( $active_nodes[$_]->[1], $expected_nodes[$_]->[1],
130 "Element has same toggle value" );
131 if( defined $active_nodes[$_]->[1] ) {
132 is( $active_nodes[$_]->[0], $expected_nodes[$_]->[0],
133 "Active or toggled element has same node name "
134 . $active_nodes[$_]->[0] );
141 foreach my $n ( @_ ) {
143 push( @words, $collation->reading( $n->[0] )->text );
144 } elsif ( !defined $n->[1] ) {
145 push( @words, '...' );
148 return join( ' ', @words );
151 # Test that the common nodes are marked common
152 foreach my $cn ( @common_nodes ) {
153 ok( $collation->reading( $cn )->is_common, "Node $cn is marked common" );
156 # Test the manuscript paths
157 my $wit_a = '# when april with his showers sweet with fruit the drought of march has pierced unto the root #';
158 my $wit_b = '# when showers sweet with april fruit the march of drought has pierced to the root #';
159 my $wit_c = '# when showers sweet with april fruit teh drought of march has pierced teh rood #';
160 is( join( ' ', @{$tradition->witness( "A" )->text} ), $wit_a, "Correct path for witness A" );
161 is( join( ' ', @{$tradition->witness( "B" )->text} ), $wit_b, "Correct path for witness B" );
162 is( join( ' ', @{$tradition->witness( "C" )->text} ), $wit_c, "Correct path for witness C" );
164 # Test the transposition identifiers
165 my $transposition_pools = [ [ 'n2', 'n11' ], [ 'n14', 'n18' ],
167 my $transposed_nodes = { 'n2' => $transposition_pools->[0],
168 'n11' => $transposition_pools->[0],
169 'n14' => $transposition_pools->[1],
170 'n15' => $transposition_pools->[2],
171 'n17' => $transposition_pools->[2],
172 'n18' => $transposition_pools->[1],
175 my $real_transposed_nodes = {};
176 foreach my $r ( $collation->readings ) {
177 my @same = map { $_->name } @{$r->same_as};
178 $real_transposed_nodes->{ $r->name } = \@same if @same > 1;
181 is_deeply( $real_transposed_nodes, $transposed_nodes, "Found the right transpositions" );
183 # Test turning on a node
184 my @off = $collation->toggle_reading( 'n21' );
185 $expected_nodes[ 16 ] = [ "n21", 1 ];
186 @active_nodes = $collation->lemma_readings( @off );
187 subtest 'Turned on node for new location' => \&compare_active;
188 $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced unto ... ... #';
189 is( make_text( @active_nodes ), $string, "Got the right text" );
191 # Test the toggling effects of same-column
192 @off = $collation->toggle_reading( 'n22' );
193 splice( @expected_nodes, 16, 1, ( [ "n21", 0 ], [ "n22", 1 ] ) );
194 @active_nodes = $collation->lemma_readings( @off );
195 subtest 'Turned on other node in that location' => \&compare_active;
196 $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced to ... ... #';
197 is( make_text( @active_nodes ), $string, "Got the right text" );
199 # Test the toggling effects of transposition
200 @off = $collation->toggle_reading( 'n14' );
201 # Add the turned on node
202 $expected_nodes[ 11 ] = [ "n14", 1 ];
203 # Remove the 'off' for the previous node
204 splice( @expected_nodes, 16, 1 );
205 @active_nodes = $collation->lemma_readings( @off );
206 subtest 'Turned on transposition node' => \&compare_active;
207 $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #';
208 is( make_text( @active_nodes ), $string, "Got the right text" );
210 @off = $collation->toggle_reading( 'n18' );
211 # Toggle on the new node
212 $expected_nodes[ 13 ] = [ "n18", 1 ];
213 # Toggle off the transposed node
214 $expected_nodes[ 11 ] = [ "n14", undef ];
215 @active_nodes = $collation->lemma_readings( @off );
216 subtest 'Turned on that node\'s partner' => \&compare_active;
217 $string = '# when ... ... ... showers sweet with ... fruit ... ... of drought has pierced to ... ... #';
218 is( make_text( @active_nodes ), $string, "Got the right text" );
220 @off = $collation->toggle_reading( 'n14' );
221 # Toggle on the new node
222 $expected_nodes[ 11 ] = [ "n14", 1 ];
223 # Toggle off the transposed node
224 $expected_nodes[ 13 ] = [ "n18", undef ];
225 @active_nodes = $collation->lemma_readings( @off );
226 subtest 'Turned on the original node' => \&compare_active;
227 $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #';
228 is( make_text( @active_nodes ), $string, "Got the right text" );
230 @off = $collation->toggle_reading( 'n15' );
231 # Toggle on the new node, and off with the old
232 splice( @expected_nodes, 11, 1, [ "n14", 0 ], [ "n15", 1 ] );
233 @active_nodes = $collation->lemma_readings( @off );
234 subtest 'Turned on the colocated node' => \&compare_active;
235 $string = '# when ... ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
236 is( make_text( @active_nodes ), $string, "Got the right text" );
238 @off = $collation->toggle_reading( 'n3' );
239 # Toggle on the new node
240 splice( @expected_nodes, 3, 1, [ "n3", 1 ] );
241 # Remove the old toggle-off
242 splice( @expected_nodes, 11, 1 );
243 @active_nodes = $collation->lemma_readings( @off );
244 subtest 'Turned on a singleton node' => \&compare_active;
245 $string = '# when ... with ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
246 is( make_text( @active_nodes ), $string, "Got the right text" );
248 @off = $collation->toggle_reading( 'n3' );
249 # Toggle off this node
250 splice( @expected_nodes, 3, 1, [ "n3", 0 ] );
251 @active_nodes = $collation->lemma_readings( @off );
252 subtest 'Turned off a singleton node' => \&compare_active;
253 $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
254 is( make_text( @active_nodes ), $string, "Got the right text" );
256 @off = $collation->toggle_reading( 'n21' );
257 splice( @expected_nodes, 16, 1, ["n22", 0 ], [ "n21", 1 ] );
258 @active_nodes = $collation->lemma_readings( @off );
259 subtest 'Turned on another node after singleton switchoff' => \&compare_active;
260 $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto ... ... #';
261 is( make_text( @active_nodes ), $string, "Got the right text" );
263 # Now start testing some position identifiers
264 # 2. 'april with his' have no colocated
265 # 3. 'april' 2 has no colocated
267 # 5. 'drought' & 'march'
268 # 6. 'march' & 'drought'
269 # 7. 'unto' 'the' 'root'...
270 # 'unto can match 'to' or 'teh'
271 # 'the' can match 'teh' or 'rood'
272 # 'root' can mach 'rood'
274 foreach my $cn ( @common_nodes ) {
275 my $cnr = $collation->reading( $cn );
276 is( scalar( $collation->same_position_as( $cnr ) ), 0, "Node $cn has no colocations" );
279 my %expected_colocations = (
284 'n8' => [ 'n13' ], # teh -> the
285 'n13' => [ 'n8' ], # the -> teh
286 'n14' => [ 'n15' ], # drought -> march
287 'n18' => [ 'n17' ], # drought -> march
288 'n17' => [ 'n18' ], # march -> drought
289 'n15' => [ 'n14' ], # march -> drought
290 'n21' => [ 'n22', 'n9' ], # unto -> to, teh
291 'n22' => [ 'n21', 'n9' ], # to -> unto, teh
292 'n9' => [ 'n21', 'n22', 'n23' ], # teh -> unto, to, the
293 'n23' => [ 'n25', 'n9' ], # the -> teh, rood
294 'n25' => [ 'n23', 'n26' ], # rood -> the, root
295 'n26' => [ 'n25' ], # root -> rood
298 foreach my $n ( keys %expected_colocations ) {
299 my $nr = $collation->reading( $n );
300 my @colocated = sort( map { $_->name } $collation->same_position_as( $nr ) );
301 is_deeply( \@colocated, $expected_colocations{$n}, "Colocated nodes for $n correct" );
304 # Test strict colocations
305 $expected_colocations{'n9'} = [];
306 $expected_colocations{'n21'} = ['n22'];
307 $expected_colocations{'n22'} = ['n21'];
308 $expected_colocations{'n23'} = [];
309 $expected_colocations{'n25'} = [];
310 $expected_colocations{'n26'} = [];
312 foreach my $n ( keys %expected_colocations ) {
313 my $nr = $collation->reading( $n );
314 my @colocated = sort( map { $_->name } $collation->same_position_as( $nr, 1 ) );
315 is_deeply( \@colocated, $expected_colocations{$n}, "Strictly colocated nodes for $n correct" );
318 # Test turning on, then off, an annoyingly overlapping node
320 @off = $collation->toggle_reading( 'n9' );
321 # Remove the old toggle-off
322 splice( @expected_nodes, 16, 1 );
323 splice( @expected_nodes, 17, 0, [ "n9", 1 ] );
324 @active_nodes = $collation->lemma_readings( @off );
325 subtest 'Turned on a node without fixed position' => \&compare_active;
326 $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto teh ... ... #';
327 is( make_text( @active_nodes ), $string, "Got the right text" );
329 @off = $collation->toggle_reading( 'n23' );
330 splice( @expected_nodes, 18, 1, [ "n23", 1 ] );
331 @active_nodes = $collation->lemma_readings( @off );
332 subtest 'Turned on a node colocated to one without fixed position' => \&compare_active;
333 $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto teh the ... #';
334 is( make_text( @active_nodes ), $string, "Got the right text" );
336 @off = $collation->toggle_reading( 'n9' );
337 splice( @expected_nodes, 17, 1, [ "n9", 0 ] );
338 @active_nodes = $collation->lemma_readings( @off );
339 subtest 'Turned on a node colocated to one without fixed position' => \&compare_active;
340 $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto the ... #';
341 is( make_text( @active_nodes ), $string, "Got the right text" );
343 ### Now test relationship madness.
345 my( $result, @relations ) = $collation->add_relationship( 'n25', 'n23', {'type' => 'lexical'} ); # rood -> the
346 ok( $result, "Added relationship between nodes" );
347 is( scalar @relations, 1, "Returned only the one collapse" );
348 is_deeply( $relations[0], [ 'n25', 'n23' ], "Returned the correct collapse" );
349 is( $collation->reading( 'n25' )->position->reference, '9,3', "Harmonized position for n25 correct" );
350 is( $collation->reading( 'n23' )->position->reference, '9,3', "Harmonized position for n23 correct" );
351 is( $collation->reading( 'n9' )->position->reference, '9,2', "Adjusted position for n9 correct" );
353 # Do some yucky hardcoded cleanup to undo this relationship.
354 $collation->reading('n25')->position->max( 4 );
355 $collation->reading('n9')->position->max( 3 );
356 $collation->graph->del_edge( $collation->reading('n25')->edges_to( $collation->reading('n23')) );
358 ( $result, @relations ) = $collation->add_relationship( 'n26', 'n25', {'type' => 'spelling'} ); # root -> rood
359 ok( $result, "Added relationship between nodes" );
360 is( scalar @relations, 1, "Returned only the one collapse" );
361 is_deeply( $relations[0], [ 'n26', 'n25' ], "Returned the correct collapse" );
362 is( $collation->reading( 'n26' )->position->reference, '9,4', "Harmonized position for n26 correct" );
363 is( $collation->reading( 'n25' )->position->reference, '9,4', "Harmonized position for n25 correct" );
364 is( $collation->reading( 'n9' )->position->reference, '9,2-3', "Adjusted position for n9 correct" );
366 ( $result, @relations ) = $collation->add_relationship( 'n15', 'n9', {'type' => 'lexical'} ); # bogus march -> teh
367 ok( !$result, "Refused to add skewed relationship: " . $relations[0] );
369 ( $result, @relations ) = $collation->add_relationship( 'n25', 'n26', {'type' => 'spelling'} ); # root -> rood
370 ok( !$result, "Refused to add dupe relationship: " . $relations[0] );
372 ( $result, @relations ) = $collation->add_relationship( 'n8', 'n13', {'type' => 'spelling', 'global' => 1 } ); # teh -> the
373 ok( $result, "Added global relationship between nodes" );
374 is( scalar @relations, 2, "Returned two relationship creations" );
375 is_deeply( $relations[0], [ 'n8', 'n13' ], "Returned the original collapse" );
376 is_deeply( $relations[1], [ 'n9', 'n23' ], "Returned the other collapse" );
377 is( $collation->reading( 'n8' )->position->reference, '6,2', "Harmonized position for n8 correct" );
378 is( $collation->reading( 'n9' )->position->reference, '9,3', "Harmonized position for n9 correct" );