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