-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryOperationTranspilerTest.php
More file actions
134 lines (122 loc) · 5.86 KB
/
BinaryOperationTranspilerTest.php
File metadata and controls
134 lines (122 loc) · 5.86 KB
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
<?php
/**
* PackageFactory.ComponentEngine - Universal View Components for PHP
* Copyright (C) 2022 Contributors of PackageFactory.ComponentEngine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace PackageFactory\ComponentEngine\Test\Unit\Target\Php\Transpiler\BinaryOperation;
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperationNode;
use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
use PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\Fixtures\DummyScope;
use PackageFactory\ComponentEngine\Target\Php\Transpiler\BinaryOperation\BinaryOperationTranspiler;
use PHPUnit\Framework\TestCase;
final class BinaryOperationTranspilerTest extends TestCase
{
/**
* @return array<string,mixed>
*/
public function binaryOperationExamples(): array
{
return [
// in php the or operator is not 100% equivalent to ecmascript
// `null || "fallback"` will be `true` in php but we want "fallback" like in js
'null || "fallback"' => '($temp_123 = null) ? $temp_123 : "fallback"',
'null && "fallback"' => '($temp_123 = null) ? "fallback" : $temp_123',
'true && false' => ['true && false', '(true && false)'],
'a && false' => ['a && false', '($this->a && false)'],
'true && b' => ['true && b', '(true && $this->b)'],
'a && b' => ['a && b', '($this->a && $this->b)'],
'true || false' => ['true || false', '(true || false)'],
'a || false' => ['a || false', '($this->a || false)'],
'true || b' => ['true || b', '(true || $this->b)'],
'a || b' => ['a || b', '($this->a || $this->b)'],
'true && "foo"' => ['true && "foo"', '(true && \'foo\')'],
'true || "foo"' => ['true || "foo"', '(true || \'foo\')'],
'true && 42' => ['true && 42', '(true && 42)'],
'true || 42' => ['true || 42', '(true || 42)'],
'1 + 2' => ['1 + 2', '(1 + 2)'],
'a + 2' => ['a + 2', '($this->a + 2)'],
'1 + b' => ['1 + b', '(1 + $this->b)'],
'a + b' => ['a + b', '($this->a + $this->b)'],
'2 - 1' => ['2 - 1', '(2 - 1)'],
'a - 1' => ['a - 1', '($this->a - 1)'],
'2 - b' => ['2 - b', '(2 - $this->b)'],
'a - b' => ['a - b', '($this->a - $this->b)'],
'2 * 4' => ['2 * 4', '(2 * 4)'],
'a * 4' => ['a * 4', '($this->a * 4)'],
'2 * b' => ['2 * b', '(2 * $this->b)'],
'a * b' => ['a * b', '($this->a * $this->b)'],
'2 / 4' => ['2 / 4', '(2 / 4)'],
'a / 4' => ['a / 4', '($this->a / 4)'],
'2 / b' => ['2 / b', '(2 / $this->b)'],
'a / b' => ['a / b', '($this->a / $this->b)'],
'2 % 4' => ['2 % 4', '(2 % 4)'],
'a % 4' => ['a % 4', '($this->a % 4)'],
'2 % b' => ['2 % b', '(2 % $this->b)'],
'a % b' => ['a % b', '($this->a % $this->b)'],
'42 * a / 23 + b - 17 * c' => [
'42 * a / 23 + b - 17 * c',
'((((42 * $this->a) / 23) + $this->b) - (17 * $this->c))'
],
'4 === 2' => ['4 === 2', '(4 === 2)'],
'a === 2' => ['a === 2', '($this->a === 2)'],
'4 === b' => ['4 === b', '(4 === $this->b)'],
'a === b' => ['a === b', '($this->a === $this->b)'],
'4 !== 2' => ['4 !== 2', '(4 !== 2)'],
'a !== 2' => ['a !== 2', '($this->a !== 2)'],
'4 !== b' => ['4 !== b', '(4 !== $this->b)'],
'a !== b' => ['a !== b', '($this->a !== $this->b)'],
'4 > 2' => ['4 > 2', '(4 > 2)'],
'a > 2' => ['a > 2', '($this->a > 2)'],
'4 > b' => ['4 > b', '(4 > $this->b)'],
'a > b' => ['a > b', '($this->a > $this->b)'],
'4 >= 2' => ['4 >= 2', '(4 >= 2)'],
'a >= 2' => ['a >= 2', '($this->a >= 2)'],
'4 >= b' => ['4 >= b', '(4 >= $this->b)'],
'a >= b' => ['a >= b', '($this->a >= $this->b)'],
'4 < 2' => ['4 < 2', '(4 < 2)'],
'a < 2' => ['a < 2', '($this->a < 2)'],
'4 < b' => ['4 < b', '(4 < $this->b)'],
'a < b' => ['a < b', '($this->a < $this->b)'],
'4 <= 2' => ['4 <= 2', '(4 <= 2)'],
'a <= 2' => ['a <= 2', '($this->a <= 2)'],
'4 <= b' => ['4 <= b', '(4 <= $this->b)'],
'a <= b' => ['a <= b', '($this->a <= $this->b)'],
];
}
/**
* @dataProvider binaryOperationExamples
* @test
* @param string $binaryOperationAsString
* @param string $expectedTranspilationResult
* @return void
*/
public function transpilesBinaryOperationNodes(string $binaryOperationAsString, string $expectedTranspilationResult): void
{
$binaryOperationTranspiler = new BinaryOperationTranspiler(
scope: new DummyScope()
);
$binaryOperationNode = ExpressionNode::fromString($binaryOperationAsString)->root;
assert($binaryOperationNode instanceof BinaryOperationNode);
$actualTranspilationResult = $binaryOperationTranspiler->transpile(
$binaryOperationNode
);
$this->assertEquals(
$expectedTranspilationResult,
$actualTranspilationResult
);
}
}