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