Commit | Line | Data |
b49c4318 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; use warnings; |
4 | use Test::More; |
5 | use lib 'lib'; |
8e1394aa |
6 | use Text::Tradition; |
b49c4318 |
7 | use XML::LibXML; |
8 | use XML::LibXML::XPathContext; |
9 | |
10 | my $datafile = 't/data/Collatex-16.xml'; |
11 | |
7035e3a6 |
12 | my $tradition = Text::Tradition->new( |
13 | 'name' => 'inline', |
14 | 'input' => 'CollateX', |
15 | 'file' => $datafile, |
16 | ); |
8e1394aa |
17 | my $collation = $tradition->collation; |
b49c4318 |
18 | |
19 | # Test the svg creation |
20 | my $parser = XML::LibXML->new(); |
21 | $parser->load_ext_dtd( 0 ); |
8e1394aa |
22 | my $svg = $parser->parse_string( $collation->as_svg() ); |
b49c4318 |
23 | is( $svg->documentElement->nodeName(), 'svg', 'Got an svg document' ); |
24 | |
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"]' ); |
3265b0ce |
29 | is( scalar @svg_nodes, 26, "Correct number of nodes in the graph" ); |
b49c4318 |
30 | |
31 | # Test for the correct number of edges |
32 | my @svg_edges = $svg_xpc->findnodes( '//svg:g[@class="edge"]' ); |
3265b0ce |
33 | is( scalar @svg_edges, 32, "Correct number of edges in the graph" ); |
b49c4318 |
34 | |
35 | # Test for the correct common nodes |
b15511bf |
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; |
3265b0ce |
39 | foreach my $idx ( qw/2 3 4 8 10 11 13 16 17 18/ ) { |
b49c4318 |
40 | splice( @expected_nodes, $idx, 0, [ "node_null", undef ] ); |
41 | } |
3a1f2523 |
42 | my @active_nodes = $collation->lemma_readings(); |
b49c4318 |
43 | subtest 'Initial common points' => \&compare_active; |
3265b0ce |
44 | my $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced ... ... ... #'; |
b49c4318 |
45 | is( make_text( @active_nodes ), $string, "Got the right starting text" ); |
46 | |
47 | sub compare_active { |
48 | is( scalar( @active_nodes ), scalar ( @expected_nodes ), |
49 | "Arrays are same length" ); |
50 | |
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], |
de51424a |
56 | "Active or toggled element has same node name " |
57 | . $active_nodes[$_]->[0] ); |
b49c4318 |
58 | } |
59 | } |
60 | } |
61 | |
62 | sub make_text { |
63 | my @words; |
64 | foreach my $n ( @_ ) { |
65 | if( $n->[1] ) { |
7035e3a6 |
66 | push( @words, $collation->reading( $n->[0] )->text ); |
b49c4318 |
67 | } elsif ( !defined $n->[1] ) { |
68 | push( @words, '...' ); |
69 | } |
70 | } |
71 | return join( ' ', @words ); |
72 | } |
73 | |
b15511bf |
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" ); |
77 | } |
78 | |
b49c4318 |
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 #'; |
3265b0ce |
82 | my $wit_c = '# when showers sweet with april fruit teh drought of march has pierced teh rood #'; |
de51424a |
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" ); |
b49c4318 |
86 | |
87 | # Test the transposition identifiers |
c557b209 |
88 | my $transposition_pools = [ [ 'n2', 'n11' ], [ 'n14', 'n18' ], |
89 | [ 'n17', 'n15' ] ]; |
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], |
b49c4318 |
96 | }; |
de51424a |
97 | |
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; |
c2d16875 |
102 | } |
de51424a |
103 | |
104 | is_deeply( $real_transposed_nodes, $transposed_nodes, "Found the right transpositions" ); |
b49c4318 |
105 | |
106 | # Test turning on a node |
3265b0ce |
107 | my @off = $collation->toggle_reading( 'n21' ); |
108 | $expected_nodes[ 16 ] = [ "n21", 1 ]; |
de51424a |
109 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
110 | subtest 'Turned on node for new location' => \&compare_active; |
3265b0ce |
111 | $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced unto ... ... #'; |
b49c4318 |
112 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
113 | |
114 | # Test the toggling effects of same-column |
3265b0ce |
115 | @off = $collation->toggle_reading( 'n22' ); |
116 | splice( @expected_nodes, 16, 1, ( [ "n21", 0 ], [ "n22", 1 ] ) ); |
de51424a |
117 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
118 | subtest 'Turned on other node in that location' => \&compare_active; |
3265b0ce |
119 | $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced to ... ... #'; |
b49c4318 |
120 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
121 | |
122 | # Test the toggling effects of transposition |
de51424a |
123 | @off = $collation->toggle_reading( 'n14' ); |
b49c4318 |
124 | # Add the turned on node |
c557b209 |
125 | $expected_nodes[ 11 ] = [ "n14", 1 ]; |
58a3c424 |
126 | # Remove the 'off' for the previous node |
3265b0ce |
127 | splice( @expected_nodes, 16, 1 ); |
de51424a |
128 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
129 | subtest 'Turned on transposition node' => \&compare_active; |
3265b0ce |
130 | $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #'; |
b49c4318 |
131 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
132 | |
de51424a |
133 | @off = $collation->toggle_reading( 'n18' ); |
58a3c424 |
134 | # Toggle on the new node |
c557b209 |
135 | $expected_nodes[ 13 ] = [ "n18", 1 ]; |
58a3c424 |
136 | # Toggle off the transposed node |
c557b209 |
137 | $expected_nodes[ 11 ] = [ "n14", undef ]; |
de51424a |
138 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
139 | subtest 'Turned on that node\'s partner' => \&compare_active; |
3265b0ce |
140 | $string = '# when ... ... ... showers sweet with ... fruit ... ... of drought has pierced to ... ... #'; |
b49c4318 |
141 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
142 | |
de51424a |
143 | @off = $collation->toggle_reading( 'n14' ); |
58a3c424 |
144 | # Toggle on the new node |
c557b209 |
145 | $expected_nodes[ 11 ] = [ "n14", 1 ]; |
58a3c424 |
146 | # Toggle off the transposed node |
c557b209 |
147 | $expected_nodes[ 13 ] = [ "n18", undef ]; |
de51424a |
148 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
149 | subtest 'Turned on the original node' => \&compare_active; |
3265b0ce |
150 | $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #'; |
b49c4318 |
151 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
152 | |
de51424a |
153 | @off = $collation->toggle_reading( 'n15' ); |
58a3c424 |
154 | # Toggle on the new node, and off with the old |
c557b209 |
155 | splice( @expected_nodes, 11, 1, [ "n14", 0 ], [ "n15", 1 ] ); |
de51424a |
156 | @active_nodes = $collation->lemma_readings( @off ); |
58a3c424 |
157 | subtest 'Turned on the colocated node' => \&compare_active; |
3265b0ce |
158 | $string = '# when ... ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #'; |
58a3c424 |
159 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
160 | |
de51424a |
161 | @off = $collation->toggle_reading( 'n3' ); |
58a3c424 |
162 | # Toggle on the new node |
c557b209 |
163 | splice( @expected_nodes, 3, 1, [ "n3", 1 ] ); |
58a3c424 |
164 | # Remove the old toggle-off |
c557b209 |
165 | splice( @expected_nodes, 11, 1 ); |
de51424a |
166 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
167 | subtest 'Turned on a singleton node' => \&compare_active; |
3265b0ce |
168 | $string = '# when ... with ... showers sweet with ... fruit ... march of ... has pierced to ... ... #'; |
b49c4318 |
169 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
170 | |
de51424a |
171 | @off = $collation->toggle_reading( 'n3' ); |
58a3c424 |
172 | # Toggle off this node |
c557b209 |
173 | splice( @expected_nodes, 3, 1, [ "n3", 0 ] ); |
de51424a |
174 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
175 | subtest 'Turned off a singleton node' => \&compare_active; |
3265b0ce |
176 | $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #'; |
58a3c424 |
177 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
178 | |
de51424a |
179 | @off = $collation->toggle_reading( 'n21' ); |
3265b0ce |
180 | splice( @expected_nodes, 16, 1, ["n22", 0 ], [ "n21", 1 ] ); |
de51424a |
181 | @active_nodes = $collation->lemma_readings( @off ); |
3265b0ce |
182 | subtest 'Turned on another node after singleton switchoff' => \&compare_active; |
183 | $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto ... ... #'; |
b49c4318 |
184 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
185 | |
b15511bf |
186 | # Now start testing some position identifiers |
187 | # 2. 'april with his' have no colocated |
188 | # 3. 'april' 2 has no colocated |
189 | # 4. 'teh' and 'the' |
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' |
196 | |
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" ); |
200 | } |
201 | |
202 | my %expected_colocations = ( |
203 | 'n2' => [], # april |
204 | 'n3' => [], # with |
205 | 'n4' => [], # his |
206 | 'n11' => [], # april |
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 |
4cdd82f1 |
213 | 'n21' => [ 'n22', 'n9' ], # unto -> to, teh |
214 | 'n22' => [ 'n21', 'n9' ], # to -> unto, teh |
b15511bf |
215 | 'n9' => [ 'n21', 'n22', 'n23' ], # teh -> unto, to, the |
4cdd82f1 |
216 | 'n23' => [ 'n25', 'n9' ], # the -> teh, rood |
217 | 'n25' => [ 'n23', 'n26' ], # rood -> the, root |
b15511bf |
218 | 'n26' => [ 'n25' ], # root -> rood |
219 | ); |
220 | |
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" ); |
225 | } |
226 | |
4cdd82f1 |
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'} = []; |
234 | |
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" ); |
239 | } |
240 | |
241 | # Test turning on, then off, an annoyingly overlapping node |
242 | |
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" ); |
251 | |
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" ); |
258 | |
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" ); |
265 | |
266 | ### Now test relationship madness. |
267 | |
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" ); |
275 | |
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')) ); |
280 | |
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" ); |
288 | |
289 | ( $result, @relations ) = $collation->add_relationship( 'n15', 'n9', {'type' => 'lexical'} ); # bogus march -> teh |
290 | ok( !$result, "Refused to add skewed relationship: " . $relations[0] ); |
291 | |
292 | ( $result, @relations ) = $collation->add_relationship( 'n25', 'n26', {'type' => 'spelling'} ); # root -> rood |
293 | ok( !$result, "Refused to add dupe relationship: " . $relations[0] ); |
294 | |
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" ); |
302 | |
b49c4318 |
303 | done_testing(); |