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