Skip to content

Commit 005655e

Browse files
authored
Merge pull request #2 from preprocess/refactor-for-new-plugin
Refactor for new plugin
2 parents 3370f00 + 11e0ef5 commit 005655e

File tree

9 files changed

+140
-90
lines changed

9 files changed

+140
-90
lines changed

composer.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@
33
"name": "pre/deferred",
44
"license": "MIT",
55
"require": {
6-
"pre/plugin": "^0.7.3"
6+
"pre/plugin": "^0.10.0"
77
},
88
"autoload": {
99
"psr-4": {
10-
"Pre\\Deferred\\": "src"
10+
"Pre\\Deferred\\": "source"
1111
}
1212
},
1313
"require-dev": {
1414
"phpunit/phpunit": "^5.0|^6.0"
1515
},
1616
"autoload-dev": {
17-
"psr-4": {
18-
"Pre\\Deferred\\": "tests"
19-
}
17+
"files": ["tests/stubs.php"]
2018
},
2119
"extra": {
22-
"macros": [
23-
"src/macros.yay"
24-
]
20+
"macros": ["source/macros.yay"]
2521
}
2622
}

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit
33
backupGlobals="false"
44
backupStaticAttributes="false"
5-
bootstrap="tests/bootstrap.php"
5+
bootstrap="vendor/autoload.php"
66
colors="true"
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"
File renamed without changes.

source/macros.yay

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
macro ·recursion {
4+
·chain(
5+
defer,
6+
·between(
7+
·token("{"), ·layer()·body, ·token("}")
8+
),
9+
·optional(·token(";")),
10+
·_()·scope,
11+
·_()·simple
12+
)
13+
} >> function($ast) {
14+
$bound = false;
15+
$scope = new \Yay\Ast("·scope");
16+
17+
$pushed = [];
18+
19+
foreach ($ast->{"·body"} as $token) {
20+
$name = $token->value();
21+
22+
if (!$token->is(T_VARIABLE)) {
23+
continue;
24+
}
25+
26+
if (isset($pushed[$name])) {
27+
continue;
28+
}
29+
30+
if (substr($name, 1) === "this") {
31+
continue;
32+
}
33+
34+
$scope->push(new \Yay\Ast("·var", $token));
35+
$pushed[$name] = true;
36+
$bound = true;
37+
}
38+
39+
if ($bound) {
40+
$ast->append($scope);
41+
} else {
42+
$simple = new \Yay\Ast("·simple");
43+
$simple->push(new \Yay\Ast());
44+
45+
$ast->append($simple);
46+
}
47+
} >> {
48+
··collapse(··trim(
49+
·scope ?·{
50+
$deferred = new \Pre\Deferred\Deferred([·scope ···(, ) { ·var = ·var ?? null}, "fn" => function () use (··trim(·scope ···(, ) {&·var})) {
51+
··trim(·body)
52+
}]["fn"]);
53+
}
54+
55+
·simple ?·{
56+
$deferred = new \Pre\Deferred\Deferred(function () {
57+
··trim(·body)
58+
});
59+
}
60+
))
61+
}
62+
63+
macro ·recursion {
64+
·chain(
65+
defer,
66+
·ns()·function,
67+
·between(
68+
·token("("), ·layer()·parameters, ·token(")")
69+
),
70+
·token(";")
71+
)
72+
} >> {
73+
$deferred = ··unsafe(new \Pre\Deferred\Deferred((function (...$parameters) {
74+
return function () use ($parameters) {
75+
·function(...$parameters);
76+
};
77+
})(·parameters)));
78+
}

src/macros.yay

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/MacroTest.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
11
<?php
22

3-
namespace Pre\Deferred;
3+
use PHPUnit\Framework\TestCase;
44

5-
use Pre\Plugin\Testing\Runner;
6-
7-
class MacroTest extends Runner
5+
class MacroTest extends TestCase
86
{
9-
protected function path(): string
7+
/**
8+
* @test
9+
* @dataProvider specs
10+
*/
11+
public function can_transform_code($from, $expected)
12+
{
13+
\Pre\Plugin\addMacro(__DIR__ . "/../source/macros.yay");
14+
15+
$actual = \Pre\plugin\format(\Pre\Plugin\parse($this->format($from)));
16+
$this->assertEquals($this->format($expected), $actual);
17+
}
18+
19+
private function format($code)
1020
{
11-
return __DIR__ . "/specs";
21+
return "<?php\n\n" . trim($code) . "\n";
22+
}
23+
24+
public static function specs()
25+
{
26+
$specs = [];
27+
28+
$files = [
29+
__DIR__ . "/specs/deferred.spec",
30+
];
31+
32+
foreach ($files as $file) {
33+
$contents = file_get_contents($file);
34+
35+
foreach (explode("---", $contents) as $spec) {
36+
array_push($specs, explode("~~~", $spec));
37+
}
38+
}
39+
40+
return $specs;
1241
}
1342
}

tests/bootstrap.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/specs/deferred.spec

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
1-
--DESCRIPTION--
1+
defer unlink("path/to/file");
22

3-
Test deferred
3+
~~~
44

5-
--GIVEN--
5+
$deferred·0 = new \Pre\Deferred\Deferred((function (...$parameters) {
6+
return function () use ($parameters) {
7+
unlink(...$parameters);
8+
};
9+
})("path/to/file"));
610

7-
defer unlink("path/to/file");
11+
---
812

913
defer {
1014
fclose($handle);
1115
print "all done";
1216
}
1317

14-
--EXPECT--
18+
~~~
1519

16-
$deferred·0 = new \Pre\Deferred\Deferred(call_user_func(function () {
17-
$context·0 = func_get_args();
18-
19-
return function () use ($context·0) {
20-
call_user_func_array('unlink', $context·0);
21-
};
22-
}, "path/to/file"));
23-
24-
$deferred·1 = new \Pre\Deferred\Deferred(call_user_func(function ($context·1) {
25-
return function () use ($context·1) {
26-
extract($context·1);
27-
fclose($handle);
28-
print "all done";
29-
};
30-
}, get_defined_vars()));
20+
$deferred·0 = new \Pre\Deferred\Deferred([$handle = $handle ?? null, "fn" => function () use (&$handle) {
21+
fclose($handle);
22+
print "all done";
23+
}]["fn"]);

tests/stubs.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Yay
4+
{
5+
function md5($foo)
6+
{
7+
return $foo;
8+
}
9+
}

0 commit comments

Comments
 (0)