| Akron | 818e852 | 2017-07-22 12:34:01 +0200 | [diff] [blame] | 1 | use Test::More; |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use Data::Dumper; |
| 5 | |
| 6 | use_ok('Krawfish::Koral'); |
| 7 | |
| 8 | my $koral = Krawfish::Koral->new; |
| 9 | |
| 10 | my $qb = $koral->query_builder; |
| 11 | |
| 12 | $koral->query( |
| 13 | $qb->seq( |
| 14 | $qb->term('x'), |
| 15 | $qb->repeat($qb->any, 2,3), |
| 16 | $qb->token( |
| Akron | b945c57 | 2017-07-23 14:55:00 +0200 | [diff] [blame] | 17 | $qb->bool_and( |
| Akron | 818e852 | 2017-07-22 12:34:01 +0200 | [diff] [blame] | 18 | 'a', |
| Akron | b945c57 | 2017-07-23 14:55:00 +0200 | [diff] [blame] | 19 | $qb->bool_or('b','c','d'), |
| Akron | 818e852 | 2017-07-22 12:34:01 +0200 | [diff] [blame] | 20 | 'e' |
| 21 | ) |
| 22 | ) |
| 23 | ) |
| 24 | ); |
| 25 | |
| 26 | my $serial = $koral->to_koral_query; |
| 27 | ok(!$serial->{corpus}, 'Corpus exists not'); |
| 28 | ok(my $query = $serial->{query}, 'Serialization successful'); |
| 29 | |
| 30 | is($query->{'@type'}, 'koral:group', 'Group'); |
| 31 | is($query->{'operation'}, 'operation:sequence', 'operation'); |
| 32 | ok($query->{'operands'}, 'operands'); |
| 33 | |
| 34 | my $ops = $query->{operands}; |
| 35 | my $op = $ops->[0]; |
| 36 | |
| 37 | is($op->{'@type'}, 'koral:term', 'Operand is a term'); |
| 38 | is($op->{key}, 'x', 'Operand is a term'); |
| 39 | |
| 40 | $op = $ops->[1]; |
| 41 | is($op->{'@type'}, 'koral:group', 'Operand is a group'); |
| 42 | is($op->{'operation'}, 'operation:repetition', 'operation'); |
| 43 | ok($op->{'operands'}, 'operands'); |
| 44 | ok($op->{boundary}, 'Operand has a boundary'); |
| 45 | |
| 46 | $op = $op->{operands}->[0]; |
| 47 | is($op->{'@type'}, 'koral:token', 'Operand is just a token'); |
| 48 | is(scalar(keys %{$op}), 1, 'Nothing more'); |
| 49 | |
| 50 | my $bound = $ops->[1]->{boundary}; |
| 51 | is($bound->{'@type'}, 'koral:boundary', 'Boundary type'); |
| 52 | is($bound->{'min'}, 2, 'min'); |
| 53 | is($bound->{'max'}, 3, 'max'); |
| 54 | |
| 55 | $op = $ops->[2]; |
| 56 | is($op->{'@type'}, 'koral:token', 'Operand is a token'); |
| 57 | ok($op->{'wrap'}, 'wrap'); |
| 58 | |
| 59 | $op = $op->{wrap}; |
| 60 | is($op->{'@type'}, 'koral:group', 'Operand is a group'); |
| 61 | is($op->{'operation'}, 'operation:termGroup', 'operation'); |
| 62 | is($op->{'relation'}, 'relation:and', 'operation'); |
| 63 | ok($op->{'operands'}, 'operands'); |
| 64 | |
| 65 | $ops = $op->{operands}; |
| 66 | $op = $ops->[0]; |
| 67 | is($op->{'@type'}, 'koral:term', 'Operand is a term'); |
| 68 | is($op->{key}, 'a', 'Operand is a term'); |
| 69 | |
| 70 | $op = $ops->[2]; |
| 71 | is($op->{'@type'}, 'koral:term', 'Operand is a term'); |
| 72 | is($op->{key}, 'e', 'Operand is a term'); |
| 73 | |
| 74 | $op = $ops->[1]; |
| 75 | is($op->{'@type'}, 'koral:group', 'Operand is a group'); |
| 76 | is($op->{'operation'}, 'operation:termGroup', 'operation'); |
| 77 | is($op->{'relation'}, 'relation:or', 'operation'); |
| 78 | ok($op->{'operands'}, 'operands'); |
| 79 | |
| 80 | $ops = $op->{operands}; |
| 81 | $op = $ops->[0]; |
| 82 | is($op->{'@type'}, 'koral:term', 'Operand is a term'); |
| 83 | is($op->{key}, 'b', 'Operand is a term'); |
| 84 | |
| 85 | $op = $ops->[1]; |
| 86 | is($op->{'@type'}, 'koral:term', 'Operand is a term'); |
| 87 | is($op->{key}, 'c', 'Operand is a term'); |
| 88 | |
| 89 | $op = $ops->[2]; |
| 90 | is($op->{'@type'}, 'koral:term', 'Operand is a term'); |
| 91 | is($op->{key}, 'd', 'Operand is a term'); |
| 92 | |
| 93 | |
| 94 | TODO: { |
| 95 | local $TODO = 'Test Serialization output'; |
| Akron | 8944098 | 2017-07-28 14:48:28 +0200 | [diff] [blame^] | 96 | ok($koral = $koral->to_nodes, 'Finalize query'); |
| Akron | 818e852 | 2017-07-22 12:34:01 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | |
| 100 | done_testing; |
| 101 | __END__ |
| 102 | |