3 use strict; use warnings;
8 use XML::LibXML::XPathContext;
10 my $datafile = 't/data/Collatex-16.xml';
12 open( GRAPHFILE, $datafile ) or die "Could not open $datafile";
13 my @lines = <GRAPHFILE>;
15 my $tradition = Text::Tradition->new( 'GraphML' => join( '', @lines ) );
16 my $collation = $tradition->collation;
18 # Test the svg creation
19 my $parser = XML::LibXML->new();
20 $parser->load_ext_dtd( 0 );
21 my $svg = $parser->parse_string( $collation->as_svg() );
22 is( $svg->documentElement->nodeName(), 'svg', 'Got an svg document' );
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"]' );
28 is( scalar @svg_nodes, 26, "Correct number of nodes in the graph" );
30 # Test for the correct number of edges
31 my @svg_edges = $svg_xpc->findnodes( '//svg:g[@class="edge"]' );
32 is( scalar @svg_edges, 32, "Correct number of edges in the graph" );
34 # Test for the correct common nodes
35 my @expected_nodes = map { [ $_, 1 ] } qw/ #START# n1 n5 n6 n7 n12
37 foreach my $idx ( qw/2 3 4 8 10 11 13 16 17 18/ ) {
38 splice( @expected_nodes, $idx, 0, [ "node_null", undef ] );
40 my @active_nodes = $collation->lemma_readings();
41 subtest 'Initial common points' => \&compare_active;
42 my $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced ... ... ... #';
43 is( make_text( @active_nodes ), $string, "Got the right starting text" );
46 is( scalar( @active_nodes ), scalar ( @expected_nodes ),
47 "Arrays are same length" );
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],
54 "Active or toggled element has same node name "
55 . $active_nodes[$_]->[0] );
62 foreach my $n ( @_ ) {
64 push( @words, $collation->reading( $n->[0] )->label );
65 } elsif ( !defined $n->[1] ) {
66 push( @words, '...' );
69 return join( ' ', @words );
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 #';
75 my $wit_c = '# when showers sweet with april fruit teh drought of march has pierced teh rood #';
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" );
80 # Test the transposition identifiers
81 my $transposition_pools = [ [ 'n2', 'n11' ], [ 'n14', 'n18' ],
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],
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;
97 is_deeply( $real_transposed_nodes, $transposed_nodes, "Found the right transpositions" );
99 # Test turning on a node
100 my @off = $collation->toggle_reading( 'n21' );
101 $expected_nodes[ 16 ] = [ "n21", 1 ];
102 @active_nodes = $collation->lemma_readings( @off );
103 subtest 'Turned on node for new location' => \&compare_active;
104 $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced unto ... ... #';
105 is( make_text( @active_nodes ), $string, "Got the right text" );
107 # Test the toggling effects of same-column
108 @off = $collation->toggle_reading( 'n22' );
109 splice( @expected_nodes, 16, 1, ( [ "n21", 0 ], [ "n22", 1 ] ) );
110 @active_nodes = $collation->lemma_readings( @off );
111 subtest 'Turned on other node in that location' => \&compare_active;
112 $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced to ... ... #';
113 is( make_text( @active_nodes ), $string, "Got the right text" );
115 # Test the toggling effects of transposition
117 @off = $collation->toggle_reading( 'n14' );
118 # Add the turned on node
119 $expected_nodes[ 11 ] = [ "n14", 1 ];
120 # Remove the 'off' for the previous node
121 splice( @expected_nodes, 16, 1 );
122 @active_nodes = $collation->lemma_readings( @off );
123 subtest 'Turned on transposition node' => \&compare_active;
124 $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #';
125 is( make_text( @active_nodes ), $string, "Got the right text" );
127 @off = $collation->toggle_reading( 'n18' );
128 # Toggle on the new node
129 $expected_nodes[ 13 ] = [ "n18", 1 ];
130 # Toggle off the transposed node
131 $expected_nodes[ 11 ] = [ "n14", undef ];
132 @active_nodes = $collation->lemma_readings( @off );
133 subtest 'Turned on that node\'s partner' => \&compare_active;
134 $string = '# when ... ... ... showers sweet with ... fruit ... ... of drought has pierced to ... ... #';
135 is( make_text( @active_nodes ), $string, "Got the right text" );
137 @off = $collation->toggle_reading( 'n14' );
138 # Toggle on the new node
139 $expected_nodes[ 11 ] = [ "n14", 1 ];
140 # Toggle off the transposed node
141 $expected_nodes[ 13 ] = [ "n18", undef ];
142 @active_nodes = $collation->lemma_readings( @off );
143 subtest 'Turned on the original node' => \&compare_active;
144 $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #';
145 is( make_text( @active_nodes ), $string, "Got the right text" );
147 @off = $collation->toggle_reading( 'n15' );
148 # Toggle on the new node, and off with the old
149 splice( @expected_nodes, 11, 1, [ "n14", 0 ], [ "n15", 1 ] );
150 @active_nodes = $collation->lemma_readings( @off );
151 subtest 'Turned on the colocated node' => \&compare_active;
152 $string = '# when ... ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
153 is( make_text( @active_nodes ), $string, "Got the right text" );
155 @off = $collation->toggle_reading( 'n3' );
156 # Toggle on the new node
157 splice( @expected_nodes, 3, 1, [ "n3", 1 ] );
158 # Remove the old toggle-off
159 splice( @expected_nodes, 11, 1 );
160 @active_nodes = $collation->lemma_readings( @off );
161 subtest 'Turned on a singleton node' => \&compare_active;
162 $string = '# when ... with ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
163 is( make_text( @active_nodes ), $string, "Got the right text" );
165 @off = $collation->toggle_reading( 'n3' );
166 # Toggle off this node
167 splice( @expected_nodes, 3, 1, [ "n3", 0 ] );
168 @active_nodes = $collation->lemma_readings( @off );
169 subtest 'Turned off a singleton node' => \&compare_active;
170 $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
171 is( make_text( @active_nodes ), $string, "Got the right text" );
173 @off = $collation->toggle_reading( 'n21' );
174 splice( @expected_nodes, 16, 1, ["n22", 0 ], [ "n21", 1 ] );
175 @active_nodes = $collation->lemma_readings( @off );
176 subtest 'Turned on another node after singleton switchoff' => \&compare_active;
177 $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto ... ... #';
178 is( make_text( @active_nodes ), $string, "Got the right text" );