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