Drop OP_SCALE_WEIGHT over OP_VALUE_* in Query ctor
[xapian.git] / xapian-bindings / perl / t / parser.t
blobb5e57cb13510cf1d63892447252b4b5f09cbb959
1 use strict;
2 # Before 'make install' is performed this script should be runnable with
3 # 'make test'. After 'make install' it should work as 'perl test.pl'
5 # FIXME: these tests pass in the XS version.
6 my $disable_fixme = 1;
8 #########################
10 # Make warnings fatal
11 use warnings;
12 BEGIN {$SIG{__WARN__} = sub { die "Terminating test due to warning: $_[0]" } };
14 use Test::More;
15 use Devel::Peek;
16 BEGIN { plan tests => 73 };
17 use Xapian qw(:standard);
18 ok(1); # If we made it this far, we're ok.
20 #########################
22 # Insert your test code below, the Test module is use()ed here so read
23 # its man page ( perldoc Test ) for help writing this test script.
25 sub mset_expect_order (\@@) {
26     my ($m, @a) = @_;
27     my @m = map { $_->get_docid() } @{$m};
28     is( scalar @m, scalar @a );
29     for my $j (0 .. (scalar @a - 1)) {
30         is( $m[$j], $a[$j] );
31     }
34 # first create database dir, if it doesn't exist;
35 my $db_dir = 'testdb';
37 my $database;
38 ok( $database = Xapian::Database->new( $db_dir ) );
40 my $qp = new Xapian::QueryParser( $database );
41 $qp = new Xapian::QueryParser();
43 $qp->set_stemmer( Xapian::Stem->new('english') );
44 $qp->set_stemming_strategy( STEM_ALL );
45 $qp->set_default_op( OP_AND );
47 my $query;
48 ok( $query = $qp->parse_query( 'one or two', FLAG_BOOLEAN|FLAG_BOOLEAN_ANY_CASE|FLAG_SPELLING_CORRECTION ) );
49 ok( not $qp->get_corrected_query_string());
50 is( $query->get_description(), 'Query((one@1 OR two@2))' );
52 ok( $query = $qp->parse_query( 'one OR (two AND three)' ) );
53 is( $query->get_description(), 'Query((one@1 OR (two@2 AND three@3)))' );
55 ok( my $enq = $database->enquire( $query ) );
58   my @stopwords = qw(a the in on and);
59   my $stopper;
60   ok( $stopper = new Xapian::SimpleStopper(@stopwords) );
61   foreach (@stopwords) {
62     ok( $stopper->stop_word($_) );
63   }
64   foreach (qw(one two three four five)) {
65     ok( !$stopper->stop_word($_) );
66   }
67   is( $qp->set_stopper($stopper), undef );
69 ok( $qp->parse_query("one two many") );
71 $qp = new Xapian::QueryParser();
72 my $vrp;
73 ok( $vrp = new Xapian::StringValueRangeProcessor(1) );
74 $qp->add_valuerangeprocessor($vrp);
75 $qp->add_boolean_prefix("test", "XTEST");
77 my $pair;
78 foreach $pair (
79     [ 'a..b', 'VALUE_RANGE 1 a b' ],
80     [ '$50..100', 'VALUE_RANGE 1 $50 100' ],
81     [ '$50..$99', 'VALUE_RANGE 1 $50 $99' ],
82     [ '$50..$100', '' ],
83     [ '02/03/1979..10/12/1980', 'VALUE_RANGE 1 02/03/1979 10/12/1980' ],
84     [ 'a..b hello', '(hello@1 FILTER VALUE_RANGE 1 a b)' ],
85     [ 'hello a..b', '(hello@1 FILTER VALUE_RANGE 1 a b)' ],
86     [ 'hello a..b world', '((hello@1 OR world@2) FILTER VALUE_RANGE 1 a b)' ],
87     [ 'hello a..b test:foo', '(hello@1 FILTER (VALUE_RANGE 1 a b AND XTESTfoo))' ],
88     [ '-5..7', 'VALUE_RANGE 1 -5 7' ],
89     [ 'hello -5..7', '(hello@1 FILTER VALUE_RANGE 1 -5 7)' ],
90     [ '-5..7 hello', '(hello@1 FILTER VALUE_RANGE 1 -5 7)' ],
91     [ '"time flies" 09:00..12:30', '((time@1 PHRASE 2 flies@2) FILTER VALUE_RANGE 1 09:00 12:30)' ]
92     ) {
93     my ($str, $res) = @{$pair};
94     my $query = $qp->parse_query($str);
95     is( $query->get_description(), "Query($res)" );
98 $qp = new Xapian::QueryParser();
100 my $vrp1 = new Xapian::DateValueRangeProcessor(1);
101 my $vrp2 = new Xapian::NumberValueRangeProcessor(2);
102 my $vrp3 = new Xapian::StringValueRangeProcessor(3);
103 my $vrp4 = new Xapian::NumberValueRangeProcessor(4, '$');
104 my $vrp5 = new Xapian::NumberValueRangeProcessor(5, 'kg', 0);
105 my $vrp6 = new Xapian::StringValueRangeProcessor(6, 'country:');
106 my $vrp7 = new Xapian::StringValueRangeProcessor(7, ':name', 0);
107 $qp->add_valuerangeprocessor( $vrp1 );
108 $qp->add_valuerangeprocessor( $vrp2 );
109 $qp->add_valuerangeprocessor( $vrp4 );
110 $qp->add_valuerangeprocessor( $vrp5 );
111 $qp->add_valuerangeprocessor( $vrp6 );
112 $qp->add_valuerangeprocessor( $vrp7 );
113 $qp->add_valuerangeprocessor( $vrp3 );
115 $qp->add_boolean_prefix("test", "XTEST");
116 foreach $pair (
117     [ 'a..b', 'VALUE_RANGE 3 a b' ],
118     [ '1..12', "VALUE_RANGE 2 \\xa0 \\xae" ],
119     [ '20070201..20070228', 'VALUE_RANGE 1 20070201 20070228' ],
120     [ '$10..20', "VALUE_RANGE 4 \\xad \\xb1" ],
121     [ '$10..$20', "VALUE_RANGE 4 \\xad \\xb1" ],
122     [ '12..42kg', "VALUE_RANGE 5 \\xae \\xb5\@" ],
123     [ '12kg..42kg', "VALUE_RANGE 5 \\xae \\xb5\@" ],
124     [ '12kg..42', 'VALUE_RANGE 3 12kg 42' ],
125     [ '10..$20', '' ],
126     [ '1999-03-12..2020-12-30', 'VALUE_RANGE 1 19990312 20201230' ],
127     [ '1999/03/12..2020/12/30', 'VALUE_RANGE 1 19990312 20201230' ],
128     [ '1999.03.12..2020.12.30', 'VALUE_RANGE 1 19990312 20201230' ],
129     [ '12/03/99..12/04/01', 'VALUE_RANGE 1 19990312 20010412' ],
130     [ '03-12-99..04-14-01', 'VALUE_RANGE 1 19990312 20010414' ],
131     [ '(test:a..test:b hello)', '(hello@1 FILTER VALUE_RANGE 3 test:a test:b)' ],
132     [ 'country:chile..denmark', 'VALUE_RANGE 6 chile denmark' ],
133     [ 'albert..xeni:name', 'VALUE_RANGE 7 albert xeni' ],
134     ) {
135     my ($str, $res) = @{$pair};
136     my $query = $qp->parse_query($str);
137     is( $query->get_description(), "Query($res)" );
140 $qp = new Xapian::QueryParser();
143   my $vrpdate = new Xapian::DateValueRangeProcessor(1, 1, 1960);
144   $qp->add_valuerangeprocessor( $vrpdate );
147 foreach $pair (
148     [ '12/03/99..12/04/01', 'VALUE_RANGE 1 19991203 20011204' ],
149     [ '03-12-99..04-14-01', 'VALUE_RANGE 1 19990312 20010414' ],
150     [ '01/30/60..02/02/59', 'VALUE_RANGE 1 19600130 20590202' ],
151     ) {
152     my ($str, $res) = @{$pair};
153     my $query = $qp->parse_query($str);
154     is( $query->get_description(), "Query($res)" );
157 # Regression test for Search::Xapian bug fixed in 1.0.5.0.  In 1.0.0.0-1.0.4.0
158 # we tried to catch const char * not Xapian::Error, so std::terminate got
159 # called.
160 $qp = Xapian::QueryParser->new;
161 eval {
162     $qp->parse_query('other* AND', FLAG_BOOLEAN|FLAG_WILDCARD);
164 ok($@);
165 is(ref($@), "Xapian::QueryParserError", "correct class for exception");
166 ok($@->isa('Xapian::Error'));
167 is($@->get_msg, "Syntax: <expression> AND <expression>", "get_msg works");
168 ok( $disable_fixme || $@ =~ /^Exception: Syntax: <expression> AND <expression>(?: at \S+ line \d+\.)?$/ );
170 # Check FLAG_DEFAULT is wrapped (new in 1.0.11.0).
171 ok( $qp->parse_query('hello world', FLAG_DEFAULT|FLAG_BOOLEAN_ANY_CASE) );
173 # Test OP_WILDCARD with limits.
174 my ($q, @matches);
175 ok( $enq = Xapian::Enquire->new($database) );
177 $qp->set_max_expansion(1, Xapian::WILDCARD_LIMIT_FIRST);
178 ok( $q = $qp->parse_query('t*', FLAG_WILDCARD) );
179 $enq->set_query($q);
180 @matches = $enq->matches(0, 10);
181 mset_expect_order(@matches, (1, 2));
183 $qp->set_max_expansion(1, Xapian::WILDCARD_LIMIT_MOST_FREQUENT);
184 ok( $q = $qp->parse_query('t*', FLAG_WILDCARD) );
185 $enq->set_query($q);
186 @matches = $enq->matches(0, 10);
187 mset_expect_order(@matches, (1, 2));
189 $qp->set_max_expansion(1, Xapian::WILDCARD_LIMIT_ERROR);
190 ok( $q = $qp->parse_query('t*', FLAG_WILDCARD) );
191 $enq->set_query($q);
192 eval {
193     @matches = $enq->matches(0, 10);
195 ok( $@ );
196 is(ref($@), "Xapian::WildcardError", "correct class for exception");