BROKEN - making lemmatization work
[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],
54 "Active or toggled element has same node name" );
55 }
56 }
57}
58
59sub make_text {
60 my @words;
61 foreach my $n ( @_ ) {
62 if( $n->[1] ) {
3a1f2523 63 push( @words, $collation->reading( $n->[0] )->label );
b49c4318 64 } elsif ( !defined $n->[1] ) {
65 push( @words, '...' );
66 }
67 }
68 return join( ' ', @words );
69}
70
3a1f2523 71__END__
72
b49c4318 73# Test the manuscript paths
74my $wit_a = '# when april with his showers sweet with fruit the drought of march has pierced unto the root #';
75my $wit_b = '# when showers sweet with april fruit the march of drought has pierced to the root #';
76my $wit_c = '# when showers sweet with april fruit the drought of march has pierced the rood #';
8e1394aa 77is( $collation->text_for_witness( "A" ), $wit_a, "Correct path for witness A" );
78is( $collation->text_for_witness( "B" ), $wit_b, "Correct path for witness B" );
79is( $collation->text_for_witness( "C" ), $wit_c, "Correct path for witness C" );
b49c4318 80
81# Test the transposition identifiers
c557b209 82my $transposition_pools = [ [ 'n2', 'n11' ], [ 'n14', 'n18' ],
83 [ 'n17', 'n15' ] ];
84my $transposed_nodes = { 'n2' => $transposition_pools->[0],
85 'n11' => $transposition_pools->[0],
86 'n14' => $transposition_pools->[1],
87 'n15' => $transposition_pools->[2],
88 'n17' => $transposition_pools->[2],
89 'n18' => $transposition_pools->[1],
b49c4318 90};
8e1394aa 91foreach my $n ( $collation->readings() ) {
c2d16875 92 $transposed_nodes->{ $n->name() } = [ $n->name() ]
93 unless exists $transposed_nodes->{ $n->name() };
94}
8e1394aa 95is_deeply( $collation->{'identical_nodes'}, $transposed_nodes, "Found the right transpositions" );
b49c4318 96
97# Test turning on a node
8e1394aa 98my @off = $collation->toggle_node( 'n25' );
c557b209 99$expected_nodes[ 18 ] = [ "n25", 1 ];
8e1394aa 100@active_nodes = $collation->active_nodes( @off );
b49c4318 101subtest 'Turned on node for new location' => \&compare_active;
c557b209 102$string = '# when ... ... ... showers sweet with ... fruit the ... of ... has pierced ... the rood #';
b49c4318 103is( make_text( @active_nodes ), $string, "Got the right text" );
104
105# Test the toggling effects of same-column
8e1394aa 106@off = $collation->toggle_node( 'n26' );
c557b209 107splice( @expected_nodes, 18, 1, ( [ "n25", 0 ], [ "n26", 1 ] ) );
8e1394aa 108@active_nodes = $collation->active_nodes( @off );
b49c4318 109subtest 'Turned on other node in that location' => \&compare_active;
c557b209 110$string = '# when ... ... ... showers sweet with ... fruit the ... of ... has pierced ... the root #';
b49c4318 111is( make_text( @active_nodes ), $string, "Got the right text" );
112
113# Test the toggling effects of transposition
114
8e1394aa 115@off = $collation->toggle_node( 'n14' );
b49c4318 116# Add the turned on node
c557b209 117$expected_nodes[ 11 ] = [ "n14", 1 ];
58a3c424 118# Remove the 'off' for the previous node
c557b209 119splice( @expected_nodes, 18, 1 );
8e1394aa 120@active_nodes = $collation->active_nodes( @off );
b49c4318 121subtest 'Turned on transposition node' => \&compare_active;
c557b209 122$string = '# when ... ... ... showers sweet with ... fruit the drought of ... has pierced ... the root #';
b49c4318 123is( make_text( @active_nodes ), $string, "Got the right text" );
124
8e1394aa 125@off = $collation->toggle_node( 'n18' );
58a3c424 126# Toggle on the new node
c557b209 127$expected_nodes[ 13 ] = [ "n18", 1 ];
58a3c424 128# Toggle off the transposed node
c557b209 129$expected_nodes[ 11 ] = [ "n14", undef ];
8e1394aa 130@active_nodes = $collation->active_nodes( @off );
b49c4318 131subtest 'Turned on that node\'s partner' => \&compare_active;
c557b209 132$string = '# when ... ... ... showers sweet with ... fruit the ... of drought has pierced ... the root #';
b49c4318 133is( make_text( @active_nodes ), $string, "Got the right text" );
134
8e1394aa 135@off = $collation->toggle_node( 'n14' );
58a3c424 136# Toggle on the new node
c557b209 137$expected_nodes[ 11 ] = [ "n14", 1 ];
58a3c424 138# Toggle off the transposed node
c557b209 139$expected_nodes[ 13 ] = [ "n18", undef ];
8e1394aa 140@active_nodes = $collation->active_nodes( @off );
b49c4318 141subtest 'Turned on the original node' => \&compare_active;
c557b209 142$string = '# when ... ... ... showers sweet with ... fruit the drought of ... has pierced ... the root #';
b49c4318 143is( make_text( @active_nodes ), $string, "Got the right text" );
144
8e1394aa 145@off = $collation->toggle_node( 'n15' );
58a3c424 146# Toggle on the new node, and off with the old
c557b209 147splice( @expected_nodes, 11, 1, [ "n14", 0 ], [ "n15", 1 ] );
8e1394aa 148@active_nodes = $collation->active_nodes( @off );
58a3c424 149subtest 'Turned on the colocated node' => \&compare_active;
c557b209 150$string = '# when ... ... ... showers sweet with ... fruit the march of ... has pierced ... the root #';
58a3c424 151is( make_text( @active_nodes ), $string, "Got the right text" );
152
8e1394aa 153@off = $collation->toggle_node( 'n3' );
58a3c424 154# Toggle on the new node
c557b209 155splice( @expected_nodes, 3, 1, [ "n3", 1 ] );
58a3c424 156# Remove the old toggle-off
c557b209 157splice( @expected_nodes, 11, 1 );
8e1394aa 158@active_nodes = $collation->active_nodes( @off );
b49c4318 159subtest 'Turned on a singleton node' => \&compare_active;
c557b209 160$string = '# when ... with ... showers sweet with ... fruit the march of ... has pierced ... the root #';
b49c4318 161is( make_text( @active_nodes ), $string, "Got the right text" );
162
8e1394aa 163@off = $collation->toggle_node( 'n3' );
58a3c424 164# Toggle off this node
c557b209 165splice( @expected_nodes, 3, 1, [ "n3", 0 ] );
8e1394aa 166@active_nodes = $collation->active_nodes( @off );
b49c4318 167subtest 'Turned off a singleton node' => \&compare_active;
c557b209 168$string = '# when ... ... showers sweet with ... fruit the march of ... has pierced ... the root #';
58a3c424 169is( make_text( @active_nodes ), $string, "Got the right text" );
170
8e1394aa 171@off = $collation->toggle_node( 'n21' );
c557b209 172splice( @expected_nodes, 16, 1, [ "n21", 1 ] );
8e1394aa 173@active_nodes = $collation->active_nodes( @off );
58a3c424 174subtest 'Turned on a new node after singleton switchoff' => \&compare_active;
c557b209 175$string = '# when ... ... showers sweet with ... fruit the march of ... has pierced unto the root #';
b49c4318 176is( make_text( @active_nodes ), $string, "Got the right text" );
177
178done_testing();