Skip to content

Commit 55fc0bd

Browse files
committed
Add more tests for operator precedence
1 parent c14709e commit 55fc0bd

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

src/test/php/lang/ast/unittest/emit/PipelinesTest.class.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function run($arg) {
155155
public function concat_precedence() {
156156
$r= $this->run('class %T {
157157
public function run() {
158-
return "te"."st" |> strtoupper(...);
158+
return "te" . "st" |> strtoupper(...);
159159
}
160160
}');
161161

@@ -187,8 +187,31 @@ public function run() {
187187
#[Test, Values([[0, 'even'], [1, 'odd'], [2, 'even']])]
188188
public function ternary_precedence($arg, $expected) {
189189
$r= $this->run('class %T {
190+
191+
private function odd($n) { return $n % 2; }
192+
193+
public function run($arg) {
194+
return $arg |> $this->odd(...) ? "odd" : "even";
195+
}
196+
}', $arg);
197+
198+
Assert::equals($expected, $r);
199+
}
200+
201+
#[Test, Values([[0, '(empty)'], [1, 'one element'], [2, '2 elements']])]
202+
public function short_ternary_precedence($arg, $expected) {
203+
$r= $this->run('class %T {
204+
205+
private function number($n) {
206+
return match ($n) {
207+
0 => null,
208+
1 => "one element",
209+
default => "{$n} elements"
210+
};
211+
}
212+
190213
public function run($arg) {
191-
return $arg |> fn($i) => $i % 2 ? "odd" : "even";
214+
return $arg |> $this->number(...) ?: "(empty)";
192215
}
193216
}', $arg);
194217

src/test/php/lang/ast/unittest/emit/TernaryTest.class.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,61 @@ public function run($value) {
5656
#[Test, Values(eval: '[["."], [new Path(".")]]')]
5757
public function with_instanceof($value) {
5858
Assert::equals(new Path('.'), $this->run(
59-
'class %T {
59+
'use io\\Path; class %T {
6060
public function run($value) {
61-
return $value instanceof \\io\\Path ? $value : new \\io\\Path($value);
61+
return $value instanceof Path ? $value : new Path($value);
6262
}
6363
}',
6464
$value
6565
));
6666
}
67+
68+
#[Test, Values(['"te" . "st"', '1 + 2', '1 || 0', '2 ?? 1', '1 | 2', '4 === strlen("Test")'])]
69+
public function precedence($lhs) {
70+
Assert::equals('OK', $this->run(
71+
'class %T {
72+
public function run() {
73+
return '.$lhs.'? "OK" : "Error";
74+
}
75+
}'
76+
));
77+
}
78+
79+
#[Test]
80+
public function assignment_precedence() {
81+
Assert::equals(['OK', 'OK'], $this->run(
82+
'class %T {
83+
public function run() {
84+
return [$a= 1 ? "OK" : "Error", $a];
85+
}
86+
}'
87+
));
88+
}
89+
90+
#[Test]
91+
public function yield_precedence() {
92+
Assert::equals(['OK', null], iterator_to_array($this->run(
93+
'class %T {
94+
public function run() {
95+
yield (yield 1 ? "OK" : "Error");
96+
}
97+
}'
98+
)));
99+
}
100+
101+
/** @see https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary */
102+
#[Test]
103+
public function chaining_short_ternaries() {
104+
Assert::equals([1, 2, 3], $this->run(
105+
'class %T {
106+
public function run() {
107+
return [
108+
0 ?: 1 ?: 2 ?: 3,
109+
0 ?: 0 ?: 2 ?: 3,
110+
0 ?: 0 ?: 0 ?: 3,
111+
];
112+
}
113+
}'
114+
));
115+
}
67116
}

0 commit comments

Comments
 (0)