split out XML parsing logic from CollateX logic in parser
[scpubgit/stemmatology.git] / t / graph.t
1 #!/usr/bin/perl
2
3 use strict; use warnings;
4 use Test::More;
5 use lib 'lib';
6 use Text::Tradition;
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;
15 my $tradition = Text::Tradition->new( 'CollateX' => join( '', @lines ) );
16 my $collation = $tradition->collation;
17
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' );
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"]' );
28 is( scalar @svg_nodes, 26, "Correct number of nodes in the graph" );
29
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" );
33
34 # Test for the correct common nodes
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/ ) {
38     splice( @expected_nodes, $idx, 0, [ "node_null", undef ] );
39 }
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" );
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], 
54                 "Active or toggled element has same node name " 
55                 . $active_nodes[$_]->[0] );
56         }
57     }
58 }
59
60 sub make_text {
61     my @words;
62     foreach my $n ( @_ ) {
63         if( $n->[1] ) {
64             push( @words, $collation->reading( $n->[0] )->label );
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 #';
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" );
79
80 # Test the transposition identifiers
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],
89 };
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;
95 }
96     
97 is_deeply( $real_transposed_nodes, $transposed_nodes, "Found the right transpositions" );
98
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" );
106  
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" );
114
115 # Test the toggling effects of transposition
116
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" );
126
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" );
136
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" );
146
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" );
154
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" );
164
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" );
172
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" );
179
180 done_testing();