/
ternary.t
140 lines (109 loc) · 3.63 KB
/
ternary.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use v6;
use Test;
#Ternary operator ?? !!
plan 28;
#L<S03/Changes to Perl 5 operators/"The ? : conditional operator becomes ?? !!">
my $str1 = "aaa";
my $str2 = "aaa";
my $str3 = "bbb";
{
my $foo = "";
$str1 eq $str2 ?? ($foo = 1) !! ($foo = 2);
is($foo, 1, "?? !!");
$str1 eq $str3 ?? ($foo = 3) !! ($foo = 4);
is($foo, 4, "?? !!");
}
is(($str2 eq $str1 ?? 8 * 8 !! 9 * 9), 64, "?? !! in parenthesis");
is(($str2 eq $str3 ?? 8 + 8 !! 9 div 9), 1, "?? !! in parenthesis");
is(1 ?? 2 ?? 3 !! 4 !! 5 ?? 6 !! 7, 3, "nested ?? !!");
is(1 ?? 0 ?? 3 !! 4 !! 5 ?? 6 !! 7, 4, "nested ?? !!");
is(0 ?? 2 ?? 3 !! 4 !! 5 ?? 6 !! 7, 6, "nested ?? !!");
is(0 ?? 2 ?? 3 !! 4 !! 0 ?? 6 !! 7, 7, "nested ?? !!");
{
my @a = (1 ?? 2 !! 3, 4 ?? 5 !! 6);
is(@a, [2, 5], "?? !! in array");
}
is((0 and 1 ?? 2 !! 3), 0, "operator priority");
is((4 or 5 ?? 6 !! 7), 4, "operator priority");
{
my $foo = 8;
$foo = 9 ?? 10 !! 11;
is($foo, 10, "operator priority");
$foo = 0 ?? 12 !! 13;
is($foo, 13, "operator priority");
}
{
my $foo = 1 ?? Bool::True !! Bool::False;
is($foo, Bool::True, "a statement with both ??!! and :: in it did compile") ;
}
{
# Defining an N! postfix (for factorial) causes a misparse on ternary op
proto postfix:<!>($n) {
return 1 if $n < 2;
return $n * ($n-1)!
}
my $foo = EVAL q[ 1 ?? 'yay' !! 'nay' ];
is($foo, "yay", "defining a postfix<!> doesn't screw up ternary op");
}
# RT #66840
{
throws-like { EVAL '1 ?? 2,3 !! 4,5' },
X::Syntax::ConditionalOperator::PrecedenceTooLoose,
'Ternary error (RT #66840)';
}
throws-like q[ 71704 !! 'bust' ], X::Syntax::Confused, 'Ternary error (RT #71704)';
throws-like { EVAL '1 ?? 3 :: 2' },
X::Syntax::ConditionalOperator::SecondPartInvalid,
second-part => "::",
'conditional operator written as ?? :: throws typed exception';
throws-like { EVAL '1 ?? 3 :foo :: 2' },
X::Syntax::ConditionalOperator::PrecedenceTooLoose,
operator => ":foo",
'adverbed literal in second part of ternary';
# RT #124323
{
my @x = ^10;
my @y = 2..3;
throws-like { EVAL 'my @z = @y ?? @x[@y] :v !! @x' },
X::Syntax::ConditionalOperator::PrecedenceTooLoose,
operator => ':v',
'precedence of adverb in second part of ternary is too loose';
is @y ?? (@x[@y] :v) !! @x, "2 3",
'adverb in second part of ternary used with parenthesis works';
}
throws-like { EVAL '1 ?? (3 :foo) !! 2' },
X::Syntax::Adverb,
'parenthesized adverbed literal in second part of ternary';
{
my $three:foo = 3;
my $thing:foo = 1 ?? $three:foo !! 2;
is $three:foo, 3, 'variable and adverb in second part of ternary';
}
throws-like { EVAL '1 ?? 3 : 2' },
X::Syntax::ConditionalOperator::SecondPartInvalid,
second-part => ":",
'conditional operator written as ?? : throws typed exception';
throws-like { EVAL '1 ?? b\n !! 2' },
X::Syntax::Confused,
'bogus code before !! of conditional operator is compile time error';
throws-like { EVAL '1 ?? 2' },
X::Syntax::Confused,
'missing !! of conditional operator is compile time error';
# RT #123115
{
sub rt123115 { 2 };
throws-like { EVAL '1 ?? rt123115 !! 3' },
X::Syntax::ConditionalOperator::SecondPartGobbled,
'typed exception when listop gobbles the !! of conditional operator';
}
# RT #129080
subtest 'fiddly meta error indicates what operator is used' => {
my @ops = <Z R X S>;
plan +@ops;
for @ops -> $op {
throws-like "1 $op?? 2 !! 3", X::Syntax::CannotMeta,
message => /['args of'|'with'] \s+ '??'/,
"$op operator";
}
}
# vim: ft=perl6