| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 1 | use Test::More; |
| 2 | use Test::Krawfish; |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | use_ok('Krawfish::Index'); |
| 7 | use_ok('Krawfish::Koral::Query::Builder'); |
| 8 | |
| 9 | my $index = Krawfish::Index->new; |
| Akron | 56422cf | 2017-08-16 14:17:01 +0200 | [diff] [blame] | 10 | ok_index($index, [qw/aa bb aa bc ac bb cc ca/], 'Add complex document'); |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 11 | |
| 12 | # Simple |
| 13 | ok(my $qb = Krawfish::Koral::Query::Builder->new, 'Create Builder'); |
| 14 | ok(my $q = $qb->term_re('[ac].'), 'Regex'); |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 15 | ok($q = $q->normalize->finalize, 'Prepare query'); |
| Akron | 7b4e4d9 | 2017-09-25 12:18:29 +0200 | [diff] [blame] | 16 | |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 17 | is($q->to_string, "/[ac]./", 'Stringification'); |
| 18 | ok($q = $q->identify($index->dict), 'Prepare query'); |
| Akron | 3d1df33 | 2017-12-23 16:21:21 +0100 | [diff] [blame] | 19 | is($q->to_string(1), '#10|#12|#2|#8', 'Stringification'); |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 20 | |
| 21 | ok($q = $q->optimize($index->segment), 'Prepare query'); |
| Akron | 92d9f67 | 2017-08-16 12:23:11 +0200 | [diff] [blame] | 22 | is($q->to_string, "or(or(or(#10,#12),#8),#2)", 'Stringification'); |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 23 | |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 24 | # Class |
| 25 | ok($q = $qb->class( |
| 26 | $qb->term_re('[ac].'), |
| 27 | 2), 'Regex in class'); |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 28 | ok($q = $q->normalize->finalize->identify($index->dict)->optimize($index->segment), 'Prepare query'); |
| Akron | 92d9f67 | 2017-08-16 12:23:11 +0200 | [diff] [blame] | 29 | is($q->to_string, "class(2:or(or(or(#10,#12),#8),#2))", 'Stringification'); |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 30 | |
| 31 | # Constraints |
| Akron | 95a37a4 | 2017-11-20 17:16:40 +0100 | [diff] [blame] | 32 | ok($q = $qb->constraint( |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 33 | [$qb->c_position('precedes')], |
| 34 | $qb->term_re('[ac].'), |
| 35 | $qb->term_re('b.') |
| 36 | ), 'Regex in constraint'); |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 37 | ok($q = $q->normalize->finalize->identify($index->dict)->optimize($index->segment), 'Prepare query'); |
| Akron | 92d9f67 | 2017-08-16 12:23:11 +0200 | [diff] [blame] | 38 | is($q->to_string, "constr(pos=1:or(or(or(#10,#12),#8),#2),or(#6,#4))", |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 39 | 'Stringification'); |
| 40 | |
| 41 | |
| 42 | # Constraints: Inflate in constraint |
| Akron | 95a37a4 | 2017-11-20 17:16:40 +0100 | [diff] [blame] | 43 | ok($q = $qb->constraint( |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 44 | [ |
| 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'); |
| Akron | 7b4e4d9 | 2017-09-25 12:18:29 +0200 | [diff] [blame] | 53 | |
| 54 | ok($q = $q->normalize->finalize, 'Prepare query'); |
| 55 | is($q->to_string, "constr(pos=precedes,between=1-1,notBetween=/[ac]./:aa,bb)", |
| 56 | 'Stringification'); |
| 57 | |
| 58 | ok($q = $q->identify($index->dict), 'Prepare query'); |
| Akron | 3d1df33 | 2017-12-23 16:21:21 +0100 | [diff] [blame] | 59 | is($q->to_string(1), "constr(pos=precedes,between=1-1,notBetween=#10|#12|#2|#8:#2,#4)", |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 60 | 'Stringification'); |
| Akron | 7b4e4d9 | 2017-09-25 12:18:29 +0200 | [diff] [blame] | 61 | |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 62 | ok($q = $q->optimize($index->segment), 'Prepare query'); |
| Akron | 92d9f67 | 2017-08-16 12:23:11 +0200 | [diff] [blame] | 63 | is($q->to_string, "constr(pos=1,between=1-1,notBetween=or(or(or(#10,#12),#8),#2):#2,#4)", |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 64 | 'Stringification'); |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 65 | |
| 66 | # Constraints: One operand is missing |
| Akron | 95a37a4 | 2017-11-20 17:16:40 +0100 | [diff] [blame] | 67 | ok($q = $qb->constraint( |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 68 | [ |
| Akron | 581e893 | 2017-12-11 14:25:03 +0100 | [diff] [blame] | 69 | $qb->c_class_between(2) |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 70 | ], |
| 71 | $qb->term_re('[ac].'), |
| 72 | $qb->term_re('b[a]'), |
| 73 | ), 'Regex in class'); |
| 74 | ok($q = $q->normalize->finalize, 'Prepare query'); |
| 75 | is($q->to_string, "constr(class=2:/[ac]./,/b[a]/)", 'Stringification'); |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 76 | ok($q = $q->identify($index->dict), 'Prepare query'); |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 77 | is($q->to_string, "[0]", 'Stringification'); |
| 78 | ok($q = $q->optimize($index), 'Prepare query'); |
| 79 | is($q->to_string, "[0]", 'Stringification'); |
| 80 | |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 81 | |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 82 | # Constraints: One constraint fails |
| Akron | 95a37a4 | 2017-11-20 17:16:40 +0100 | [diff] [blame] | 83 | ok($q = $qb->constraint( |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 84 | [ |
| 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'); |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 92 | is($q->to_string, "constr(notBetween=/[e]./:/[ac]./,/b./)", 'Stringification'); |
| Akron | 6ea3b81 | 2017-07-25 16:08:46 +0200 | [diff] [blame] | 93 | ok($q = $q->normalize->finalize, 'Prepare query'); |
| 94 | is($q->to_string, "constr(pos=precedes;succeeds,between=1-1,notBetween=/[e]./:/[ac]./,/b./)", 'Stringification'); |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 95 | ok($q = $q->identify($index->dict), 'Prepare query'); |
| 96 | is($q->to_string, |
| Akron | 3d1df33 | 2017-12-23 16:21:21 +0100 | [diff] [blame] | 97 | "constr(pos=precedes;succeeds,between=1-1,notBetween=[0]:#10|#12|#2|#8,#4|#6)", |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 98 | 'Stringification'); |
| 99 | ok($q = $q->optimize($index->segment), 'Prepare query'); |
| 100 | is($q->to_string, |
| Akron | 92d9f67 | 2017-08-16 12:23:11 +0200 | [diff] [blame] | 101 | "constr(pos=4097,between=1-1:or(or(or(#10,#12),#8),#2),or(#6,#4))", |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 102 | 'Stringification'); |
| 103 | |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 104 | |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 105 | |
| 106 | # Sequence |
| 107 | ok($q = $qb->seq( |
| 108 | $qb->term('aa'), |
| 109 | $qb->term_re('[ac].'), |
| 110 | ), 'Regex in sequence'); |
| Akron | 3ab2e97 | 2017-08-02 19:10:10 +0200 | [diff] [blame] | 111 | ok($q = $q->normalize->finalize->identify($index->dict)->optimize($index->segment), 'Prepare query'); |
| Akron | 92d9f67 | 2017-08-16 12:23:11 +0200 | [diff] [blame] | 112 | is($q->to_string, "constr(pos=2048:or(or(or(#10,#12),#8),#2),#2)", 'Stringification'); |
| Akron | 4f9eef4 | 2017-07-24 11:41:09 +0200 | [diff] [blame] | 113 | |
| 114 | |
| 115 | |
| 116 | TODO: { |
| 117 | local $TODO = 'Test with more queries and corpus query'; |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | done_testing; |
| 122 | |
| 123 | __END__ |