Get rid of Graph::Easy; add stemma tests
[scpubgit/stemmatology.git] / t / stemma.t
CommitLineData
6f4946fb 1#!/usr/bin/perl
2
3use strict; use warnings;
4use Test::More;
5use lib 'lib';
6use Text::Tradition;
6f4946fb 7use XML::LibXML;
8use XML::LibXML::XPathContext;
9
10my $datafile = 't/data/Collatex-16.xml'; #TODO need other test data
11
7035e3a6 12my $tradition = Text::Tradition->new(
13 'name' => 'inline',
14 'input' => 'CollateX',
15 'file' => $datafile,
16 );
7a7c249c 17# Set up some relationships
18my $c = $tradition->collation;
19$c->add_relationship( 'n25', 'n26', { 'type' => 'spelling' } );
20$c->add_relationship( 'n9', 'n23', { 'type' => 'spelling' } );
21$c->add_relationship( 'n8', 'n13', { 'type' => 'spelling' } );
22$c->calculate_ranks();
23
24my $stemma = $tradition->add_stemma( 't/data/simple.dot' );
6f4946fb 25
26# Test for object creation
27ok( $stemma->isa( 'Text::Tradition::Stemma' ), 'Got the right sort of object' );
7a7c249c 28is( $stemma->graph, '1-2,1-A,2-B,2-C', "Got the correct graph" );
6f4946fb 29
30# Test for character matrix creation
7a7c249c 31my $m = $stemma->make_character_matrix();
6f4946fb 32 ## check number of rows
7a7c249c 33is( scalar @$m, 3, "Found three witnesses in char matrix" );
6f4946fb 34 ## check number of columns
7a7c249c 35is( scalar( @{$m->[0]} ), 19, "Found 18 rows plus sigla in char matrix" );
36 ## check matrix
37my %expected = (
38 'A' => 'AAAAAAAXAAAAAAAAAA',
39 'B' => 'AXXXAAAAAABABAABAA',
40 'C' => 'AXXXAAAAABAAAAAXBB',
41 );
42my @wits = map { shift @$_; } @$m;
43map { s/\s+//g } @wits;
44foreach my $i ( 0 .. $#wits ) {
45 my $w = $wits[$i];
46 is( join( '', @{$m->[$i]} ), $expected{$w}, "Row for witness $w is correct" );
47}
6f4946fb 48
49# Test that pars runs
7035e3a6 50my( $status, $tree ) = $stemma->run_phylip_pars();
6f4946fb 51ok( $status, "pars ran successfully" );
52print STDERR "Error was $tree\n" unless $status;
53
54# Test that we get a tree
7a7c249c 55is( scalar @{$stemma->distance_trees}, 1, "Got a single tree" );
6f4946fb 56# Test that the tree has all our witnesses
7a7c249c 57$tree = $stemma->distance_trees->[0];
58my @leaves = grep { $tree->degree( $_ ) == 1 } $tree->vertices;
59is( scalar @leaves, 3, "All witnesses in the tree" );
60
61# Test our dot output
62my $display = $stemma->as_dot();
63ok( $display =~ /digraph/, "Got a dot display graph" );
64ok( $display !~ /hypothetical/, "Graph is display rather than edit" );
65# Test our editable output
66my $editable = $stemma->editable();
67ok( $editable =~ /digraph/, "Got a dot edit graph" );
68ok( $editable =~ /hypothetical/, "Graph contains an edit class" );
7035e3a6 69
70done_testing();