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