make tests for Tradition.pm and Tradition/Parser/CollateX.pm work
[scpubgit/stemmatology.git] / t / graph.t
CommitLineData
b49c4318 1#!/usr/bin/perl
2
3use strict; use warnings;
4use Test::More;
5use lib 'lib';
8e1394aa 6use Text::Tradition;
b49c4318 7use XML::LibXML;
8use XML::LibXML::XPathContext;
9
10my $datafile = 't/data/Collatex-16.xml';
11
12open( GRAPHFILE, $datafile ) or die "Could not open $datafile";
13my @lines = <GRAPHFILE>;
14close GRAPHFILE;
ec3f9144 15my $tradition = Text::Tradition->new( 'CollateX' => join( '', @lines ) );
8e1394aa 16my $collation = $tradition->collation;
b49c4318 17
18# Test the svg creation
19my $parser = XML::LibXML->new();
20$parser->load_ext_dtd( 0 );
8e1394aa 21my $svg = $parser->parse_string( $collation->as_svg() );
b49c4318 22is( $svg->documentElement->nodeName(), 'svg', 'Got an svg document' );
23
24# Test for the correct number of nodes in the SVG
25my $svg_xpc = XML::LibXML::XPathContext->new( $svg->documentElement() );
26$svg_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
27my @svg_nodes = $svg_xpc->findnodes( '//svg:g[@class="node"]' );
3265b0ce 28is( scalar @svg_nodes, 26, "Correct number of nodes in the graph" );
b49c4318 29
30# Test for the correct number of edges
31my @svg_edges = $svg_xpc->findnodes( '//svg:g[@class="edge"]' );
3265b0ce 32is( scalar @svg_edges, 32, "Correct number of edges in the graph" );
b49c4318 33
34# Test for the correct common nodes
b15511bf 35my @common_nodes = ( '#START#' );
36push( @common_nodes, qw/ n1 n5 n6 n7 n12 n16 n19 n20 n27 / );
37my @expected_nodes = map { [ $_, 1 ] } @common_nodes;
3265b0ce 38foreach my $idx ( qw/2 3 4 8 10 11 13 16 17 18/ ) {
b49c4318 39 splice( @expected_nodes, $idx, 0, [ "node_null", undef ] );
40}
3a1f2523 41my @active_nodes = $collation->lemma_readings();
b49c4318 42subtest 'Initial common points' => \&compare_active;
3265b0ce 43my $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced ... ... ... #';
b49c4318 44is( make_text( @active_nodes ), $string, "Got the right starting text" );
45
46sub compare_active {
47 is( scalar( @active_nodes ), scalar ( @expected_nodes ),
48 "Arrays are same length" );
49
50 foreach ( 0 .. scalar(@active_nodes)-1 ) {
51 is( $active_nodes[$_]->[1], $expected_nodes[$_]->[1],
52 "Element has same toggle value" );
53 if( defined $active_nodes[$_]->[1] ) {
54 is( $active_nodes[$_]->[0], $expected_nodes[$_]->[0],
de51424a 55 "Active or toggled element has same node name "
56 . $active_nodes[$_]->[0] );
b49c4318 57 }
58 }
59}
60
61sub make_text {
62 my @words;
63 foreach my $n ( @_ ) {
64 if( $n->[1] ) {
3a1f2523 65 push( @words, $collation->reading( $n->[0] )->label );
b49c4318 66 } elsif ( !defined $n->[1] ) {
67 push( @words, '...' );
68 }
69 }
70 return join( ' ', @words );
71}
72
b15511bf 73# Test that the common nodes are marked common
74foreach my $cn ( @common_nodes ) {
75 ok( $collation->reading( $cn )->is_common, "Node $cn is marked common" );
76}
77
b49c4318 78# Test the manuscript paths
79my $wit_a = '# when april with his showers sweet with fruit the drought of march has pierced unto the root #';
80my $wit_b = '# when showers sweet with april fruit the march of drought has pierced to the root #';
3265b0ce 81my $wit_c = '# when showers sweet with april fruit teh drought of march has pierced teh rood #';
de51424a 82is( join( ' ', @{$tradition->witness( "A" )->text} ), $wit_a, "Correct path for witness A" );
83is( join( ' ', @{$tradition->witness( "B" )->text} ), $wit_b, "Correct path for witness B" );
84is( join( ' ', @{$tradition->witness( "C" )->text} ), $wit_c, "Correct path for witness C" );
b49c4318 85
86# Test the transposition identifiers
c557b209 87my $transposition_pools = [ [ 'n2', 'n11' ], [ 'n14', 'n18' ],
88 [ 'n17', 'n15' ] ];
89my $transposed_nodes = { 'n2' => $transposition_pools->[0],
90 'n11' => $transposition_pools->[0],
91 'n14' => $transposition_pools->[1],
92 'n15' => $transposition_pools->[2],
93 'n17' => $transposition_pools->[2],
94 'n18' => $transposition_pools->[1],
b49c4318 95};
de51424a 96
97my $real_transposed_nodes = {};
98foreach my $r ( $collation->readings ) {
99 my @same = map { $_->name } @{$r->same_as};
100 $real_transposed_nodes->{ $r->name } = \@same if @same > 1;
c2d16875 101}
de51424a 102
103is_deeply( $real_transposed_nodes, $transposed_nodes, "Found the right transpositions" );
b49c4318 104
105# Test turning on a node
3265b0ce 106my @off = $collation->toggle_reading( 'n21' );
107$expected_nodes[ 16 ] = [ "n21", 1 ];
de51424a 108@active_nodes = $collation->lemma_readings( @off );
b49c4318 109subtest 'Turned on node for new location' => \&compare_active;
3265b0ce 110$string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced unto ... ... #';
b49c4318 111is( make_text( @active_nodes ), $string, "Got the right text" );
112
113# Test the toggling effects of same-column
3265b0ce 114@off = $collation->toggle_reading( 'n22' );
115splice( @expected_nodes, 16, 1, ( [ "n21", 0 ], [ "n22", 1 ] ) );
de51424a 116@active_nodes = $collation->lemma_readings( @off );
b49c4318 117subtest 'Turned on other node in that location' => \&compare_active;
3265b0ce 118$string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced to ... ... #';
b49c4318 119is( make_text( @active_nodes ), $string, "Got the right text" );
120
121# Test the toggling effects of transposition
de51424a 122@off = $collation->toggle_reading( 'n14' );
b49c4318 123# Add the turned on node
c557b209 124$expected_nodes[ 11 ] = [ "n14", 1 ];
58a3c424 125# Remove the 'off' for the previous node
3265b0ce 126splice( @expected_nodes, 16, 1 );
de51424a 127@active_nodes = $collation->lemma_readings( @off );
b49c4318 128subtest 'Turned on transposition node' => \&compare_active;
3265b0ce 129$string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #';
b49c4318 130is( make_text( @active_nodes ), $string, "Got the right text" );
131
de51424a 132@off = $collation->toggle_reading( 'n18' );
58a3c424 133# Toggle on the new node
c557b209 134$expected_nodes[ 13 ] = [ "n18", 1 ];
58a3c424 135# Toggle off the transposed node
c557b209 136$expected_nodes[ 11 ] = [ "n14", undef ];
de51424a 137@active_nodes = $collation->lemma_readings( @off );
b49c4318 138subtest 'Turned on that node\'s partner' => \&compare_active;
3265b0ce 139$string = '# when ... ... ... showers sweet with ... fruit ... ... of drought has pierced to ... ... #';
b49c4318 140is( make_text( @active_nodes ), $string, "Got the right text" );
141
de51424a 142@off = $collation->toggle_reading( 'n14' );
58a3c424 143# Toggle on the new node
c557b209 144$expected_nodes[ 11 ] = [ "n14", 1 ];
58a3c424 145# Toggle off the transposed node
c557b209 146$expected_nodes[ 13 ] = [ "n18", undef ];
de51424a 147@active_nodes = $collation->lemma_readings( @off );
b49c4318 148subtest 'Turned on the original node' => \&compare_active;
3265b0ce 149$string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #';
b49c4318 150is( make_text( @active_nodes ), $string, "Got the right text" );
151
de51424a 152@off = $collation->toggle_reading( 'n15' );
58a3c424 153# Toggle on the new node, and off with the old
c557b209 154splice( @expected_nodes, 11, 1, [ "n14", 0 ], [ "n15", 1 ] );
de51424a 155@active_nodes = $collation->lemma_readings( @off );
58a3c424 156subtest 'Turned on the colocated node' => \&compare_active;
3265b0ce 157$string = '# when ... ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
58a3c424 158is( make_text( @active_nodes ), $string, "Got the right text" );
159
de51424a 160@off = $collation->toggle_reading( 'n3' );
58a3c424 161# Toggle on the new node
c557b209 162splice( @expected_nodes, 3, 1, [ "n3", 1 ] );
58a3c424 163# Remove the old toggle-off
c557b209 164splice( @expected_nodes, 11, 1 );
de51424a 165@active_nodes = $collation->lemma_readings( @off );
b49c4318 166subtest 'Turned on a singleton node' => \&compare_active;
3265b0ce 167$string = '# when ... with ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
b49c4318 168is( make_text( @active_nodes ), $string, "Got the right text" );
169
de51424a 170@off = $collation->toggle_reading( 'n3' );
58a3c424 171# Toggle off this node
c557b209 172splice( @expected_nodes, 3, 1, [ "n3", 0 ] );
de51424a 173@active_nodes = $collation->lemma_readings( @off );
b49c4318 174subtest 'Turned off a singleton node' => \&compare_active;
3265b0ce 175$string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
58a3c424 176is( make_text( @active_nodes ), $string, "Got the right text" );
177
de51424a 178@off = $collation->toggle_reading( 'n21' );
3265b0ce 179splice( @expected_nodes, 16, 1, ["n22", 0 ], [ "n21", 1 ] );
de51424a 180@active_nodes = $collation->lemma_readings( @off );
3265b0ce 181subtest 'Turned on another node after singleton switchoff' => \&compare_active;
182$string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto ... ... #';
b49c4318 183is( make_text( @active_nodes ), $string, "Got the right text" );
184
b15511bf 185# Now start testing some position identifiers
186# 2. 'april with his' have no colocated
187# 3. 'april' 2 has no colocated
188# 4. 'teh' and 'the'
189# 5. 'drought' & 'march'
190# 6. 'march' & 'drought'
191# 7. 'unto' 'the' 'root'...
192# 'unto can match 'to' or 'teh'
193# 'the' can match 'teh' or 'rood'
194# 'root' can mach 'rood'
195
196foreach my $cn ( @common_nodes ) {
197 my $cnr = $collation->reading( $cn );
198 is( scalar( $collation->same_position_as( $cnr ) ), 0, "Node $cn has no colocations" );
199}
200
201my %expected_colocations = (
202 'n2' => [], # april
203 'n3' => [], # with
204 'n4' => [], # his
205 'n11' => [], # april
206 'n8' => [ 'n13' ], # teh -> the
207 'n13' => [ 'n8' ], # the -> teh
208 'n14' => [ 'n15' ], # drought -> march
209 'n18' => [ 'n17' ], # drought -> march
210 'n17' => [ 'n18' ], # march -> drought
211 'n15' => [ 'n14' ], # march -> drought
4cdd82f1 212 'n21' => [ 'n22', 'n9' ], # unto -> to, teh
213 'n22' => [ 'n21', 'n9' ], # to -> unto, teh
b15511bf 214 'n9' => [ 'n21', 'n22', 'n23' ], # teh -> unto, to, the
4cdd82f1 215 'n23' => [ 'n25', 'n9' ], # the -> teh, rood
216 'n25' => [ 'n23', 'n26' ], # rood -> the, root
b15511bf 217 'n26' => [ 'n25' ], # root -> rood
218);
219
220foreach my $n ( keys %expected_colocations ) {
221 my $nr = $collation->reading( $n );
222 my @colocated = sort( map { $_->name } $collation->same_position_as( $nr ) );
223 is_deeply( \@colocated, $expected_colocations{$n}, "Colocated nodes for $n correct" );
224}
225
4cdd82f1 226# Test strict colocations
227$expected_colocations{'n9'} = [];
228$expected_colocations{'n21'} = ['n22'];
229$expected_colocations{'n22'} = ['n21'];
230$expected_colocations{'n23'} = [];
231$expected_colocations{'n25'} = [];
232$expected_colocations{'n26'} = [];
233
234foreach my $n ( keys %expected_colocations ) {
235 my $nr = $collation->reading( $n );
236 my @colocated = sort( map { $_->name } $collation->same_position_as( $nr, 1 ) );
237 is_deeply( \@colocated, $expected_colocations{$n}, "Strictly colocated nodes for $n correct" );
238}
239
240# Test turning on, then off, an annoyingly overlapping node
241
242@off = $collation->toggle_reading( 'n9' );
243# Remove the old toggle-off
244splice( @expected_nodes, 16, 1 );
245splice( @expected_nodes, 17, 0, [ "n9", 1 ] );
246@active_nodes = $collation->lemma_readings( @off );
247subtest 'Turned on a node without fixed position' => \&compare_active;
248$string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto teh ... ... #';
249is( make_text( @active_nodes ), $string, "Got the right text" );
250
251@off = $collation->toggle_reading( 'n23' );
252splice( @expected_nodes, 18, 1, [ "n23", 1 ] );
253@active_nodes = $collation->lemma_readings( @off );
254subtest 'Turned on a node colocated to one without fixed position' => \&compare_active;
255$string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto teh the ... #';
256is( make_text( @active_nodes ), $string, "Got the right text" );
257
258@off = $collation->toggle_reading( 'n9' );
259splice( @expected_nodes, 17, 1, [ "n9", 0 ] );
260@active_nodes = $collation->lemma_readings( @off );
261subtest 'Turned on a node colocated to one without fixed position' => \&compare_active;
262$string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto the ... #';
263is( make_text( @active_nodes ), $string, "Got the right text" );
264
265### Now test relationship madness.
266
267my( $result, @relations ) = $collation->add_relationship( 'n25', 'n23', {'type' => 'lexical'} ); # rood -> the
268ok( $result, "Added relationship between nodes" );
269is( scalar @relations, 1, "Returned only the one collapse" );
270is_deeply( $relations[0], [ 'n25', 'n23' ], "Returned the correct collapse" );
271is( $collation->reading( 'n25' )->position->reference, '9,3', "Harmonized position for n25 correct" );
272is( $collation->reading( 'n23' )->position->reference, '9,3', "Harmonized position for n23 correct" );
273is( $collation->reading( 'n9' )->position->reference, '9,2', "Adjusted position for n9 correct" );
274
275# Do some yucky hardcoded cleanup to undo this relationship.
276$collation->reading('n25')->position->max( 4 );
277$collation->reading('n9')->position->max( 3 );
278$collation->graph->del_edge( $collation->reading('n25')->edges_to( $collation->reading('n23')) );
279
280( $result, @relations ) = $collation->add_relationship( 'n26', 'n25', {'type' => 'spelling'} ); # root -> rood
281ok( $result, "Added relationship between nodes" );
282is( scalar @relations, 1, "Returned only the one collapse" );
283is_deeply( $relations[0], [ 'n26', 'n25' ], "Returned the correct collapse" );
284is( $collation->reading( 'n26' )->position->reference, '9,4', "Harmonized position for n26 correct" );
285is( $collation->reading( 'n25' )->position->reference, '9,4', "Harmonized position for n25 correct" );
286is( $collation->reading( 'n9' )->position->reference, '9,2-3', "Adjusted position for n9 correct" );
287
288( $result, @relations ) = $collation->add_relationship( 'n15', 'n9', {'type' => 'lexical'} ); # bogus march -> teh
289ok( !$result, "Refused to add skewed relationship: " . $relations[0] );
290
291( $result, @relations ) = $collation->add_relationship( 'n25', 'n26', {'type' => 'spelling'} ); # root -> rood
292ok( !$result, "Refused to add dupe relationship: " . $relations[0] );
293
294( $result, @relations ) = $collation->add_relationship( 'n8', 'n13', {'type' => 'spelling', 'global' => 1 } ); # teh -> the
295ok( $result, "Added global relationship between nodes" );
296is( scalar @relations, 2, "Returned two relationship creations" );
297is_deeply( $relations[0], [ 'n8', 'n13' ], "Returned the original collapse" );
298is_deeply( $relations[1], [ 'n9', 'n23' ], "Returned the other collapse" );
299is( $collation->reading( 'n8' )->position->reference, '6,2', "Harmonized position for n8 correct" );
300is( $collation->reading( 'n9' )->position->reference, '9,3', "Harmonized position for n9 correct" );
301
b49c4318 302done_testing();