buildlib/Lucy/Test/TestUtils.pm

filter: Statementstmt 99.2% 131 / 132 Branchbran 72.7% 16 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 9 totaltotal 89.8% 169 / 188 CC 21 SCAR 14.1
CC
21
Cov
95%
get_uscon_docs
9.3
create_uscon_index
6.0
uscon_dir
3.3
CRAP 4.1
« buildlib/Lucy/Build.pm ↑ Summary blib/lib/LucyX/Index/ByteBufDocReader.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15  
16 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 package Lucy::Test::TestUtils;
20  
21 41 our $VERSION = '0.008000';
22 41 $VERSION = eval $VERSION;
23  
24 41 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 41 our @EXPORT_OK = qw(
26 uscon_dir
27 create_index
28 create_uscon_index
29 init_test_index_loc
30 get_uscon_docs
31 utf8_test_strings
32 test_analyzer
33 doc_ids_from_td_coll
34 modulo_set
35 );
36  
37 41 ▶use Lucy;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 41 ▶use Lucy::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 41 ▶use File::Spec::Functions qw( catdir catfile curdir updir );
Subroutine BEGIN: called CC=1 SCAR=0.0
40 41 ▶use Encode qw( _utf8_off );
Subroutine BEGIN: called CC=1 SCAR=0.0
41 41 ▶use File::Path qw( rmtree );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 41 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 41 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 # Create a temporary test directory that will be removed at exit.
46 7 ▶sub init_test_index_loc {
Subroutine init_test_index_loc: called CC=1 SCAR=0.0
Pod undocumented
47 7 return tempdir( DIR => 't', CLEANUP => 1 );
48 }
49  
50 # Build a RAM index, using the supplied array of strings as source material.
51 # The index will have a single field: "content".
52 26 ▶sub create_index {
Subroutine create_index: called CC=2 SCAR=6.9
Pod undocumented
53 26 my $folder = Lucy::Store::RAMFolder->new;
54 26 my $indexer = Lucy::Index::Indexer->new(
55 index => $folder,
56 schema => Lucy::Test::TestSchema->new,
57 );
58 26 $indexer->add_doc( { content => $_ } ) for @_;
59 26 $indexer->commit;
60 26 return $folder;
61 }
62  
63 2 ▶sub uscon_dir {
Subroutine uscon_dir: called CC=3 SCAR=12.0
Pod undocumented
64 2 my @dirs = (
65 catdir('sample', 'us_constitution'),
66 catdir(updir(), 'common', 'sample', 'us_constitution'),
67 );
68  
69 2 for my $dir (@dirs) {
70 2 ▶ return $dir if -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir 2 0 2
71 }
72  
73 0 die("uscon source dir not found");
74 }
75  
76 # Slurp us constitition docs and build hashrefs.
77 2 ▶sub get_uscon_docs {
Subroutine get_uscon_docs: called CC=9 SCAR=22.3
Pod undocumented
78  
79 2 my $uscon_dir = uscon_dir();
80 2 ▶ opendir( my $uscon_dh, $uscon_dir )
Branch: 1 branch
BranchTrueFalseTotal
opendir my $uscon_dh, $uscon_dir or die "couldn't opendir '${uscon_dir}': $!" 2 0 2
81 or die "couldn't opendir '$uscon_dir': $!";
82 2 my @filenames = grep {/\.txt$/} sort readdir $uscon_dh;
83 2 ▶ closedir $uscon_dh or die "couldn't closedir '$uscon_dir': $!";
Branch: 1 branch
BranchTrueFalseTotal
closedir $uscon_dh or die "couldn't closedir '${uscon_dir}': $!" 2 0 2
84  
85 2 my %docs;
86  
87 2 for my $filename (@filenames) {
88 104 my $filepath = catfile( $uscon_dir, $filename );
89 104 ▶ open( my $fh, '<', $filepath )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filepath or die "couldn't open file '${filepath}': $!" 104 0 104
90 or die "couldn't open file '$filepath': $!";
91 104 my $content = do { local $/; <$fh> };
92 104 ▶ $content =~ /\A(.+?)^\s+(.*)/ms
Branch: 1 branch
BranchTrueFalseTotal
$content =~ /\A(.+?)^\s+(.*)/ms or die "Can't extract title/bodytext from '${filepath}'" 104 0 104
93 or die "Can't extract title/bodytext from '$filepath'";
94 104 my $title = $1;
95 104 my $bodytext = $2;
96 104 $bodytext =~ s/\s+/ /sg;
97 104 ▶ my $category
Branch: 3 branches
BranchTrueFalseTotal
$filename =~ /art/ ? : 48 56 104
$filename =~ /amend/ ? : 54 2 56
$filename =~ /preamble/ ? : 2 0 2
98 = $filename =~ /art/ ? 'article'
99 : $filename =~ /amend/ ? 'amendment'
100 : $filename =~ /preamble/ ? 'preamble'
101 : confess "Can't derive category for $filename";
102  
103 104 $docs{$filename} = {
104 title => $title,
105 bodytext => $bodytext,
106 url => "/us_constitution/$filename",
107 category => $category,
108 };
109 }
110  
111 2 return \%docs;
112 }
113  
114 1 ▶sub _uscon_schema {
Subroutine _uscon_schema: called CC=1 SCAR=0.0
115 1 my $schema = Lucy::Plan::Schema->new;
116 1 my $analyzer = Lucy::Analysis::EasyAnalyzer->new( language => 'en' );
117 1 my $title_type = Lucy::Plan::FullTextType->new( analyzer => $analyzer, );
118 1 my $content_type = Lucy::Plan::FullTextType->new(
119 analyzer => $analyzer,
120 highlightable => 1,
121 );
122 1 my $url_type = Lucy::Plan::StringType->new( indexed => 0, );
123 1 my $cat_type = Lucy::Plan::StringType->new;
124 1 $schema->spec_field( name => 'title', type => $title_type );
125 1 $schema->spec_field( name => 'content', type => $content_type );
126 1 $schema->spec_field( name => 'url', type => $url_type );
127 1 $schema->spec_field( name => 'category', type => $cat_type );
128 1 return $schema;
129 }
130  
131 1 ▶sub create_uscon_index {
Subroutine create_uscon_index: called CC=6 SCAR=17.9
Pod undocumented
132 1 my $dir = tempdir( DIR => 't', CLEANUP => 1 );
133 1 my $folder = Lucy::Store::FSFolder->new( path => $dir );
134 1 my $indexer = Lucy::Index::Indexer->new(
135 schema => _uscon_schema(),
136 index => $folder,
137 truncate => 1,
138 create => 1,
139 );
140  
141 1 $indexer->add_doc( { content => "zz$_" } ) for ( 0 .. 10000 );
142 1 $indexer->commit;
143 1 undef $indexer;
144  
145 1 $indexer = Lucy::Index::Indexer->new( index => $folder );
146 1 my $source_docs = get_uscon_docs();
147 $indexer->add_doc( { content => $_->{bodytext} } )
148 1 for values %$source_docs;
149 1 $indexer->commit;
150 1 undef $indexer;
151  
152 1 $indexer = Lucy::Index::Indexer->new( index => $folder );
153 1 my @chars = ( 'a' .. 'z' );
154 1 for ( 0 .. 1000 ) {
155 1001 my $content = '';
156 1001 for my $num_words ( 1 .. int( rand(20) ) ) {
157 9704 for ( 1 .. ( int( rand(10) ) + 10 ) ) {
158 140810 $content .= @chars[ rand(@chars) ];
159 }
160 9704 $content .= ' ';
161 }
162 1001 $indexer->add_doc( { content => $content } );
163 }
164 1 $indexer->optimize;
165 1 $indexer->commit;
166  
167 1 return $dir;
168 }
169  
170 # Return 3 strings useful for verifying UTF-8 integrity.
171 4 ▶sub utf8_test_strings {
Subroutine utf8_test_strings: called CC=1 SCAR=0.0
Pod undocumented
172 4 my $smiley = "\x{263a}";
173 4 my $not_a_smiley = $smiley;
174 4 _utf8_off($not_a_smiley);
175 4 my $frowny = $not_a_smiley;
176 4 utf8::upgrade($frowny);
177 4 return ( $smiley, $not_a_smiley, $frowny );
178 }
179  
180 # Verify an Analyzer's transform, transform_text, and split methods.
181 11 ▶sub test_analyzer {
Subroutine test_analyzer: called CC=3 SCAR=11.0
Pod undocumented
182 11 my ( $analyzer, $source, $expected, $message ) = @_;
183  
184 11 my $inversion = Lucy::Analysis::Inversion->new( text => $source );
185 11 $inversion = $analyzer->transform($inversion);
186 11 my @got;
187 11 ▶ while ( my $token = $inversion->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $inversion->next) 18 11 29
188 18 push @got, $token->get_text;
189 }
190 11 Test::More::is_deeply( \@got, $expected, "analyze: $message" );
191  
192 11 $inversion = $analyzer->transform_text($source);
193 11 @got = ();
194 11 ▶ while ( my $token = $inversion->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $inversion->next) 18 11 29
195 18 push @got, $token->get_text;
196 }
197 11 Test::More::is_deeply( \@got, $expected, "transform_text: $message" );
198  
199 11 @got = @{ $analyzer->split($source) };
200 11 Test::More::is_deeply( \@got, $expected, "split: $message" );
201 }
202  
203 # Extract all doc nums from a SortCollector. Return two sorted array refs:
204 # by_score and by_id.
205 1484 ▶sub doc_ids_from_td_coll {
Subroutine doc_ids_from_td_coll: called CC=2 SCAR=6.9
Pod undocumented
206 1484 my $collector = shift;
207 1484 my @by_score;
208 1484 my $match_docs = $collector->pop_match_docs;
209 24842 my @by_score_then_id = map { $_->get_doc_id }
210 sort {
211 1484 ▶ $b->get_score <=> $a->get_score
Condition66%2/3
$b->get_score <=> $a->get_score || $a->get_doc_id <=> $b->get_doc_id
l!l&&r!l&&!r
1084262060
MC/DC0%0/2
$b->get_score <=> $a->get_score || $a->get_doc_id <=> $b->get_doc_id
$b->get_score <=> $a->get_score $a->get_doc_id <=> $b->get_doc_id
Truth table66%2/3
A || B
A= $b->get_score <=> $a->get_scoreB= $a->get_doc_id <=> $b->get_doc_id
ABresult
1X1
011
000
212 || $a->get_doc_id <=> $b->get_doc_id
213 } @$match_docs;
214 1484 my @by_id = sort { $a <=> $b } @by_score_then_id;
215 1484 return ( \@by_score_then_id, \@by_id );
216 }
217  
218 # Use a modulus to generate a set of numbers.
219 3973 ▶sub modulo_set {
Subroutine modulo_set: called CC=2 SCAR=6.9
Pod undocumented
220 3973 my ( $interval, $max ) = @_;
221 3973 my @out;
222 3973 ▶ for ( my $doc = $interval; $doc < $max; $doc += $interval ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc < $max) 53362 3973 57335
223 53362 push @out, $doc;
224 }
225 3973 return \@out;
226 }
227  
228 41 1;
229  
230 __END__
« buildlib/Lucy/Build.pm ↑ Summary blib/lib/LucyX/Index/ByteBufDocReader.pm »