Debian Codegraph @

t/02-evaluate.t

raw
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Test2::V0;
7
8 use Cucumber::TagExpressions;
9
10
11 my @good = (
12 { expr => '@a\ or @b',
13 tests => [ { tags => [ '@a\ ' ], outcome => !!0 },
14 { tags => [ '@a ' ], outcome => 1 },
15 { tags => [ '@b' ], outcome => 1 },
16 { tags => [ '@a' ], outcome => !!0 },
17 ] },
18 { expr => "\@a\\\\", tests => [] },
19 { expr => '@a',
20 tests => [ { tags => [ qw/@a/ ], outcome => 1 },
21 { tags => [ qw/@b/ ], outcome => !!0 },
22 { tags => [ qw/@a @b/ ], outcome => 1 },
23 ] },
24 { expr => 'not @a',
25 tests => [ { tags => [ qw/@a/ ], outcome => !!0 },
26 { tags => [ qw/@b/ ], outcome => 1 },
27 { tags => [ qw/@a @b/ ], outcome => !!0 },
28 ] },
29 { expr => '@a and @b',
30 tests => [ { tags => [ qw/@a/ ], outcome => !!0 },
31 { tags => [ qw/@b/ ], outcome => !!0 },
32 { tags => [ qw/@a @b/ ], outcome => 1 },
33 ] },
34 { expr => 'not (@a and @b)',
35 tests => [ { tags => [ qw/@a/ ], outcome => 1 },
36 { tags => [ qw/@b/ ], outcome => 1 },
37 { tags => [ qw/@a @b/ ], outcome => !!0 },
38 ] },
39 { expr => '@a or @b',
40 tests => [ { tags => [ qw/@a/ ], outcome => 1 },
41 { tags => [ qw/@b/ ], outcome => 1 },
42 { tags => [ qw/@a @b/ ], outcome => 1 },
43 ] },
44 { expr => 'not @a or @b',
45 tests => [ { tags => [ qw/@a/ ], outcome => !!0 },
46 { tags => [ qw/@b/ ], outcome => 1 },
47 { tags => [ qw/@a @b/ ], outcome => 1 },
48 { tags => [ qw/@q/ ], outcome => 1 },
49 ] },
50 { expr => '@a and not @b',
51 tests => [ { tags => [ qw/@a/ ], outcome => 1 },
52 { tags => [ qw/@b/ ], outcome => !!0 },
53 { tags => [ qw/@a @b/ ], outcome => !!0 },
54 { tags => [ qw/@q/ ], outcome => !!0 },
55 { tags => [ qw/@a @q/ ], outcome => 1 },
56 ] },
57 { expr => '@a or @b and @c',
58 tests => [ { tags => [ qw/@a/ ], outcome => 1 },
59 { tags => [ qw/@b/ ], outcome => !!0 },
60 { tags => [ qw/@a @b/ ], outcome => 1 },
61 { tags => [ qw/@a @c/ ], outcome => 1 },
62 { tags => [ qw/@b @c/ ], outcome => 1 },
63 ] },
64 { expr => '@a and @b or not @c',
65 tests => [ { tags => [ qw/@a/ ], outcome => 1 },
66 { tags => [ qw/@b/ ], outcome => 1 },
67 { tags => [ qw/@c/ ], outcome => !!0 },
68 { tags => [ qw/@q/ ], outcome => 1 },
69 { tags => [ qw/@a @b/ ], outcome => 1 },
70 { tags => [ qw/@a @c/ ], outcome => !!0 },
71 { tags => [ qw/@b @c/ ], outcome => !!0 },
72 ] },
73 { expr => '@a or ((@b or @c) and (@d or @e))',
74 tests => [ { tags => [ qw/@a/ ], outcome => 1 },
75 { tags => [ qw/@b/ ], outcome => !!0 },
76 { tags => [ qw/@d/ ], outcome => !!0 },
77 { tags => [ qw/@b @d/ ], outcome => 1 },
78 { tags => [ qw/@q/ ], outcome => !!0 },
79 ] },
80 { expr => "\@a\\\\b",
81 tests => [ { tags => [ "\@a\\b" ], outcome => 1 },
82 { tags => [ '@ab' ], outcome => !!0 },
83 { tags => [ qw/@a/ ], outcome => !!0 },
84 ] },
85 );
86
87 for my $ex (@good) {
88 my $e;
89 ok( lives {
90 $e = Cucumber::TagExpressions->parse($ex->{expr});
91 }, "Parsing $ex->{expr}")
92 or note($@);
93
94 for my $test ( @{ $ex->{tests} } ) {
95 my @tags = @{ $test->{tags} };
96 is( $e->evaluate(@tags), $test->{outcome},
97 "Expr $ex->{expr}; Tags: @tags; Outcome: $test->{outcome} " )
98 or diag( "Parsed expression: " . $e->stringify );
99 }
100 }
101
102
103 my %bad_syntax = (
104 '@a @b' => q{Expected operator.},
105 '@a not' => q{Expected operator.},
106 '@a or' => q{Expected operand.},
107 '@a not @b' => q{Expected operator.},
108 '@a or (' => q{Expected operand.},
109 '@a and @b)' => q{Unmatched ).},
110 "\@a\\" => q{Illegal escape before "<end-of-input>"},
111 );
112
113 for my $expr (keys %bad_syntax) {
114 like( dies { Cucumber::TagExpressions->parse($expr); },
115 qr/\Q$bad_syntax{$expr}\E/,
116 "Parsing bad expression '$expr'" );
117 }
118
119
120 done_testing;