Plucene/TestCase.pm

filter: Statementstmt 100% 64 / 64 Branchbran 66.6% 4 / 6 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 90.1% 91 / 101 CC 6 SCAR 5.3
CC
6
Cov
92%
new_index
3.1
re_index
3.1
add_document
2.0
CRAP 1.7
« Plucene/Index/SegmentTermEnum.pm ↑ Summary Plucene/Index/Reader.pm »
1 package Plucene::TestCase;
2  
3 =head1 NAME
4  
5 Plucene::TestCase - Handy functions when testing Plucene
6  
7 =head1 SYNOPSIS
8  
9 use Test::More tests => 10;
10 use Plucene::TestCase;
11  
12 new_index {
13 add_document( foo => "bar" );
14 };
15  
16 re_index {
17 add_document( foo => "baz" );
18 }
19  
20 with_reader {
21 $READER->whatever;
22 }
23  
24 my $hits = search("foo:ba*");
25  
26 =head1 EXPORTS
27  
28 =cut
29  
30 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 11 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 11 ▶use Plucene::Index::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 11 ▶use Plucene::Index::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 11 ▶use Plucene::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 11 ▶use Plucene::Document::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 11 ▶use Plucene::Analysis::SimpleAnalyzer;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 11 ▶use Plucene::QueryParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 11 ▶use Plucene::Search::IndexSearcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 11 our (@EXPORT, $DIR, $DEBUG, $WRITER, $READER, $ANALYZER);
44 11 @EXPORT = qw($DIR $WRITER $READER $ANALYZER new_index re_index
45 with_reader add_document search);
46  
47 11 $ANALYZER = "Plucene::Analysis::SimpleAnalyzer";
48  
49 =over 3
50  
51 =item C<$DIR>
52  
53 A directory which is created for the purposes of this test, in which the
54 index will be placed. It will normally be cleaned up at the end of the
55 test, unless C<$Plucene::TestCase::DEBUG> is set to allow you to peruse
56 the entrails.
57  
58 =cut
59  
60 11 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 11 $DIR = tempdir(CLEANUP => !$DEBUG);
62  
63 =item C<$WRITER>
64  
65 A variable holding the current C<Index::Writer> object, if there is one.
66  
67 =item C<$READER>
68  
69 A variable holding the current C<Index::Reader> object, if there is one.
70  
71 =item C<$ANALYZER>
72  
73 A variable holding the class name of the desired C<Analysis::Analyzer>
74 class.
75  
76 =item new_index BLOCK (Analyzer)
77  
78 Create a new index, and do the following stuff in the block before
79 closing the index writer. C<$WRITER> is set for the duration of the
80 block.
81  
82 The optional parameter should be the class name of the analyzer to use;
83 if not specified, the value from C<$ANALYZER>, which in turn defaults to
84 C<Plucene::Analysis::SimpleAnalyzer>, will be used.
85  
86 =cut
87  
88 11 ▶sub new_index(&;$) {
Subroutine new_index: called CC=3 SCAR=11.4
Pod documented
89 11 my ($block, $analyzer) = @_;
90 11 ▶ $analyzer ||= $ANALYZER;
Condition33%1/3
$analyzer ||= $Plucene::TestCase::ANALYZER
l!l&&r!l&&!r
0110
MC/DC0%0/2
$analyzer ||= $Plucene::TestCase::ANALYZER
$analyzer $Plucene::TestCase::ANALYZER
Truth table33%1/3
A ||= B
A= $analyzerB= $Plucene::TestCase::ANALYZER
ABresult
1X1
011
000
91  
92 # UNIVERSAL::require loads UNIVERSAL->import, which won't do.
93 11 eval "require $analyzer";
94 11 ▶ die "Couldn't require $analyzer" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
95 11 $WRITER = Plucene::Index::Writer->new($DIR, $analyzer->new, 1);
96 11 $block->();
97 11 undef $WRITER;
98 }
99  
100 =item re_index BLOCK (Analyzer)
101  
102 Same as C<new_index>, but doesn't create a new index, rather re-uses an
103 old one.
104  
105 =cut
106  
107 1 ▶sub re_index(&;$) {
Subroutine re_index: called CC=3 SCAR=11.4
Pod documented
108 1 my ($block, $analyzer) = @_;
109 1 ▶ $analyzer ||= $ANALYZER;
Condition33%1/3
$analyzer ||= $Plucene::TestCase::ANALYZER
l!l&&r!l&&!r
010
MC/DC0%0/2
$analyzer ||= $Plucene::TestCase::ANALYZER
$analyzer $Plucene::TestCase::ANALYZER
Truth table33%1/3
A ||= B
A= $analyzerB= $Plucene::TestCase::ANALYZER
ABresult
1X1
011
000
110 1 eval "require $analyzer";
111 1 ▶ die "Couldn't require $analyzer" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
112 1 $WRITER = Plucene::Index::Writer->new($DIR, $analyzer->new, 0);
113 1 $block->();
114 1 undef $WRITER;
115 }
116  
117 =item add_document( field1 => value1, ...)
118  
119 Add a new document to the index, with the given fields and values
120  
121 =cut
122  
123 199 ▶sub add_document {
Subroutine add_document: called CC=2 SCAR=6.9
Pod documented
124 199 my @args = @_;
125 199 my $doc = Plucene::Document->new;
126 199 ▶ while (my ($k, $v) = splice(@args, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@args, 0, 2)) 250 199 449
127 250 $doc->add(Plucene::Document::Field->Text($k, $v));
128 }
129 199 $WRITER->add_document($doc);
130 }
131  
132 =item with_reader BLOCK
133  
134 Opens an index reader in C<$READER> and runs the block.
135  
136 =cut
137  
138 4 ▶sub with_reader (&) {
Subroutine with_reader: called CC=1 SCAR=0.0
Pod documented
139 4 $READER = Plucene::Index::Reader->open($DIR);
140 4 shift->();
141 4 $READER->close;
142 4 undef $READER;
143 }
144  
145 =item search
146  
147 Searches for the query given. If any fields are not specified, they will
148 be assumed to be the default C<text>. Returns a C<Plucene::Search::Hits>
149 object. The value of C<$ANALYZER> will be used to construct an analyzer
150 for the query string.
151  
152 =cut
153  
154 182 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod documented
155 182 eval "require $ANALYZER";
156 182 my $parser = Plucene::QueryParser->new({
157 analyzer => $ANALYZER->new(),
158 default => "text"
159 });
160 182 Plucene::Search::IndexSearcher->new($DIR)->search($parser->parse(shift));
161 }
162  
163 11 1;
« Plucene/Index/SegmentTermEnum.pm ↑ Summary Plucene/Index/Reader.pm »