add docs and test to Result class
[scpubgit/stemmatology.git] / t / text_tradition_analysis_result.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 $| = 1;
6
7
8
9 # =begin testing
10 {
11 use Test::More::UTF8;
12 use Text::Tradition;
13 use TryCatch;
14 use_ok( 'Text::Tradition::Analysis::Result' );
15
16 # Make a problem with a graph and a set of groupings
17
18 my $datafile = 't/data/florilegium_tei_ps.xml';
19 my $tradition = Text::Tradition->new( 'input' => 'TEI',
20                                       'name' => 'flortest',
21                                       'file' => $datafile );
22 my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
23
24 my $sets = [ [ qw/ D Q / ], [ qw/ F H / ], [ qw/ A B C P S T / ] ];
25 my $extant = {};
26 foreach my $set ( @$sets ) {
27         map { $extant->{$_} = 1 } @$set;
28 }
29 my $sitgraph = $s->editable( { extant => $extant } );
30 my $result = Text::Tradition::Analysis::Result->new(
31         graph => $sitgraph,
32         setlist => $sets );
33 is( ref( $result ), 'Text::Tradition::Analysis::Result', "Got a Result object" );
34 is( $result->graph, $sitgraph, "Got identical graph string back" );
35 is( $result->status, "new", "Calculation status of result set correctly" );
36 my @rsets = $result->sets;
37 is( $rsets[0], '(A B C P S T)', "First set is biggest set" );
38 is( $rsets[1], '(D Q)', "Second set is by alphabetical order" );
39 is( $rsets[2], '(F H)', "Second set is by alphabetical order" );
40
41 # Add some calculation values
42 $result->is_genealogical( 1 );
43 $result->record_grouping( [ qw/ 4 5 D Q / ] );
44 try {
45         $result->record_grouping( [ qw/ 3 4 D H / ] );
46         ok( 0, "Recorded a grouping that does not match the input sets" );
47 } catch ( Text::Tradition::Error $e ) {
48         like( $e->message, qr/Failed to find witness set that is a subset of/, 
49                 "Correct error thrown on bad record_grouping attempt" );
50 }
51 $result->record_grouping( [ qw/ 3 F H / ] );
52 my $gp1 = $result->grouping(1);
53 is( $result->minimum_grouping_for( $rsets[1] ), $gp1, 
54         "Found a minimum grouping for D Q" );
55 is( "$gp1", "(4 5 D Q)", "Retrieved minimum grouping is correct" );
56 is( $result->minimum_grouping_for( $rsets[0] ), $rsets[0], 
57         "Default minimum grouping found for biggest group" );
58 $result->record_grouping( [ qw/ 1 α δ A B C P S T / ] );
59 my %classes = (
60         α => 'source',
61         3 => 'source',
62         4 => 'source' );
63 foreach my $gp ( $result->groupings ) {
64         map { my $c = $classes{$_} || 'copy'; $result->set_class( $_, $c ) } @$gp;
65 }
66 foreach my $gp ( $result->groupings ) {
67         foreach my $wit ( @$gp ) {
68                 my $expected = $classes{$wit} || 'copy';
69                 is( $result->class( $wit ), $expected, "Got expected witness class for $wit" );
70         }
71 }
72
73 # Now write it out to JSON
74 my $struct = $result->TO_JSON;
75 my $newresult = Text::Tradition::Analysis::Result->new( $struct );
76 is( $result->object_key, $newresult->object_key, 
77         "Object key stayed constant on export/import" );
78 my $problem = Text::Tradition::Analysis::Result->new( graph => $sitgraph, setlist => $sets );
79 is( $problem->object_key, $result->object_key, 
80         "Object key stayed constant for newly created problem" );
81 }
82
83
84
85
86 1;