blob: 13e1d769d6f89e417ed3911ea9682ad51399dff3 [file] [log] [blame]
Akronc697fc22016-11-11 02:34:56 +01001use Test::More;
Akrone0201942016-11-26 01:11:31 +01002use Test::Krawfish;
Akronc697fc22016-11-11 02:34:56 +01003use strict;
4use warnings;
Akronc697fc22016-11-11 02:34:56 +01005
6use_ok('Krawfish::Koral');
7use_ok('Krawfish::Index');
8
9my $index = Krawfish::Index->new;
10
Akrone0201942016-11-26 01:11:31 +010011ok_index($index, '<1:aaa>[hey][hey]</1>', 'Add new document');
Akronc697fc22016-11-11 02:34:56 +010012
13my $koral = Krawfish::Koral->new;
14
15my $builder = $koral->query_builder;
16
17# [hey]{0,3}
18my $rep = $builder->repeat($builder->token('hey'), 0, 3);
19is($rep->to_string, '[hey]{0,3}', 'Stringification');
20ok(!$rep->is_any, 'Is not any');
21ok($rep->is_optional, 'Is optional');
22ok(!$rep->is_null, 'Is not null');
23ok(!$rep->is_negative, 'Is not negative');
24ok(!$rep->is_extended, 'Is not extended');
25ok(!$rep->is_extended_right, 'Is not extended to the right');
26ok(!$rep->is_extended_left, 'Is not extended to the left');
27
Akron8aee4a62016-11-14 21:33:12 +010028# TODO: Probably better to warn here
29ok(!$rep->plan_for($index), 'Unable to stringify');
30ok($rep->has_error, 'Error set');
31is($rep->error->[0]->[1], 'Optionality is ignored', 'Error');
32
Akronc697fc22016-11-11 02:34:56 +010033# [hey]{1,3}
34$rep = $builder->repeat($builder->token('hey'), 1, 3);
35is($rep->to_string, '[hey]{1,3}', 'Stringification');
36ok(!$rep->is_any, 'Is not any');
37ok(!$rep->is_optional, 'Is not optional');
38ok(!$rep->is_null, 'Is not null');
39ok(!$rep->is_negative, 'Is not negative');
40ok(!$rep->is_extended, 'Is not extended');
41ok(!$rep->is_extended_right, 'Is not extended to the right');
42ok(!$rep->is_extended_left, 'Is not extended to the left');
Akron8aee4a62016-11-14 21:33:12 +010043is($rep->plan_for($index)->to_string, "rep(1-3:'hey')", 'Stringification');
44ok(!$rep->has_error, 'Error not set');
Akronc697fc22016-11-11 02:34:56 +010045
46# [hey]{2,}
47$rep = $builder->repeat($builder->token('hey'), 2, undef);
48is($rep->to_string, '[hey]{2,}', 'Stringification');
49ok(!$rep->is_any, 'Is not any');
50ok(!$rep->is_optional, 'Is not optional');
51ok(!$rep->is_null, 'Is not null');
52ok(!$rep->is_negative, 'Is not negative');
53ok(!$rep->is_extended, 'Is not extended');
54ok(!$rep->is_extended_right, 'Is not extended to the right');
55ok(!$rep->is_extended_left, 'Is not extended to the left');
Akron8aee4a62016-11-14 21:33:12 +010056is($rep->plan_for($index)->to_string, "rep(2-100:'hey')", 'Stringification');
57ok(!$rep->has_error, 'Error not set');
Akronc697fc22016-11-11 02:34:56 +010058
Akrona526b9a2016-11-15 17:16:17 +010059# [hey]*
60$rep = $builder->repeat($builder->token('hey'), undef, undef);
61is($rep->to_string, '[hey]*', 'Stringification');
62ok(!$rep->is_any, 'Is not any');
Akrond306aa92016-11-15 20:46:26 +010063ok($rep->is_optional, 'Is not optional');
Akrona526b9a2016-11-15 17:16:17 +010064ok(!$rep->is_null, 'Is not null');
65ok(!$rep->is_negative, 'Is not negative');
66ok(!$rep->is_extended, 'Is not extended');
67ok(!$rep->is_extended_right, 'Is not extended to the right');
68ok(!$rep->is_extended_left, 'Is not extended to the left');
Akrond306aa92016-11-15 20:46:26 +010069ok(!$rep->plan_for($index), 'Unplannable');
70ok($rep->has_error, 'Error set');
71is($rep->error->[0]->[1], 'Optionality is ignored', 'Error');
Akrona526b9a2016-11-15 17:16:17 +010072
Akronc697fc22016-11-11 02:34:56 +010073# [hey]{0,2}
74$rep = $builder->repeat($builder->token('hey'), undef, 2);
Akron8aee4a62016-11-14 21:33:12 +010075is($rep->to_string, '[hey]{,2}', 'Stringification');
Akronc697fc22016-11-11 02:34:56 +010076ok(!$rep->is_any, 'Is not any');
77ok($rep->is_optional, 'Is optional');
78ok(!$rep->is_null, 'Is not null');
79ok(!$rep->is_negative, 'Is not negative');
80ok(!$rep->is_extended, 'Is not extended');
81ok(!$rep->is_extended_right, 'Is not extended to the right');
82ok(!$rep->is_extended_left, 'Is not extended to the left');
Akron8aee4a62016-11-14 21:33:12 +010083ok(!$rep->plan_for($index), 'Unplannable');
84ok($rep->has_error, 'Error set');
85is($rep->error->[0]->[1], 'Optionality is ignored', 'Error');
Akronc697fc22016-11-11 02:34:56 +010086
87# [hey]{3}
88$rep = $builder->repeat($builder->token('hey'), 3, 3);
89is($rep->to_string, '[hey]{3}', 'Stringification');
90ok(!$rep->is_any, 'Is not any');
91ok(!$rep->is_optional, 'Is not optional');
92ok(!$rep->is_null, 'Is not null');
93ok(!$rep->is_negative, 'Is not negative');
94ok(!$rep->is_extended, 'Is not extended');
95ok(!$rep->is_extended_right, 'Is not extended to the right');
96ok(!$rep->is_extended_left, 'Is not extended to the left');
Akron8aee4a62016-11-14 21:33:12 +010097is($rep->plan_for($index)->to_string, "rep(3-3:'hey')", 'Planned');
98ok(!$rep->has_error, 'Error not set');
Akronc697fc22016-11-11 02:34:56 +010099
100# []{2,4}
101$rep = $builder->repeat($builder->token, 2, 4);
102is($rep->to_string, '[]{2,4}', 'Stringification');
103ok($rep->is_any, 'Is any');
104ok(!$rep->is_optional, 'Is not optional');
105ok(!$rep->is_null, 'Is not null');
106ok(!$rep->is_negative, 'Is not negative');
107ok($rep->is_extended, 'Is extended');
108ok($rep->is_extended_right, 'Is extended to the right');
109ok(!$rep->is_extended_left, 'Is not extended to the left');
Akron8aee4a62016-11-14 21:33:12 +0100110ok(!$rep->plan_for($index), 'Unplannable');
111ok($rep->has_error, 'Error set');
112is($rep->error->[0]->[1], 'Unable to search for any tokens', 'Error');
Akronc697fc22016-11-11 02:34:56 +0100113
114# []{,4}
115$rep = $builder->repeat($builder->token, 0, 4);
116is($rep->to_string, '[]{0,4}', 'Stringification');
117ok($rep->is_any, 'Is any');
118ok($rep->is_optional, 'Is optional');
119ok(!$rep->is_null, 'Is not null');
120ok(!$rep->is_negative, 'Is not negative');
121ok($rep->is_extended, 'Is extended');
122ok($rep->is_extended_right, 'Is extended to the right');
123ok(!$rep->is_extended_left, 'Is not extended to the left');
Akron8aee4a62016-11-14 21:33:12 +0100124ok(!$rep->plan_for($index), 'Unplannable');
125ok($rep->has_error, 'Error set');
126is($rep->error->[0]->[1], 'Unable to search for any tokens', 'Error');
Akronc697fc22016-11-11 02:34:56 +0100127
128# []{4,}
129$rep = $builder->repeat($builder->token, 4, undef);
130is($rep->to_string, '[]{4,}', 'Stringification');
131ok($rep->is_any, 'Is any');
132ok(!$rep->is_optional, 'Is not optional');
133ok(!$rep->is_null, 'Is not null');
134ok(!$rep->is_negative, 'Is not negative');
135ok($rep->is_extended, 'Is extended');
136ok($rep->is_extended_right, 'Is extended to the right');
137ok(!$rep->is_extended_left, 'Is not extended to the left');
Akron8aee4a62016-11-14 21:33:12 +0100138ok(!$rep->plan_for($index), 'Unplannable');
139ok($rep->has_error, 'Error set');
140is($rep->error->[0]->[1], 'Unable to search for any tokens', 'Error');
Akronc697fc22016-11-11 02:34:56 +0100141
142# []{8}
143$rep = $builder->repeat($builder->token, 8);
144is($rep->to_string, '[]{8}', 'Stringification');
145ok($rep->is_any, 'Is any');
146ok(!$rep->is_optional, 'Is not optional');
147ok(!$rep->is_null, 'Is not null');
148ok(!$rep->is_negative, 'Is not negative');
149ok($rep->is_extended, 'Is extended');
150ok($rep->is_extended_right, 'Is extended to the right');
151ok(!$rep->is_extended_left, 'Is not extended to the left');
Akron8aee4a62016-11-14 21:33:12 +0100152ok(!$rep->plan_for($index), 'Unplannable');
153ok($rep->has_error, 'Error set');
154is($rep->error->[0]->[1], 'Unable to search for any tokens', 'Error');
Akronc697fc22016-11-11 02:34:56 +0100155
156# <x>{2,3}
157$rep = $builder->repeat($builder->span('aaa'), 2,3);
158is($rep->to_string, '<aaa>{2,3}', 'Stringification');
159ok(!$rep->is_any, 'Is not any');
160ok(!$rep->is_optional, 'Is not optional');
161ok(!$rep->is_null, 'Is not null');
162ok(!$rep->is_negative, 'Is not negative');
163ok(!$rep->is_extended, 'Is not extended');
164ok(!$rep->is_extended_right, 'Is not extended to the right');
165ok(!$rep->is_extended_left, 'Is not extended to the left');
166
Akron8aee4a62016-11-14 21:33:12 +0100167is($rep->plan_for($index)->to_string, "rep(2-3:'<>aaa')", 'Planned');
168
Akronc697fc22016-11-11 02:34:56 +0100169
170done_testing;
171
172__END__
173
174
175