try to fix test failure of #30
[scpubgit/stemmatology.git] / base / t / text_tradition_witness.t
CommitLineData
7158714d 1#!/usr/bin/perl -w
2
3use strict;
4use Test::More 'no_plan';
5$| = 1;
6
7
8
9# =begin testing
10{
e46fff93 11use Test::More::UTF8 qw/ -utf8 /;
fae52efd 12use Text::Tradition;
13my $trad = Text::Tradition->new( 'name' => 'test tradition' );
14my $c = $trad->collation;
7158714d 15
fae52efd 16# Test a plaintext witness via string
17my $str = 'This is a line of text';
18my $ptwit = $trad->add_witness(
7158714d 19 'sigil' => 'A',
fae52efd 20 'sourcetype' => 'plaintext',
21 'string' => $str
7158714d 22 );
fae52efd 23is( ref( $ptwit ), 'Text::Tradition::Witness', 'Created a witness' );
24if( $ptwit ) {
25 is( $ptwit->sigil, 'A', "Witness has correct sigil" );
248276a2 26 $c->make_witness_path( $ptwit );
fae52efd 27 is( $c->path_text( $ptwit->sigil ), $str, "Witness has correct text" );
7158714d 28}
fae52efd 29
65ed66b9 30# Test some JSON witnesses via object
31open( JSIN, 't/data/witnesses/testwit.json' ) or die "Could not open JSON test input";
32binmode( JSIN, ':encoding(UTF-8)' );
33my @lines = <JSIN>;
34close JSIN;
35$trad->add_json_witnesses( join( '', @lines ) );
36is( ref( $trad->witness( 'MsAJ' ) ), 'Text::Tradition::Witness',
37 "Found first JSON witness" );
38is( ref( $trad->witness( 'MsBJ' ) ), 'Text::Tradition::Witness',
39 "Found second JSON witness" );
40
b39fb0b3 41# Test an XML witness via file
42my $xmlwit = $trad->add_witness( 'sourcetype' => 'xmldesc',
43 'file' => 't/data/witnesses/teiwit.xml' );
44is( ref( $xmlwit ), 'Text::Tradition::Witness', "Created witness from XML file" );
45if( $xmlwit ) {
46 is( $xmlwit->sigil, 'V887', "XML witness has correct sigil" );
47 ok( $xmlwit->is_layered, "Picked up correction layer" );
48 is( @{$xmlwit->text}, 182, "Got correct text length" );
49 is( @{$xmlwit->layertext}, 182, "Got correct a.c. text length" );
50}
51my @allwitwords = grep { $_->id =~ /^V887/ } $c->readings;
52is( @allwitwords, 184, "Reused appropriate readings" );
fae52efd 53
54## Test use_text
b39fb0b3 55my $xpwit = $trad->add_witness( 'sourcetype' => 'xmldesc',
56 'file' => 't/data/witnesses/group.xml',
57 'use_text' => '//tei:group/tei:text[2]' );
58is( ref( $xpwit ), 'Text::Tradition::Witness', "Created witness from XML group" );
59if( $xpwit ) {
60 is( $xpwit->sigil, 'G', "XML part witness has correct sigil" );
61 ok( !$xpwit->is_layered, "Picked up no correction layer" );
62 is( @{$xpwit->text}, 157, "Got correct text length" );
63}
e46fff93 64
65# Test non-ASCII sigla
66my $at = Text::Tradition->new(
67 name => 'armexample',
68 input => 'Tabular',
69 excel => 'xlsx',
70 file => 't/data/armexample.xlsx' );
71foreach my $wit ( $at->witnesses ) {
72 my $sig = $wit->sigil;
73 if( $sig =~ /^\p{ASCII}+$/ ) {
74 is( $wit->ascii_sigil, '_A_' . $sig,
75 "Correct ASCII sigil for ASCII witness $sig" );
76 } else {
77 # This is our non-ASCII example
78 is( $wit->ascii_sigil, '_A_5315622',
79 "Correct ASCII sigil for non-ASCII witness $sig" );
80 }
81}
7158714d 82}
83
84
85
f025e303 86# =begin testing
87{
88use Text::Tradition;
fae52efd 89my $trad = Text::Tradition->new();
f025e303 90
fae52efd 91my @text = qw/ Thhis is a line of text /;
92my $wit = $trad->add_witness(
f025e303 93 'sigil' => 'A',
fae52efd 94 'string' => join( ' ', @text ),
95 'sourcetype' => 'plaintext',
f025e303 96 'identifier' => 'test witness',
97 );
98my $jsonstruct = $wit->export_as_json;
99is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
100is( $jsonstruct->{'name'}, 'test witness', "got the right identifier" );
101is( scalar @{$jsonstruct->{'tokens'}}, 6, "got six text tokens" );
102foreach my $idx ( 0 .. $#text ) {
103 is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $text[$idx], "tokens look OK" );
104}
105
106my @ctext = qw( when april with his showers sweet with fruit the drought of march
107 has pierced unto the root );
fae52efd 108$trad = Text::Tradition->new(
f025e303 109 'input' => 'CollateX',
110 'file' => 't/data/Collatex-16.xml' );
111
112$jsonstruct = $trad->witness('A')->export_as_json;
113is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
114is( $jsonstruct->{'name'}, undef, "got undef for missing identifier" );
115is( scalar @{$jsonstruct->{'tokens'}}, 17, "got all text tokens" );
116foreach my $idx ( 0 .. $#ctext ) {
117 is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $ctext[$idx], "tokens look OK" );
118}
fae52efd 119
120## TODO test layertext export
f025e303 121}
122
123
124
7158714d 125
1261;