add docs and test to Result class
[scpubgit/stemmatology.git] / t / text_tradition_analysis_result.t
CommitLineData
0a17afe9 1#!/usr/bin/perl -w
2
3use strict;
4use Test::More 'no_plan';
5$| = 1;
6
7
8
9# =begin testing
10{
11use Test::More::UTF8;
12use Text::Tradition;
13use TryCatch;
14use_ok( 'Text::Tradition::Analysis::Result' );
15
16# Make a problem with a graph and a set of groupings
17
18my $datafile = 't/data/florilegium_tei_ps.xml';
19my $tradition = Text::Tradition->new( 'input' => 'TEI',
20 'name' => 'flortest',
21 'file' => $datafile );
22my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
23
24my $sets = [ [ qw/ D Q / ], [ qw/ F H / ], [ qw/ A B C P S T / ] ];
25my $extant = {};
26foreach my $set ( @$sets ) {
27 map { $extant->{$_} = 1 } @$set;
28}
29my $sitgraph = $s->editable( { extant => $extant } );
30my $result = Text::Tradition::Analysis::Result->new(
31 graph => $sitgraph,
32 setlist => $sets );
33is( ref( $result ), 'Text::Tradition::Analysis::Result', "Got a Result object" );
34is( $result->graph, $sitgraph, "Got identical graph string back" );
35is( $result->status, "new", "Calculation status of result set correctly" );
36my @rsets = $result->sets;
37is( $rsets[0], '(A B C P S T)', "First set is biggest set" );
38is( $rsets[1], '(D Q)', "Second set is by alphabetical order" );
39is( $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 / ] );
44try {
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 / ] );
52my $gp1 = $result->grouping(1);
53is( $result->minimum_grouping_for( $rsets[1] ), $gp1,
54 "Found a minimum grouping for D Q" );
55is( "$gp1", "(4 5 D Q)", "Retrieved minimum grouping is correct" );
56is( $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 / ] );
59my %classes = (
60 α => 'source',
61 3 => 'source',
62 4 => 'source' );
63foreach my $gp ( $result->groupings ) {
64 map { my $c = $classes{$_} || 'copy'; $result->set_class( $_, $c ) } @$gp;
65}
66foreach 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
74my $struct = $result->TO_JSON;
75my $newresult = Text::Tradition::Analysis::Result->new( $struct );
76is( $result->object_key, $newresult->object_key,
77 "Object key stayed constant on export/import" );
78my $problem = Text::Tradition::Analysis::Result->new( graph => $sitgraph, setlist => $sets );
79is( $problem->object_key, $result->object_key,
80 "Object key stayed constant for newly created problem" );
81}
82
83
84
85
861;