Change namespace
[scpubgit/stemmatology.git] / t / graph.t
CommitLineData
b49c4318 1#!/usr/bin/perl
2
3use strict; use warnings;
4use Test::More;
5use lib 'lib';
a25d4374 6use Traditions::Graph;
b49c4318 7use XML::LibXML;
8use XML::LibXML::XPathContext;
9
10my $datafile = 't/data/Collatex-16.xml';
11
12open( GRAPHFILE, $datafile ) or die "Could not open $datafile";
13my @lines = <GRAPHFILE>;
14close GRAPHFILE;
a25d4374 15my $graph = Traditions::Graph->new( 'GraphML' => join( '', @lines ) );
b49c4318 16
17# Test the svg creation
18my $parser = XML::LibXML->new();
19$parser->load_ext_dtd( 0 );
20my $svg = $parser->parse_string( $graph->as_svg() );
21is( $svg->documentElement->nodeName(), 'svg', 'Got an svg document' );
22
23# Test for the correct number of nodes in the SVG
24my $svg_xpc = XML::LibXML::XPathContext->new( $svg->documentElement() );
25$svg_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
26my @svg_nodes = $svg_xpc->findnodes( '//svg:g[@class="node"]' );
27is( scalar @svg_nodes, 21, "Correct number of nodes in the graph" );
28
29# Test for the correct number of edges
30my @svg_edges = $svg_xpc->findnodes( '//svg:g[@class="edge"]' );
31is( scalar @svg_edges, 27, "Correct number of edges in the graph" );
32
33# Test for the correct common nodes
a25d4374 34my @expected_nodes = map { [ $_, 1 ] } qw/#START# 1 8 12 13 16 19 20 23 27/;
b49c4318 35foreach my $idx ( qw/2 3 5 8 10 13 15/ ) {
36 splice( @expected_nodes, $idx, 0, [ "node_null", undef ] );
37}
38my @active_nodes = $graph->active_nodes();
39# is_deeply( \@active_nodes, \@expected_nodes, "Initial common points" );
40subtest 'Initial common points' => \&compare_active;
41my $string = '# when ... ... showers sweet with ... fruit the ... of ... has pierced ... the ... #';
42is( make_text( @active_nodes ), $string, "Got the right starting text" );
43
44sub 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
58sub 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
71my $wit_a = '# when april with his showers sweet with fruit the drought of march has pierced unto the root #';
72my $wit_b = '# when showers sweet with april fruit the march of drought has pierced to the root #';
73my $wit_c = '# when showers sweet with april fruit the drought of march has pierced the rood #';
74is( $graph->text_for_witness( "A" ), $wit_a, "Correct path for witness A" );
75is( $graph->text_for_witness( "B" ), $wit_b, "Correct path for witness B" );
76is( $graph->text_for_witness( "C" ), $wit_c, "Correct path for witness C" );
77
78# Test the transposition identifiers
c2d16875 79my $transposition_pools = [ [ 2, 9 ], [ 14, 18 ], [ 15, 17 ] ];
80my $transposed_nodes = { 2 => $transposition_pools->[0],
81 9 => $transposition_pools->[0],
82 14 => $transposition_pools->[1],
83 15 => $transposition_pools->[2],
84 17 => $transposition_pools->[2],
85 18 => $transposition_pools->[1],
b49c4318 86};
c2d16875 87foreach my $n ( $graph->nodes() ) {
88 $transposed_nodes->{ $n->name() } = [ $n->name() ]
89 unless exists $transposed_nodes->{ $n->name() };
90}
a25d4374 91is_deeply( $graph->{'identical_nodes'}, $transposed_nodes, "Found the right transpositions" );
b49c4318 92
93# Test turning on a node
a25d4374 94my @off = $graph->toggle_node( '24' );
95$expected_nodes[ 15 ] = [ "24", 1 ];
b49c4318 96@active_nodes = $graph->active_nodes( @off );
97subtest 'Turned on node for new location' => \&compare_active;
98$string = '# when ... ... showers sweet with ... fruit the ... of ... has pierced ... the root #';
99is( make_text( @active_nodes ), $string, "Got the right text" );
100
101# Test the toggling effects of same-column
a25d4374 102@off = $graph->toggle_node( '26' );
58a3c424 103splice( @expected_nodes, 15, 1, ( [ "24", 0 ], [ "26", 1 ] ) );
b49c4318 104@active_nodes = $graph->active_nodes( @off );
105subtest 'Turned on other node in that location' => \&compare_active;
106$string = '# when ... ... showers sweet with ... fruit the ... of ... has pierced ... the rood #';
107is( make_text( @active_nodes ), $string, "Got the right text" );
108
109# Test the toggling effects of transposition
110
a25d4374 111@off = $graph->toggle_node( '14' );
b49c4318 112# Add the turned on node
58a3c424 113$expected_nodes[ 8 ] = [ "14", 1 ];
114# Remove the 'off' for the previous node
115splice( @expected_nodes, 15, 1 );
b49c4318 116@active_nodes = $graph->active_nodes( @off );
117subtest 'Turned on transposition node' => \&compare_active;
118$string = '# when ... ... showers sweet with ... fruit the drought of ... has pierced ... the rood #';
119is( make_text( @active_nodes ), $string, "Got the right text" );
120
a25d4374 121@off = $graph->toggle_node( '18' );
58a3c424 122# Toggle on the new node
123$expected_nodes[ 10 ] = [ "18", 1 ];
124# Toggle off the transposed node
125$expected_nodes[ 8 ] = [ "14", undef ];
b49c4318 126@active_nodes = $graph->active_nodes( @off );
127subtest 'Turned on that node\'s partner' => \&compare_active;
128$string = '# when ... ... showers sweet with ... fruit the ... of drought has pierced ... the rood #';
129is( make_text( @active_nodes ), $string, "Got the right text" );
130
a25d4374 131@off = $graph->toggle_node( '14' );
58a3c424 132# Toggle on the new node
133$expected_nodes[ 8 ] = [ "14", 1 ];
134# Toggle off the transposed node
135$expected_nodes[ 10 ] = [ "18", undef ];
b49c4318 136@active_nodes = $graph->active_nodes( @off );
137subtest 'Turned on the original node' => \&compare_active;
138$string = '# when ... ... showers sweet with ... fruit the drought of ... has pierced ... the rood #';
139is( make_text( @active_nodes ), $string, "Got the right text" );
140
58a3c424 141@off = $graph->toggle_node( '15' );
142# Toggle on the new node, and off with the old
143splice( @expected_nodes, 8, 1, [ "14", 0 ], [ "15", 1 ] );
144@active_nodes = $graph->active_nodes( @off );
145subtest 'Turned on the colocated node' => \&compare_active;
146$string = '# when ... ... showers sweet with ... fruit the march of ... has pierced ... the rood #';
147is( make_text( @active_nodes ), $string, "Got the right text" );
148
a25d4374 149@off = $graph->toggle_node( '3' );
58a3c424 150# Toggle on the new node
a25d4374 151splice( @expected_nodes, 3, 1, [ "3", 1 ] );
58a3c424 152# Remove the old toggle-off
b49c4318 153splice( @expected_nodes, 8, 1 );
154@active_nodes = $graph->active_nodes( @off );
155subtest 'Turned on a singleton node' => \&compare_active;
58a3c424 156$string = '# when ... with his showers sweet with ... fruit the march of ... has pierced ... the rood #';
b49c4318 157is( make_text( @active_nodes ), $string, "Got the right text" );
158
a25d4374 159@off = $graph->toggle_node( '3' );
58a3c424 160# Toggle off this node
a25d4374 161splice( @expected_nodes, 3, 1, [ "3", 0 ] );
b49c4318 162@active_nodes = $graph->active_nodes( @off );
163subtest 'Turned off a singleton node' => \&compare_active;
58a3c424 164$string = '# when ... showers sweet with ... fruit the march of ... has pierced ... the rood #';
165is( make_text( @active_nodes ), $string, "Got the right text" );
166
167@off = $graph->toggle_node( '21' );
168splice( @expected_nodes, 13, 1, [ "21", 1 ] );
169@active_nodes = $graph->active_nodes( @off );
170subtest 'Turned on a new node after singleton switchoff' => \&compare_active;
171$string = '# when ... showers sweet with ... fruit the march of ... has pierced unto the rood #';
b49c4318 172is( make_text( @active_nodes ), $string, "Got the right text" );
173
174done_testing();