Commit | Line | Data |
b49c4318 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; use warnings; |
4 | use Test::More; |
5 | use lib 'lib'; |
67ecdf37 |
6 | use Text::Tradition::Graph; |
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; |
67ecdf37 |
15 | my $graph = Text::Tradition::Graph->new( 'GraphML' => join( '', @lines ) ); |
b49c4318 |
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"]' ); |
c557b209 |
27 | is( scalar @svg_nodes, 24, "Correct number of nodes in the graph" ); |
b49c4318 |
28 | |
29 | # Test for the correct number of edges |
30 | my @svg_edges = $svg_xpc->findnodes( '//svg:g[@class="edge"]' ); |
c557b209 |
31 | is( scalar @svg_edges, 30, "Correct number of edges in the graph" ); |
b49c4318 |
32 | |
33 | # Test for the correct common nodes |
c557b209 |
34 | my @expected_nodes = map { [ $_, 1 ] } qw/ #START# n1 n5 n6 n7 n12 n13 |
35 | n16 n19 n20 n23 n27 /; |
36 | foreach my $idx ( qw/2 3 4 8 11 13 16 18/ ) { |
b49c4318 |
37 | splice( @expected_nodes, $idx, 0, [ "node_null", undef ] ); |
38 | } |
39 | my @active_nodes = $graph->active_nodes(); |
40 | # is_deeply( \@active_nodes, \@expected_nodes, "Initial common points" ); |
41 | subtest 'Initial common points' => \&compare_active; |
c557b209 |
42 | my $string = '# when ... ... ... showers sweet with ... fruit the ... of ... has pierced ... the ... #'; |
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], |
54 | "Active or toggled element has same node name" ); |
55 | } |
56 | } |
57 | } |
58 | |
59 | sub make_text { |
60 | my @words; |
61 | foreach my $n ( @_ ) { |
62 | if( $n->[1] ) { |
63 | push( @words, $graph->text_of_node( $n->[0] ) ); |
64 | } elsif ( !defined $n->[1] ) { |
65 | push( @words, '...' ); |
66 | } |
67 | } |
68 | return join( ' ', @words ); |
69 | } |
70 | |
71 | # Test the manuscript paths |
72 | my $wit_a = '# when april with his showers sweet with fruit the drought of march has pierced unto the root #'; |
73 | my $wit_b = '# when showers sweet with april fruit the march of drought has pierced to the root #'; |
74 | my $wit_c = '# when showers sweet with april fruit the drought of march has pierced the rood #'; |
75 | is( $graph->text_for_witness( "A" ), $wit_a, "Correct path for witness A" ); |
76 | is( $graph->text_for_witness( "B" ), $wit_b, "Correct path for witness B" ); |
77 | is( $graph->text_for_witness( "C" ), $wit_c, "Correct path for witness C" ); |
78 | |
79 | # Test the transposition identifiers |
c557b209 |
80 | my $transposition_pools = [ [ 'n2', 'n11' ], [ 'n14', 'n18' ], |
81 | [ 'n17', 'n15' ] ]; |
82 | my $transposed_nodes = { 'n2' => $transposition_pools->[0], |
83 | 'n11' => $transposition_pools->[0], |
84 | 'n14' => $transposition_pools->[1], |
85 | 'n15' => $transposition_pools->[2], |
86 | 'n17' => $transposition_pools->[2], |
87 | 'n18' => $transposition_pools->[1], |
b49c4318 |
88 | }; |
c2d16875 |
89 | foreach my $n ( $graph->nodes() ) { |
90 | $transposed_nodes->{ $n->name() } = [ $n->name() ] |
91 | unless exists $transposed_nodes->{ $n->name() }; |
92 | } |
a25d4374 |
93 | is_deeply( $graph->{'identical_nodes'}, $transposed_nodes, "Found the right transpositions" ); |
b49c4318 |
94 | |
95 | # Test turning on a node |
c557b209 |
96 | my @off = $graph->toggle_node( 'n25' ); |
97 | $expected_nodes[ 18 ] = [ "n25", 1 ]; |
b49c4318 |
98 | @active_nodes = $graph->active_nodes( @off ); |
99 | subtest 'Turned on node for new location' => \&compare_active; |
c557b209 |
100 | $string = '# when ... ... ... showers sweet with ... fruit the ... of ... has pierced ... the rood #'; |
b49c4318 |
101 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
102 | |
103 | # Test the toggling effects of same-column |
c557b209 |
104 | @off = $graph->toggle_node( 'n26' ); |
105 | splice( @expected_nodes, 18, 1, ( [ "n25", 0 ], [ "n26", 1 ] ) ); |
b49c4318 |
106 | @active_nodes = $graph->active_nodes( @off ); |
107 | subtest 'Turned on other node in that location' => \&compare_active; |
c557b209 |
108 | $string = '# when ... ... ... showers sweet with ... fruit the ... of ... has pierced ... the root #'; |
b49c4318 |
109 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
110 | |
111 | # Test the toggling effects of transposition |
112 | |
c557b209 |
113 | @off = $graph->toggle_node( 'n14' ); |
b49c4318 |
114 | # Add the turned on node |
c557b209 |
115 | $expected_nodes[ 11 ] = [ "n14", 1 ]; |
58a3c424 |
116 | # Remove the 'off' for the previous node |
c557b209 |
117 | splice( @expected_nodes, 18, 1 ); |
b49c4318 |
118 | @active_nodes = $graph->active_nodes( @off ); |
119 | subtest 'Turned on transposition node' => \&compare_active; |
c557b209 |
120 | $string = '# when ... ... ... showers sweet with ... fruit the drought of ... has pierced ... the root #'; |
b49c4318 |
121 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
122 | |
c557b209 |
123 | @off = $graph->toggle_node( 'n18' ); |
58a3c424 |
124 | # Toggle on the new node |
c557b209 |
125 | $expected_nodes[ 13 ] = [ "n18", 1 ]; |
58a3c424 |
126 | # Toggle off the transposed node |
c557b209 |
127 | $expected_nodes[ 11 ] = [ "n14", undef ]; |
b49c4318 |
128 | @active_nodes = $graph->active_nodes( @off ); |
129 | subtest 'Turned on that node\'s partner' => \&compare_active; |
c557b209 |
130 | $string = '# when ... ... ... showers sweet with ... fruit the ... of drought has pierced ... the root #'; |
b49c4318 |
131 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
132 | |
c557b209 |
133 | @off = $graph->toggle_node( 'n14' ); |
58a3c424 |
134 | # Toggle on the new node |
c557b209 |
135 | $expected_nodes[ 11 ] = [ "n14", 1 ]; |
58a3c424 |
136 | # Toggle off the transposed node |
c557b209 |
137 | $expected_nodes[ 13 ] = [ "n18", undef ]; |
b49c4318 |
138 | @active_nodes = $graph->active_nodes( @off ); |
139 | subtest 'Turned on the original node' => \&compare_active; |
c557b209 |
140 | $string = '# when ... ... ... showers sweet with ... fruit the drought of ... has pierced ... the root #'; |
b49c4318 |
141 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
142 | |
c557b209 |
143 | @off = $graph->toggle_node( 'n15' ); |
58a3c424 |
144 | # Toggle on the new node, and off with the old |
c557b209 |
145 | splice( @expected_nodes, 11, 1, [ "n14", 0 ], [ "n15", 1 ] ); |
58a3c424 |
146 | @active_nodes = $graph->active_nodes( @off ); |
147 | subtest 'Turned on the colocated node' => \&compare_active; |
c557b209 |
148 | $string = '# when ... ... ... showers sweet with ... fruit the march of ... has pierced ... the root #'; |
58a3c424 |
149 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
150 | |
c557b209 |
151 | @off = $graph->toggle_node( 'n3' ); |
58a3c424 |
152 | # Toggle on the new node |
c557b209 |
153 | splice( @expected_nodes, 3, 1, [ "n3", 1 ] ); |
58a3c424 |
154 | # Remove the old toggle-off |
c557b209 |
155 | splice( @expected_nodes, 11, 1 ); |
b49c4318 |
156 | @active_nodes = $graph->active_nodes( @off ); |
157 | subtest 'Turned on a singleton node' => \&compare_active; |
c557b209 |
158 | $string = '# when ... with ... showers sweet with ... fruit the march of ... has pierced ... the root #'; |
b49c4318 |
159 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
160 | |
c557b209 |
161 | @off = $graph->toggle_node( 'n3' ); |
58a3c424 |
162 | # Toggle off this node |
c557b209 |
163 | splice( @expected_nodes, 3, 1, [ "n3", 0 ] ); |
b49c4318 |
164 | @active_nodes = $graph->active_nodes( @off ); |
165 | subtest 'Turned off a singleton node' => \&compare_active; |
c557b209 |
166 | $string = '# when ... ... showers sweet with ... fruit the march of ... has pierced ... the root #'; |
58a3c424 |
167 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
168 | |
c557b209 |
169 | @off = $graph->toggle_node( 'n21' ); |
170 | splice( @expected_nodes, 16, 1, [ "n21", 1 ] ); |
58a3c424 |
171 | @active_nodes = $graph->active_nodes( @off ); |
172 | subtest 'Turned on a new node after singleton switchoff' => \&compare_active; |
c557b209 |
173 | $string = '# when ... ... showers sweet with ... fruit the march of ... has pierced unto the root #'; |
b49c4318 |
174 | is( make_text( @active_nodes ), $string, "Got the right text" ); |
175 | |
176 | done_testing(); |