| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 1 | use Test::More; |
| Akron | e020194 | 2016-11-26 01:11:31 +0100 | [diff] [blame] | 2 | use Test::Krawfish; |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 3 | use strict; |
| 4 | use warnings; |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 5 | |
| 6 | use_ok('Krawfish::Koral'); |
| 7 | use_ok('Krawfish::Index'); |
| 8 | |
| 9 | my $index = Krawfish::Index->new; |
| 10 | |
| Akron | e020194 | 2016-11-26 01:11:31 +0100 | [diff] [blame] | 11 | ok_index($index, '<1:aaa>[hey][hey]</1>', 'Add new document'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 12 | |
| 13 | my $koral = Krawfish::Koral->new; |
| 14 | |
| 15 | my $builder = $koral->query_builder; |
| 16 | |
| 17 | # [hey]{0,3} |
| 18 | my $rep = $builder->repeat($builder->token('hey'), 0, 3); |
| 19 | is($rep->to_string, '[hey]{0,3}', 'Stringification'); |
| 20 | ok(!$rep->is_any, 'Is not any'); |
| 21 | ok($rep->is_optional, 'Is optional'); |
| 22 | ok(!$rep->is_null, 'Is not null'); |
| 23 | ok(!$rep->is_negative, 'Is not negative'); |
| 24 | ok(!$rep->is_extended, 'Is not extended'); |
| 25 | ok(!$rep->is_extended_right, 'Is not extended to the right'); |
| 26 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| 27 | |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 28 | # TODO: Probably better to warn here |
| 29 | ok(!$rep->plan_for($index), 'Unable to stringify'); |
| 30 | ok($rep->has_error, 'Error set'); |
| 31 | is($rep->error->[0]->[1], 'Optionality is ignored', 'Error'); |
| 32 | |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 33 | # [hey]{1,3} |
| 34 | $rep = $builder->repeat($builder->token('hey'), 1, 3); |
| 35 | is($rep->to_string, '[hey]{1,3}', 'Stringification'); |
| 36 | ok(!$rep->is_any, 'Is not any'); |
| 37 | ok(!$rep->is_optional, 'Is not optional'); |
| 38 | ok(!$rep->is_null, 'Is not null'); |
| 39 | ok(!$rep->is_negative, 'Is not negative'); |
| 40 | ok(!$rep->is_extended, 'Is not extended'); |
| 41 | ok(!$rep->is_extended_right, 'Is not extended to the right'); |
| 42 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 43 | is($rep->plan_for($index)->to_string, "rep(1-3:'hey')", 'Stringification'); |
| 44 | ok(!$rep->has_error, 'Error not set'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 45 | |
| 46 | # [hey]{2,} |
| 47 | $rep = $builder->repeat($builder->token('hey'), 2, undef); |
| 48 | is($rep->to_string, '[hey]{2,}', 'Stringification'); |
| 49 | ok(!$rep->is_any, 'Is not any'); |
| 50 | ok(!$rep->is_optional, 'Is not optional'); |
| 51 | ok(!$rep->is_null, 'Is not null'); |
| 52 | ok(!$rep->is_negative, 'Is not negative'); |
| 53 | ok(!$rep->is_extended, 'Is not extended'); |
| 54 | ok(!$rep->is_extended_right, 'Is not extended to the right'); |
| 55 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 56 | is($rep->plan_for($index)->to_string, "rep(2-100:'hey')", 'Stringification'); |
| 57 | ok(!$rep->has_error, 'Error not set'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 58 | |
| Akron | a526b9a | 2016-11-15 17:16:17 +0100 | [diff] [blame] | 59 | # [hey]* |
| 60 | $rep = $builder->repeat($builder->token('hey'), undef, undef); |
| 61 | is($rep->to_string, '[hey]*', 'Stringification'); |
| 62 | ok(!$rep->is_any, 'Is not any'); |
| Akron | d306aa9 | 2016-11-15 20:46:26 +0100 | [diff] [blame] | 63 | ok($rep->is_optional, 'Is not optional'); |
| Akron | a526b9a | 2016-11-15 17:16:17 +0100 | [diff] [blame] | 64 | ok(!$rep->is_null, 'Is not null'); |
| 65 | ok(!$rep->is_negative, 'Is not negative'); |
| 66 | ok(!$rep->is_extended, 'Is not extended'); |
| 67 | ok(!$rep->is_extended_right, 'Is not extended to the right'); |
| 68 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | d306aa9 | 2016-11-15 20:46:26 +0100 | [diff] [blame] | 69 | ok(!$rep->plan_for($index), 'Unplannable'); |
| 70 | ok($rep->has_error, 'Error set'); |
| 71 | is($rep->error->[0]->[1], 'Optionality is ignored', 'Error'); |
| Akron | a526b9a | 2016-11-15 17:16:17 +0100 | [diff] [blame] | 72 | |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 73 | # [hey]{0,2} |
| 74 | $rep = $builder->repeat($builder->token('hey'), undef, 2); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 75 | is($rep->to_string, '[hey]{,2}', 'Stringification'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 76 | ok(!$rep->is_any, 'Is not any'); |
| 77 | ok($rep->is_optional, 'Is optional'); |
| 78 | ok(!$rep->is_null, 'Is not null'); |
| 79 | ok(!$rep->is_negative, 'Is not negative'); |
| 80 | ok(!$rep->is_extended, 'Is not extended'); |
| 81 | ok(!$rep->is_extended_right, 'Is not extended to the right'); |
| 82 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 83 | ok(!$rep->plan_for($index), 'Unplannable'); |
| 84 | ok($rep->has_error, 'Error set'); |
| 85 | is($rep->error->[0]->[1], 'Optionality is ignored', 'Error'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 86 | |
| 87 | # [hey]{3} |
| 88 | $rep = $builder->repeat($builder->token('hey'), 3, 3); |
| 89 | is($rep->to_string, '[hey]{3}', 'Stringification'); |
| 90 | ok(!$rep->is_any, 'Is not any'); |
| 91 | ok(!$rep->is_optional, 'Is not optional'); |
| 92 | ok(!$rep->is_null, 'Is not null'); |
| 93 | ok(!$rep->is_negative, 'Is not negative'); |
| 94 | ok(!$rep->is_extended, 'Is not extended'); |
| 95 | ok(!$rep->is_extended_right, 'Is not extended to the right'); |
| 96 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 97 | is($rep->plan_for($index)->to_string, "rep(3-3:'hey')", 'Planned'); |
| 98 | ok(!$rep->has_error, 'Error not set'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 99 | |
| 100 | # []{2,4} |
| 101 | $rep = $builder->repeat($builder->token, 2, 4); |
| 102 | is($rep->to_string, '[]{2,4}', 'Stringification'); |
| 103 | ok($rep->is_any, 'Is any'); |
| 104 | ok(!$rep->is_optional, 'Is not optional'); |
| 105 | ok(!$rep->is_null, 'Is not null'); |
| 106 | ok(!$rep->is_negative, 'Is not negative'); |
| 107 | ok($rep->is_extended, 'Is extended'); |
| 108 | ok($rep->is_extended_right, 'Is extended to the right'); |
| 109 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 110 | ok(!$rep->plan_for($index), 'Unplannable'); |
| 111 | ok($rep->has_error, 'Error set'); |
| 112 | is($rep->error->[0]->[1], 'Unable to search for any tokens', 'Error'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 113 | |
| 114 | # []{,4} |
| 115 | $rep = $builder->repeat($builder->token, 0, 4); |
| 116 | is($rep->to_string, '[]{0,4}', 'Stringification'); |
| 117 | ok($rep->is_any, 'Is any'); |
| 118 | ok($rep->is_optional, 'Is optional'); |
| 119 | ok(!$rep->is_null, 'Is not null'); |
| 120 | ok(!$rep->is_negative, 'Is not negative'); |
| 121 | ok($rep->is_extended, 'Is extended'); |
| 122 | ok($rep->is_extended_right, 'Is extended to the right'); |
| 123 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 124 | ok(!$rep->plan_for($index), 'Unplannable'); |
| 125 | ok($rep->has_error, 'Error set'); |
| 126 | is($rep->error->[0]->[1], 'Unable to search for any tokens', 'Error'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 127 | |
| 128 | # []{4,} |
| 129 | $rep = $builder->repeat($builder->token, 4, undef); |
| 130 | is($rep->to_string, '[]{4,}', 'Stringification'); |
| 131 | ok($rep->is_any, 'Is any'); |
| 132 | ok(!$rep->is_optional, 'Is not optional'); |
| 133 | ok(!$rep->is_null, 'Is not null'); |
| 134 | ok(!$rep->is_negative, 'Is not negative'); |
| 135 | ok($rep->is_extended, 'Is extended'); |
| 136 | ok($rep->is_extended_right, 'Is extended to the right'); |
| 137 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 138 | ok(!$rep->plan_for($index), 'Unplannable'); |
| 139 | ok($rep->has_error, 'Error set'); |
| 140 | is($rep->error->[0]->[1], 'Unable to search for any tokens', 'Error'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 141 | |
| 142 | # []{8} |
| 143 | $rep = $builder->repeat($builder->token, 8); |
| 144 | is($rep->to_string, '[]{8}', 'Stringification'); |
| 145 | ok($rep->is_any, 'Is any'); |
| 146 | ok(!$rep->is_optional, 'Is not optional'); |
| 147 | ok(!$rep->is_null, 'Is not null'); |
| 148 | ok(!$rep->is_negative, 'Is not negative'); |
| 149 | ok($rep->is_extended, 'Is extended'); |
| 150 | ok($rep->is_extended_right, 'Is extended to the right'); |
| 151 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 152 | ok(!$rep->plan_for($index), 'Unplannable'); |
| 153 | ok($rep->has_error, 'Error set'); |
| 154 | is($rep->error->[0]->[1], 'Unable to search for any tokens', 'Error'); |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 155 | |
| 156 | # <x>{2,3} |
| 157 | $rep = $builder->repeat($builder->span('aaa'), 2,3); |
| 158 | is($rep->to_string, '<aaa>{2,3}', 'Stringification'); |
| 159 | ok(!$rep->is_any, 'Is not any'); |
| 160 | ok(!$rep->is_optional, 'Is not optional'); |
| 161 | ok(!$rep->is_null, 'Is not null'); |
| 162 | ok(!$rep->is_negative, 'Is not negative'); |
| 163 | ok(!$rep->is_extended, 'Is not extended'); |
| 164 | ok(!$rep->is_extended_right, 'Is not extended to the right'); |
| 165 | ok(!$rep->is_extended_left, 'Is not extended to the left'); |
| 166 | |
| Akron | 8aee4a6 | 2016-11-14 21:33:12 +0100 | [diff] [blame] | 167 | is($rep->plan_for($index)->to_string, "rep(2-3:'<>aaa')", 'Planned'); |
| 168 | |
| Akron | c697fc2 | 2016-11-11 02:34:56 +0100 | [diff] [blame] | 169 | |
| 170 | done_testing; |
| 171 | |
| 172 | __END__ |
| 173 | |
| 174 | |
| 175 | |