get JSON witness parsing to work
[scpubgit/stemmatology.git] / 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 Text::Tradition;
12 my $trad = Text::Tradition->new( 'name' => 'test tradition' );
13 my $c = $trad->collation;
14
15 # Test a plaintext witness via string
16 my $str = 'This is a line of text';
17 my $ptwit = $trad->add_witness( 
18     'sigil' => 'A',
19     'sourcetype' => 'plaintext',
20     'string' => $str
21      );
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" );
26 }
27
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
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->path}, 185, "Got correct text length" );
47 #       is( @{$xmlwit->uncorrected_path}, 185, "Got correct a.c. text length" );
48 # }
49
50 ## Test use_text
51 }
52
53
54
55 # =begin testing
56 {
57 use Text::Tradition;
58 my $trad = Text::Tradition->new();
59
60 my @text = qw/ Thhis is a line of text /;
61 my $wit = $trad->add_witness( 
62     'sigil' => 'A',
63     'string' => join( ' ', @text ),
64     'sourcetype' => 'plaintext',
65     'identifier' => 'test witness',
66      );
67 my $jsonstruct = $wit->export_as_json;
68 is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
69 is( $jsonstruct->{'name'}, 'test witness', "got the right identifier" );
70 is( scalar @{$jsonstruct->{'tokens'}}, 6, "got six text tokens" );
71 foreach my $idx ( 0 .. $#text ) {
72         is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $text[$idx], "tokens look OK" );
73 }
74
75 my @ctext = qw( when april with his showers sweet with fruit the drought of march 
76                                 has pierced unto the root );
77 $trad = Text::Tradition->new(
78         'input' => 'CollateX',
79         'file' => 't/data/Collatex-16.xml' );
80
81 $jsonstruct = $trad->witness('A')->export_as_json;
82 is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
83 is( $jsonstruct->{'name'}, undef, "got undef for missing identifier" );
84 is( scalar @{$jsonstruct->{'tokens'}}, 17, "got all text tokens" );
85 foreach my $idx ( 0 .. $#ctext ) {
86         is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $ctext[$idx], "tokens look OK" );
87 }
88
89 ## TODO test layertext export
90 }
91
92
93
94
95 1;