blob: c3a1e967c52c45aeb2d0a16b90c5b7305be8498f [file] [log] [blame]
Akron4f9eef42017-07-24 11:41:09 +02001use Test::More;
2use Test::Krawfish;
3use strict;
4use warnings;
5
6use_ok('Krawfish::Index');
7use_ok('Krawfish::Koral::Query::Builder');
8
9my $index = Krawfish::Index->new;
Akron56422cf2017-08-16 14:17:01 +020010ok_index($index, [qw/aa bb aa bc ac bb cc ca/], 'Add complex document');
Akron4f9eef42017-07-24 11:41:09 +020011
12# Simple
13ok(my $qb = Krawfish::Koral::Query::Builder->new, 'Create Builder');
14ok(my $q = $qb->term_re('[ac].'), 'Regex');
Akron3ab2e972017-08-02 19:10:10 +020015ok($q = $q->normalize->finalize, 'Prepare query');
Akron7b4e4d92017-09-25 12:18:29 +020016
Akron3ab2e972017-08-02 19:10:10 +020017is($q->to_string, "/[ac]./", 'Stringification');
18ok($q = $q->identify($index->dict), 'Prepare query');
Akron3d1df332017-12-23 16:21:21 +010019is($q->to_string(1), '#10|#12|#2|#8', 'Stringification');
Akron3ab2e972017-08-02 19:10:10 +020020
21ok($q = $q->optimize($index->segment), 'Prepare query');
Akron92d9f672017-08-16 12:23:11 +020022is($q->to_string, "or(or(or(#10,#12),#8),#2)", 'Stringification');
Akron4f9eef42017-07-24 11:41:09 +020023
Akron4f9eef42017-07-24 11:41:09 +020024# Class
25ok($q = $qb->class(
26 $qb->term_re('[ac].'),
272), 'Regex in class');
Akron3ab2e972017-08-02 19:10:10 +020028ok($q = $q->normalize->finalize->identify($index->dict)->optimize($index->segment), 'Prepare query');
Akron92d9f672017-08-16 12:23:11 +020029is($q->to_string, "class(2:or(or(or(#10,#12),#8),#2))", 'Stringification');
Akron4f9eef42017-07-24 11:41:09 +020030
31# Constraints
Akron95a37a42017-11-20 17:16:40 +010032ok($q = $qb->constraint(
Akron4f9eef42017-07-24 11:41:09 +020033 [$qb->c_position('precedes')],
34 $qb->term_re('[ac].'),
35 $qb->term_re('b.')
36), 'Regex in constraint');
Akron3ab2e972017-08-02 19:10:10 +020037ok($q = $q->normalize->finalize->identify($index->dict)->optimize($index->segment), 'Prepare query');
Akron92d9f672017-08-16 12:23:11 +020038is($q->to_string, "constr(pos=1:or(or(or(#10,#12),#8),#2),or(#6,#4))",
Akron4f9eef42017-07-24 11:41:09 +020039 'Stringification');
40
41
42# Constraints: Inflate in constraint
Akron95a37a42017-11-20 17:16:40 +010043ok($q = $qb->constraint(
Akron4f9eef42017-07-24 11:41:09 +020044 [
45 $qb->c_position('precedes'),
46 $qb->c_not_between(
47 $qb->term_re('[ac].')
48 )
49 ],
50 $qb->term('aa'),
51 $qb->term('bb')
52), 'Regex in constraint');
Akron7b4e4d92017-09-25 12:18:29 +020053
54ok($q = $q->normalize->finalize, 'Prepare query');
55is($q->to_string, "constr(pos=precedes,between=1-1,notBetween=/[ac]./:aa,bb)",
56 'Stringification');
57
58ok($q = $q->identify($index->dict), 'Prepare query');
Akron3d1df332017-12-23 16:21:21 +010059is($q->to_string(1), "constr(pos=precedes,between=1-1,notBetween=#10|#12|#2|#8:#2,#4)",
Akron4f9eef42017-07-24 11:41:09 +020060 'Stringification');
Akron7b4e4d92017-09-25 12:18:29 +020061
Akron3ab2e972017-08-02 19:10:10 +020062ok($q = $q->optimize($index->segment), 'Prepare query');
Akron92d9f672017-08-16 12:23:11 +020063is($q->to_string, "constr(pos=1,between=1-1,notBetween=or(or(or(#10,#12),#8),#2):#2,#4)",
Akron3ab2e972017-08-02 19:10:10 +020064 'Stringification');
Akron4f9eef42017-07-24 11:41:09 +020065
66# Constraints: One operand is missing
Akron95a37a42017-11-20 17:16:40 +010067ok($q = $qb->constraint(
Akron4f9eef42017-07-24 11:41:09 +020068 [
Akron581e8932017-12-11 14:25:03 +010069 $qb->c_class_between(2)
Akron4f9eef42017-07-24 11:41:09 +020070 ],
71 $qb->term_re('[ac].'),
72 $qb->term_re('b[a]'),
73), 'Regex in class');
74ok($q = $q->normalize->finalize, 'Prepare query');
75is($q->to_string, "constr(class=2:/[ac]./,/b[a]/)", 'Stringification');
Akron3ab2e972017-08-02 19:10:10 +020076ok($q = $q->identify($index->dict), 'Prepare query');
Akron4f9eef42017-07-24 11:41:09 +020077is($q->to_string, "[0]", 'Stringification');
78ok($q = $q->optimize($index), 'Prepare query');
79is($q->to_string, "[0]", 'Stringification');
80
Akron3ab2e972017-08-02 19:10:10 +020081
Akron4f9eef42017-07-24 11:41:09 +020082# Constraints: One constraint fails
Akron95a37a42017-11-20 17:16:40 +010083ok($q = $qb->constraint(
Akron4f9eef42017-07-24 11:41:09 +020084 [
85 $qb->c_not_between(
86 $qb->term_re('[e].')
87 )
88 ],
89 $qb->term_re('[ac].'),
90 $qb->term_re('b.'),
91), 'Regex in class');
Akron4f9eef42017-07-24 11:41:09 +020092is($q->to_string, "constr(notBetween=/[e]./:/[ac]./,/b./)", 'Stringification');
Akron6ea3b812017-07-25 16:08:46 +020093ok($q = $q->normalize->finalize, 'Prepare query');
94is($q->to_string, "constr(pos=precedes;succeeds,between=1-1,notBetween=/[e]./:/[ac]./,/b./)", 'Stringification');
Akron3ab2e972017-08-02 19:10:10 +020095ok($q = $q->identify($index->dict), 'Prepare query');
96is($q->to_string,
Akron3d1df332017-12-23 16:21:21 +010097 "constr(pos=precedes;succeeds,between=1-1,notBetween=[0]:#10|#12|#2|#8,#4|#6)",
Akron3ab2e972017-08-02 19:10:10 +020098 'Stringification');
99ok($q = $q->optimize($index->segment), 'Prepare query');
100is($q->to_string,
Akron92d9f672017-08-16 12:23:11 +0200101 "constr(pos=4097,between=1-1:or(or(or(#10,#12),#8),#2),or(#6,#4))",
Akron3ab2e972017-08-02 19:10:10 +0200102 'Stringification');
103
Akron4f9eef42017-07-24 11:41:09 +0200104
Akron4f9eef42017-07-24 11:41:09 +0200105
106# Sequence
107ok($q = $qb->seq(
108 $qb->term('aa'),
109 $qb->term_re('[ac].'),
110), 'Regex in sequence');
Akron3ab2e972017-08-02 19:10:10 +0200111ok($q = $q->normalize->finalize->identify($index->dict)->optimize($index->segment), 'Prepare query');
Akron92d9f672017-08-16 12:23:11 +0200112is($q->to_string, "constr(pos=2048:or(or(or(#10,#12),#8),#2),#2)", 'Stringification');
Akron4f9eef42017-07-24 11:41:09 +0200113
114
115
116TODO: {
117 local $TODO = 'Test with more queries and corpus query';
118};
119
120
121done_testing;
122
123__END__