Add apparatus codicum support and witness long-identifier support to CTE parser....
[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{
fae52efd 11use Text::Tradition;
12my $trad = Text::Tradition->new( 'name' => 'test tradition' );
13my $c = $trad->collation;
7158714d 14
fae52efd 15# Test a plaintext witness via string
16my $str = 'This is a line of text';
17my $ptwit = $trad->add_witness(
7158714d 18 'sigil' => 'A',
fae52efd 19 'sourcetype' => 'plaintext',
20 'string' => $str
7158714d 21 );
fae52efd 22is( ref( $ptwit ), 'Text::Tradition::Witness', 'Created a witness' );
23if( $ptwit ) {
24 is( $ptwit->sigil, 'A', "Witness has correct sigil" );
248276a2 25 $c->make_witness_path( $ptwit );
fae52efd 26 is( $c->path_text( $ptwit->sigil ), $str, "Witness has correct text" );
7158714d 27}
fae52efd 28
65ed66b9 29# Test some JSON witnesses via object
30open( JSIN, 't/data/witnesses/testwit.json' ) or die "Could not open JSON test input";
31binmode( JSIN, ':encoding(UTF-8)' );
32my @lines = <JSIN>;
33close JSIN;
34$trad->add_json_witnesses( join( '', @lines ) );
35is( ref( $trad->witness( 'MsAJ' ) ), 'Text::Tradition::Witness',
36 "Found first JSON witness" );
37is( ref( $trad->witness( 'MsBJ' ) ), 'Text::Tradition::Witness',
38 "Found second JSON witness" );
39
b39fb0b3 40# Test an XML witness via file
41my $xmlwit = $trad->add_witness( 'sourcetype' => 'xmldesc',
42 'file' => 't/data/witnesses/teiwit.xml' );
43is( ref( $xmlwit ), 'Text::Tradition::Witness', "Created witness from XML file" );
44if( $xmlwit ) {
45 is( $xmlwit->sigil, 'V887', "XML witness has correct sigil" );
46 ok( $xmlwit->is_layered, "Picked up correction layer" );
47 is( @{$xmlwit->text}, 182, "Got correct text length" );
48 is( @{$xmlwit->layertext}, 182, "Got correct a.c. text length" );
49}
50my @allwitwords = grep { $_->id =~ /^V887/ } $c->readings;
51is( @allwitwords, 184, "Reused appropriate readings" );
fae52efd 52
53## Test use_text
b39fb0b3 54my $xpwit = $trad->add_witness( 'sourcetype' => 'xmldesc',
55 'file' => 't/data/witnesses/group.xml',
56 'use_text' => '//tei:group/tei:text[2]' );
57is( ref( $xpwit ), 'Text::Tradition::Witness', "Created witness from XML group" );
58if( $xpwit ) {
59 is( $xpwit->sigil, 'G', "XML part witness has correct sigil" );
60 ok( !$xpwit->is_layered, "Picked up no correction layer" );
61 is( @{$xpwit->text}, 157, "Got correct text length" );
62}
7158714d 63}
64
65
66
f025e303 67# =begin testing
68{
69use Text::Tradition;
fae52efd 70my $trad = Text::Tradition->new();
f025e303 71
fae52efd 72my @text = qw/ Thhis is a line of text /;
73my $wit = $trad->add_witness(
f025e303 74 'sigil' => 'A',
fae52efd 75 'string' => join( ' ', @text ),
76 'sourcetype' => 'plaintext',
f025e303 77 'identifier' => 'test witness',
78 );
79my $jsonstruct = $wit->export_as_json;
80is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
81is( $jsonstruct->{'name'}, 'test witness', "got the right identifier" );
82is( scalar @{$jsonstruct->{'tokens'}}, 6, "got six text tokens" );
83foreach my $idx ( 0 .. $#text ) {
84 is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $text[$idx], "tokens look OK" );
85}
86
87my @ctext = qw( when april with his showers sweet with fruit the drought of march
88 has pierced unto the root );
fae52efd 89$trad = Text::Tradition->new(
f025e303 90 'input' => 'CollateX',
91 'file' => 't/data/Collatex-16.xml' );
92
93$jsonstruct = $trad->witness('A')->export_as_json;
94is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
95is( $jsonstruct->{'name'}, undef, "got undef for missing identifier" );
96is( scalar @{$jsonstruct->{'tokens'}}, 17, "got all text tokens" );
97foreach my $idx ( 0 .. $#ctext ) {
98 is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $ctext[$idx], "tokens look OK" );
99}
fae52efd 100
101## TODO test layertext export
f025e303 102}
103
104
105
7158714d 106
1071;