try to fix test failure of #30
[scpubgit/stemmatology.git] / base / t / text_tradition_witness.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 qw/ -utf8 /;
12 use Text::Tradition;
13 my $trad = Text::Tradition->new( 'name' => 'test tradition' );
14 my $c = $trad->collation;
15
16 # Test a plaintext witness via string
17 my $str = 'This is a line of text';
18 my $ptwit = $trad->add_witness( 
19     'sigil' => 'A',
20     'sourcetype' => 'plaintext',
21     'string' => $str
22      );
23 is( ref( $ptwit ), 'Text::Tradition::Witness', 'Created a witness' );
24 if( $ptwit ) {
25     is( $ptwit->sigil, 'A', "Witness has correct sigil" );
26     $c->make_witness_path( $ptwit );
27     is( $c->path_text( $ptwit->sigil ), $str, "Witness has correct text" );
28 }
29
30 # Test some JSON witnesses via object
31 open( JSIN, 't/data/witnesses/testwit.json' ) or die "Could not open JSON test input";
32 binmode( JSIN, ':encoding(UTF-8)' );
33 my @lines = <JSIN>;
34 close JSIN;
35 $trad->add_json_witnesses( join( '', @lines ) );
36 is( ref( $trad->witness( 'MsAJ' ) ), 'Text::Tradition::Witness', 
37         "Found first JSON witness" );
38 is( ref( $trad->witness( 'MsBJ' ) ), 'Text::Tradition::Witness', 
39         "Found second JSON witness" );
40
41 # Test an XML witness via file
42 my $xmlwit = $trad->add_witness( 'sourcetype' => 'xmldesc', 
43         'file' => 't/data/witnesses/teiwit.xml' );
44 is( ref( $xmlwit ), 'Text::Tradition::Witness', "Created witness from XML file" );
45 if( $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 }
51 my @allwitwords = grep { $_->id =~ /^V887/ } $c->readings;
52 is( @allwitwords, 184, "Reused appropriate readings" );
53
54 ## Test use_text
55 my $xpwit = $trad->add_witness( 'sourcetype' => 'xmldesc',
56         'file' => 't/data/witnesses/group.xml',
57         'use_text' => '//tei:group/tei:text[2]' );
58 is( ref( $xpwit ), 'Text::Tradition::Witness', "Created witness from XML group" );
59 if( $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 }
64
65 # Test non-ASCII sigla
66 my $at = Text::Tradition->new(
67         name => 'armexample',
68         input => 'Tabular',
69         excel => 'xlsx',
70         file => 't/data/armexample.xlsx' );
71 foreach 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 }
82 }
83
84
85
86 # =begin testing
87 {
88 use Text::Tradition;
89 my $trad = Text::Tradition->new();
90
91 my @text = qw/ Thhis is a line of text /;
92 my $wit = $trad->add_witness( 
93     'sigil' => 'A',
94     'string' => join( ' ', @text ),
95     'sourcetype' => 'plaintext',
96     'identifier' => 'test witness',
97      );
98 my $jsonstruct = $wit->export_as_json;
99 is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
100 is( $jsonstruct->{'name'}, 'test witness', "got the right identifier" );
101 is( scalar @{$jsonstruct->{'tokens'}}, 6, "got six text tokens" );
102 foreach my $idx ( 0 .. $#text ) {
103         is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $text[$idx], "tokens look OK" );
104 }
105
106 my @ctext = qw( when april with his showers sweet with fruit the drought of march 
107                                 has pierced unto the root );
108 $trad = Text::Tradition->new(
109         'input' => 'CollateX',
110         'file' => 't/data/Collatex-16.xml' );
111
112 $jsonstruct = $trad->witness('A')->export_as_json;
113 is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
114 is( $jsonstruct->{'name'}, undef, "got undef for missing identifier" );
115 is( scalar @{$jsonstruct->{'tokens'}}, 17, "got all text tokens" );
116 foreach my $idx ( 0 .. $#ctext ) {
117         is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $ctext[$idx], "tokens look OK" );
118 }
119
120 ## TODO test layertext export
121 }
122
123
124
125
126 1;