split stemma lib into util and object; make phylip_input microservice
[scpubgit/stemmatology.git] / t / stemma.t
index 95b25e3..6f3e7db 100644 (file)
@@ -1,9 +1,11 @@
 #!/usr/bin/perl
 
 use strict; use warnings;
+use File::Which;
 use Test::More;
 use lib 'lib';
 use Text::Tradition;
+use Text::Tradition::StemmaUtil qw/ phylip_pars_input /;
 use XML::LibXML;
 use XML::LibXML::XPathContext;
 
@@ -28,35 +30,44 @@ ok( $stemma->isa( 'Text::Tradition::Stemma' ), 'Got the right sort of object' );
 is( $stemma->graph, '1-2,1-A,2-B,2-C', "Got the correct graph" );
 
 # Test for character matrix creation
-my $m = $stemma->make_character_matrix();
+my $m = phylip_pars_input( $c->make_alignment_table() );
  ## check number of rows
-is( scalar @$m, 3, "Found three witnesses in char matrix" );
- ## check number of columns
-is( scalar( @{$m->[0]} ), 19, "Found 18 rows plus sigla in char matrix" );
- ## check matrix
-my %expected = (
-       'A' => 'AAAAAAAXAAAAAAAAAA',
-       'B' => 'AXXXAAAAAABABAABAA',
-       'C' => 'AXXXAAAAABAAAAAXBB',
-       );
-my @wits = map { shift @$_; } @$m;
-map { s/\s+//g } @wits;
-foreach my $i ( 0 .. $#wits ) {
-       my $w = $wits[$i];
-       is( join( '', @{$m->[$i]} ), $expected{$w}, "Row for witness $w is correct" );
-}
+my $expected = "\t3\t18\n";
+$expected .= 'A         AAAAAAAXAAAAAAAAAA
+B         AXXXAAAAAABABAABAA
+C         AXXXAAAAABAAAAAXBB';
+$expected .= "\n";
+is( $m, $expected, "Got the right pars input" );
+# is( scalar @$m, 3, "Found three witnesses in char matrix" );
+#  ## check number of columns
+# is( scalar( @{$m->[0]} ), 19, "Found 18 rows plus sigla in char matrix" );
+#  ## check matrix
+# my %expected = (
+#      'A' => 'AAAAAAAXAAAAAAAAAA',
+#      'B' => 'AXXXAAAAAABABAABAA',
+#      'C' => 'AXXXAAAAABAAAAAXBB',
+#      );
+# my @wits = map { shift @$_; } @$m;
+# map { s/\s+//g } @wits;
+# foreach my $i ( 0 .. $#wits ) {
+#      my $w = $wits[$i];
+#      is( join( '', @{$m->[$i]} ), $expected{$w}, "Row for witness $w is correct" );
+# }
 
 # Test that pars runs
-my( $status, $tree ) = $stemma->run_phylip_pars();
-ok( $status, "pars ran successfully" );
-print STDERR "Error was $tree\n" unless $status;
-
-# Test that we get a tree
-is( scalar @{$stemma->distance_trees}, 1, "Got a single tree" );
-# Test that the tree has all our witnesses
-$tree = $stemma->distance_trees->[0];
-my @leaves = grep { $tree->degree( $_ ) == 1 } $tree->vertices;
-is( scalar @leaves, 3, "All witnesses in the tree" );
+SKIP: {
+    skip "pars not in path", 3 unless File::Which::which('pars');
+    my( $status, $tree ) = $stemma->run_phylip_pars();
+    ok( $status, "pars ran successfully" );
+    print STDERR "Error was $tree\n" unless $status;
+    
+    # Test that we get a tree
+    is( scalar @{$stemma->distance_trees}, 1, "Got a single tree" );
+    # Test that the tree has all our witnesses
+    $tree = $stemma->distance_trees->[0];
+    my @leaves = grep { $tree->degree( $_ ) == 1 } $tree->vertices;
+    is( scalar @leaves, 3, "All witnesses in the tree" );
+}
 
 # Test our dot output
 my $display = $stemma->as_dot();
@@ -67,4 +78,4 @@ my $editable = $stemma->editable();
 ok( $editable =~ /digraph/, "Got a dot edit graph" );
 ok( $editable =~ /hypothetical/, "Graph contains an edit class" );
 
-done_testing();
\ No newline at end of file
+done_testing();