-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.php
53 lines (38 loc) · 1.24 KB
/
calculator.php
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
<?php
require 'vendor/autoload.php';
$twig = new Twig\Environment(
new Twig\Loader\ArrayLoader(),
['autoescape' => false]
);
// Your code goes here
$test = static function (string $expression, string $expected) use ($twig): bool {
$twig->getLoader()->setTemplate($expression, "{{ $expression }}");
$actual = $twig->render($expression, [
'a' => 1000,
'b' => 337,
'c' => null,
]);
if ($actual !== $expected) {
error_log("Result for expression '$expression' is '$actual', expected '$expected'");
return false;
}
return true;
};
$test('a', '1000');
$test('+a', '1000');
$test('a + b', '1337');
$test('c', '');
$test('"#{c}"', '');
$test('+c', '');
$test('+c is null', '1');
$test('a + c', '');
$test('a + (b - b)', '1000');
$test('a / 2 + b', '837');
$test('a ? b : c', '337');
$test('a + (c ?? b)', '1337');
$test('a + {"v": c}["v"]', '');
// Stretch goal: remove return statement and ensure that the tests below pass as well
return;
$test('a - c', '');
$test('a * c', '');
$test('a / c', '');