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 | |
12 | open( GRAPHFILE, $datafile ) or die "Could not open $datafile"; |
13 | my @lines = <GRAPHFILE>; |
14 | close GRAPHFILE; |
8e1394aa |
15 | my $tradition = Text::Tradition->new( 'GraphML' => join( '', @lines ) ); |
16 | my $collation = $tradition->collation; |
b49c4318 |
17 | |
18 | # Test the svg creation |
19 | my $parser = XML::LibXML->new(); |
20 | $parser->load_ext_dtd( 0 ); |
8e1394aa |
21 | my $svg = $parser->parse_string( $collation->as_svg() ); |
b49c4318 |
22 | is( $svg->documentElement->nodeName(), 'svg', 'Got an svg document' ); |
23 | |
24 | # Test for the correct number of nodes in the SVG |
25 | my $svg_xpc = XML::LibXML::XPathContext->new( $svg->documentElement() ); |
26 | $svg_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' ); |
27 | my @svg_nodes = $svg_xpc->findnodes( '//svg:g[@class="node"]' ); |
3265b0ce |
28 | is( scalar @svg_nodes, 26, "Correct number of nodes in the graph" ); |
b49c4318 |
29 | |
30 | # Test for the correct number of edges |
31 | my @svg_edges = $svg_xpc->findnodes( '//svg:g[@class="edge"]' ); |
3265b0ce |
32 | is( scalar @svg_edges, 32, "Correct number of edges in the graph" ); |
b49c4318 |
33 | |
34 | # Test for the correct common nodes |
3265b0ce |
35 | my @expected_nodes = map { [ $_, 1 ] } qw/ #START# n1 n5 n6 n7 n12 |
36 | n16 n19 n20 n27 /; |
37 | foreach my $idx ( qw/2 3 4 8 10 11 13 16 17 18/ ) { |
b49c4318 |
38 | splice( @expected_nodes, $idx, 0, [ "node_null", undef ] ); |
39 | } |
3a1f2523 |
40 | my @active_nodes = $collation->lemma_readings(); |
b49c4318 |
41 | subtest 'Initial common points' => \&compare_active; |
3265b0ce |
42 | my $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced ... ... ... #'; |
b49c4318 |
43 | is( make_text( @active_nodes ), $string, "Got the right starting text" ); |
44 | |
45 | sub compare_active { |
46 | is( scalar( @active_nodes ), scalar ( @expected_nodes ), |
47 | "Arrays are same length" ); |
48 | |
49 | foreach ( 0 .. scalar(@active_nodes)-1 ) { |
50 | is( $active_nodes[$_]->[1], $expected_nodes[$_]->[1], |
51 | "Element has same toggle value" ); |
52 | if( defined $active_nodes[$_]->[1] ) { |
53 | is( $active_nodes[$_]->[0], $expected_nodes[$_]->[0], |
de51424a |
54 | "Active or toggled element has same node name " |
55 | . $active_nodes[$_]->[0] ); |
b49c4318 |
56 | } |
57 | } |
58 | } |
59 | |
60 | sub make_text { |
61 | my @words; |
62 | foreach my $n ( @_ ) { |
63 | if( $n->[1] ) { |
3a1f2523 |
64 | push( @words, $collation->reading( $n->[0] )->label ); |
b49c4318 |
65 | } elsif ( !defined $n->[1] ) { |
66 | push( @words, '...' ); |
67 | } |
68 | } |
69 | return join( ' ', @words ); |
70 | } |
71 | |
72 | # Test the manuscript paths |
73 | my $wit_a = '# when april with his showers sweet with fruit the drought of march has pierced unto the root #'; |
74 | my $wit_b = '# when showers sweet with april fruit the march of drought has pierced to the root #'; |
3265b0ce |
75 | my $wit_c = '# when showers sweet with april fruit teh drought of march has pierced teh rood #'; |
de51424a |
76 | is( join( ' ', @{$tradition->witness( "A" )->text} ), $wit_a, "Correct path for witness A" ); |
77 | is( join( ' ', @{$tradition->witness( "B" )->text} ), $wit_b, "Correct path for witness B" ); |
78 | is( join( ' ', @{$tradition->witness( "C" )->text} ), $wit_c, "Correct path for witness C" ); |
b49c4318 |
79 | |
80 | # Test the transposition identifiers |
c557b209 |
81 | my $transposition_pools = [ [ 'n2', 'n11' ], [ 'n14', 'n18' ], |
82 | [ 'n17', 'n15' ] ]; |
83 | my $transposed_nodes = { 'n2' => $transposition_pools->[0], |
84 | 'n11' => $transposition_pools->[0], |
85 | 'n14' => $transposition_pools->[1], |
86 | 'n15' => $transposition_pools->[2], |
87 | 'n17' => $transposition_pools->[2], |
88 | 'n18' => $transposition_pools->[1], |
b49c4318 |
89 | }; |
de51424a |
90 | |
91 | my $real_transposed_nodes = {}; |
92 | foreach my $r ( $collation->readings ) { |
93 | my @same = map { $_->name } @{$r->same_as}; |
94 | $real_transposed_nodes->{ $r->name } = \@same if @same > 1; |
c2d16875 |
95 | } |
de51424a |
96 | |
97 | is_deeply( $real_transposed_nodes, $transposed_nodes, "Found the right transpositions" ); |
b49c4318 |
98 | |
99 | # Test turning on a node |
3265b0ce |
100 | my @off = $collation->toggle_reading( 'n21' ); |
101 | $expected_nodes[ 16 ] = [ "n21", 1 ]; |
de51424a |
102 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
103 | subtest 'Turned on node for new location' => \&compare_active; |
3265b0ce |
104 | $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced unto ... ... #'; |
b49c4318 |
105 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
106 | |
107 | # Test the toggling effects of same-column |
3265b0ce |
108 | @off = $collation->toggle_reading( 'n22' ); |
109 | splice( @expected_nodes, 16, 1, ( [ "n21", 0 ], [ "n22", 1 ] ) ); |
de51424a |
110 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
111 | subtest 'Turned on other node in that location' => \&compare_active; |
3265b0ce |
112 | $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced to ... ... #'; |
b49c4318 |
113 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
114 | |
115 | # Test the toggling effects of transposition |
116 | |
de51424a |
117 | @off = $collation->toggle_reading( 'n14' ); |
b49c4318 |
118 | # Add the turned on node |
c557b209 |
119 | $expected_nodes[ 11 ] = [ "n14", 1 ]; |
58a3c424 |
120 | # Remove the 'off' for the previous node |
3265b0ce |
121 | splice( @expected_nodes, 16, 1 ); |
de51424a |
122 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
123 | subtest 'Turned on transposition node' => \&compare_active; |
3265b0ce |
124 | $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #'; |
b49c4318 |
125 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
126 | |
de51424a |
127 | @off = $collation->toggle_reading( 'n18' ); |
58a3c424 |
128 | # Toggle on the new node |
c557b209 |
129 | $expected_nodes[ 13 ] = [ "n18", 1 ]; |
58a3c424 |
130 | # Toggle off the transposed node |
c557b209 |
131 | $expected_nodes[ 11 ] = [ "n14", undef ]; |
de51424a |
132 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
133 | subtest 'Turned on that node\'s partner' => \&compare_active; |
3265b0ce |
134 | $string = '# when ... ... ... showers sweet with ... fruit ... ... of drought has pierced to ... ... #'; |
b49c4318 |
135 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
136 | |
de51424a |
137 | @off = $collation->toggle_reading( 'n14' ); |
58a3c424 |
138 | # Toggle on the new node |
c557b209 |
139 | $expected_nodes[ 11 ] = [ "n14", 1 ]; |
58a3c424 |
140 | # Toggle off the transposed node |
c557b209 |
141 | $expected_nodes[ 13 ] = [ "n18", undef ]; |
de51424a |
142 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
143 | subtest 'Turned on the original node' => \&compare_active; |
3265b0ce |
144 | $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #'; |
b49c4318 |
145 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
146 | |
de51424a |
147 | @off = $collation->toggle_reading( 'n15' ); |
58a3c424 |
148 | # Toggle on the new node, and off with the old |
c557b209 |
149 | splice( @expected_nodes, 11, 1, [ "n14", 0 ], [ "n15", 1 ] ); |
de51424a |
150 | @active_nodes = $collation->lemma_readings( @off ); |
58a3c424 |
151 | subtest 'Turned on the colocated node' => \&compare_active; |
3265b0ce |
152 | $string = '# when ... ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #'; |
58a3c424 |
153 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
154 | |
de51424a |
155 | @off = $collation->toggle_reading( 'n3' ); |
58a3c424 |
156 | # Toggle on the new node |
c557b209 |
157 | splice( @expected_nodes, 3, 1, [ "n3", 1 ] ); |
58a3c424 |
158 | # Remove the old toggle-off |
c557b209 |
159 | splice( @expected_nodes, 11, 1 ); |
de51424a |
160 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
161 | subtest 'Turned on a singleton node' => \&compare_active; |
3265b0ce |
162 | $string = '# when ... with ... showers sweet with ... fruit ... march of ... has pierced to ... ... #'; |
b49c4318 |
163 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
164 | |
de51424a |
165 | @off = $collation->toggle_reading( 'n3' ); |
58a3c424 |
166 | # Toggle off this node |
c557b209 |
167 | splice( @expected_nodes, 3, 1, [ "n3", 0 ] ); |
de51424a |
168 | @active_nodes = $collation->lemma_readings( @off ); |
b49c4318 |
169 | subtest 'Turned off a singleton node' => \&compare_active; |
3265b0ce |
170 | $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #'; |
58a3c424 |
171 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
172 | |
de51424a |
173 | @off = $collation->toggle_reading( 'n21' ); |
3265b0ce |
174 | splice( @expected_nodes, 16, 1, ["n22", 0 ], [ "n21", 1 ] ); |
de51424a |
175 | @active_nodes = $collation->lemma_readings( @off ); |
3265b0ce |
176 | subtest 'Turned on another node after singleton switchoff' => \&compare_active; |
177 | $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto ... ... #'; |
b49c4318 |
178 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
179 | |
180 | done_testing(); |