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