X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Ftext_tradition_witness.t;h=2457b2c548c146e98363cdbbafdff36de9da02c3;hb=96ba0418c65f3450b419aea78db41bf697612b63;hp=997b88870357c53a9258599b7c2f56f8fc8ef329;hpb=7158714d7066a37cfaefca599c3deb34b178b69d;p=scpubgit%2Fstemmatology.git diff --git a/t/text_tradition_witness.t b/t/text_tradition_witness.t index 997b888..2457b2c 100644 --- a/t/text_tradition_witness.t +++ b/t/text_tradition_witness.t @@ -8,17 +8,56 @@ $| = 1; # =begin testing { -use_ok( 'Text::Tradition::Witness', "can use module" ); +use Text::Tradition; +my $trad = Text::Tradition->new( 'name' => 'test tradition' ); +my $c = $trad->collation; -my @text = qw( This is a line of text ); -my $wit = Text::Tradition::Witness->new( +# Test a plaintext witness via string +my $str = 'This is a line of text'; +my $ptwit = $trad->add_witness( 'sigil' => 'A', - 'text' => \@text, + 'sourcetype' => 'plaintext', + 'string' => $str ); -is( ref( $wit ), 'Text::Tradition::Witness', 'Created a witness' ); -if( $wit ) { - is( $wit->sigil, 'A', "Witness has correct sigil" ); - is( join( ' ', @{$wit->text} ), join( ' ', @text ), "Witness has correct text" ); +is( ref( $ptwit ), 'Text::Tradition::Witness', 'Created a witness' ); +if( $ptwit ) { + is( $ptwit->sigil, 'A', "Witness has correct sigil" ); + is( $c->path_text( $ptwit->sigil ), $str, "Witness has correct text" ); +} + +# Test some JSON witnesses via object +open( JSIN, 't/data/witnesses/testwit.json' ) or die "Could not open JSON test input"; +binmode( JSIN, ':encoding(UTF-8)' ); +my @lines = ; +close JSIN; +$trad->add_json_witnesses( join( '', @lines ) ); +is( ref( $trad->witness( 'MsAJ' ) ), 'Text::Tradition::Witness', + "Found first JSON witness" ); +is( ref( $trad->witness( 'MsBJ' ) ), 'Text::Tradition::Witness', + "Found second JSON witness" ); + +# Test an XML witness via file +my $xmlwit = $trad->add_witness( 'sourcetype' => 'xmldesc', + 'file' => 't/data/witnesses/teiwit.xml' ); +is( ref( $xmlwit ), 'Text::Tradition::Witness', "Created witness from XML file" ); +if( $xmlwit ) { + is( $xmlwit->sigil, 'V887', "XML witness has correct sigil" ); + ok( $xmlwit->is_layered, "Picked up correction layer" ); + is( @{$xmlwit->text}, 182, "Got correct text length" ); + is( @{$xmlwit->layertext}, 182, "Got correct a.c. text length" ); +} +my @allwitwords = grep { $_->id =~ /^V887/ } $c->readings; +is( @allwitwords, 184, "Reused appropriate readings" ); + +## Test use_text +my $xpwit = $trad->add_witness( 'sourcetype' => 'xmldesc', + 'file' => 't/data/witnesses/group.xml', + 'use_text' => '//tei:group/tei:text[2]' ); +is( ref( $xpwit ), 'Text::Tradition::Witness', "Created witness from XML group" ); +if( $xpwit ) { + is( $xpwit->sigil, 'G', "XML part witness has correct sigil" ); + ok( !$xpwit->is_layered, "Picked up no correction layer" ); + is( @{$xpwit->text}, 157, "Got correct text length" ); } } @@ -27,21 +66,38 @@ if( $wit ) { # =begin testing { use Text::Tradition; +my $trad = Text::Tradition->new(); -my $simple = 't/data/simple.txt'; -my $s = Text::Tradition->new( - 'name' => 'inline', - 'input' => 'Tabular', - 'file' => $simple, - ); -my $wit_c = $s->witness( 'C' ); -is( ref( $wit_c ), 'Text::Tradition::Witness' ),; -if( $wit_c ) { - ok( !$wit_c->has_text, "Text property not yet set" ); - my $c_arr = $wit_c->text; - is( $c_arr->[0], 'Je', "Text constructed from path" ); - ok( $wit_c->has_text, "Text property now set" ); +my @text = qw/ Thhis is a line of text /; +my $wit = $trad->add_witness( + 'sigil' => 'A', + 'string' => join( ' ', @text ), + 'sourcetype' => 'plaintext', + 'identifier' => 'test witness', + ); +my $jsonstruct = $wit->export_as_json; +is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" ); +is( $jsonstruct->{'name'}, 'test witness', "got the right identifier" ); +is( scalar @{$jsonstruct->{'tokens'}}, 6, "got six text tokens" ); +foreach my $idx ( 0 .. $#text ) { + is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $text[$idx], "tokens look OK" ); +} + +my @ctext = qw( when april with his showers sweet with fruit the drought of march + has pierced unto the root ); +$trad = Text::Tradition->new( + 'input' => 'CollateX', + 'file' => 't/data/Collatex-16.xml' ); + +$jsonstruct = $trad->witness('A')->export_as_json; +is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" ); +is( $jsonstruct->{'name'}, undef, "got undef for missing identifier" ); +is( scalar @{$jsonstruct->{'tokens'}}, 17, "got all text tokens" ); +foreach my $idx ( 0 .. $#ctext ) { + is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $ctext[$idx], "tokens look OK" ); } + +## TODO test layertext export }