Skip to content

Commit

Permalink
Merge pull request #2 from preprocess/refactor-for-new-plugin
Browse files Browse the repository at this point in the history
Refactor for new plugin
  • Loading branch information
assertchris authored Oct 28, 2017
2 parents 3370f00 + 11e0ef5 commit 005655e
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 90 deletions.
12 changes: 4 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
"name": "pre/deferred",
"license": "MIT",
"require": {
"pre/plugin": "^0.7.3"
"pre/plugin": "^0.10.0"
},
"autoload": {
"psr-4": {
"Pre\\Deferred\\": "src"
"Pre\\Deferred\\": "source"
}
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0"
},
"autoload-dev": {
"psr-4": {
"Pre\\Deferred\\": "tests"
}
"files": ["tests/stubs.php"]
},
"extra": {
"macros": [
"src/macros.yay"
]
"macros": ["source/macros.yay"]
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
File renamed without changes.
78 changes: 78 additions & 0 deletions source/macros.yay
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

macro ·recursion {
·chain(
defer,
·between(
·token("{"), ·layer()·body, ·token("}")
),
·optional(·token(";")),
·_()·scope,
·_()·simple
)
} >> function($ast) {
$bound = false;
$scope = new \Yay\Ast("·scope");

$pushed = [];

foreach ($ast->{"·body"} as $token) {
$name = $token->value();

if (!$token->is(T_VARIABLE)) {
continue;
}

if (isset($pushed[$name])) {
continue;
}

if (substr($name, 1) === "this") {
continue;
}

$scope->push(new \Yay\Ast("·var", $token));
$pushed[$name] = true;
$bound = true;
}

if ($bound) {
$ast->append($scope);
} else {
$simple = new \Yay\Ast("·simple");
$simple->push(new \Yay\Ast());

$ast->append($simple);
}
} >> {
··collapse(··trim(
·scope ?·{
$deferred = new \Pre\Deferred\Deferred([·scope ···(, ) { ·var = ·var ?? null}, "fn" => function () use (··trim(·scope ···(, ) {&·var})) {
··trim(·body)
}]["fn"]);
}

·simple ?·{
$deferred = new \Pre\Deferred\Deferred(function () {
··trim(·body)
});
}
))
}

macro ·recursion {
·chain(
defer,
·ns()·function,
·between(
·token("("), ·layer()·parameters, ·token(")")
),
·token(";")
)
} >> {
$deferred = ··unsafe(new \Pre\Deferred\Deferred((function (...$parameters) {
return function () use ($parameters) {
·function(...$parameters);
};
})(·parameters)));
}
37 changes: 0 additions & 37 deletions src/macros.yay

This file was deleted.

41 changes: 35 additions & 6 deletions tests/MacroTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
<?php

namespace Pre\Deferred;
use PHPUnit\Framework\TestCase;

use Pre\Plugin\Testing\Runner;

class MacroTest extends Runner
class MacroTest extends TestCase
{
protected function path(): string
/**
* @test
* @dataProvider specs
*/
public function can_transform_code($from, $expected)
{
\Pre\Plugin\addMacro(__DIR__ . "/../source/macros.yay");

$actual = \Pre\plugin\format(\Pre\Plugin\parse($this->format($from)));
$this->assertEquals($this->format($expected), $actual);
}

private function format($code)
{
return __DIR__ . "/specs";
return "<?php\n\n" . trim($code) . "\n";
}

public static function specs()
{
$specs = [];

$files = [
__DIR__ . "/specs/deferred.spec",
];

foreach ($files as $file) {
$contents = file_get_contents($file);

foreach (explode("---", $contents) as $spec) {
array_push($specs, explode("~~~", $spec));
}
}

return $specs;
}
}
18 changes: 0 additions & 18 deletions tests/bootstrap.php

This file was deleted.

33 changes: 13 additions & 20 deletions tests/specs/deferred.spec
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
--DESCRIPTION--
defer unlink("path/to/file");

Test deferred
~~~

--GIVEN--
$deferred·0 = new \Pre\Deferred\Deferred((function (...$parameters) {
return function () use ($parameters) {
unlink(...$parameters);
};
})("path/to/file"));

defer unlink("path/to/file");
---

defer {
fclose($handle);
print "all done";
}

--EXPECT--
~~~

$deferred·0 = new \Pre\Deferred\Deferred(call_user_func(function () {
$context·0 = func_get_args();

return function () use ($context·0) {
call_user_func_array('unlink', $context·0);
};
}, "path/to/file"));

$deferred·1 = new \Pre\Deferred\Deferred(call_user_func(function ($context·1) {
return function () use ($context·1) {
extract($context·1);
fclose($handle);
print "all done";
};
}, get_defined_vars()));
$deferred·0 = new \Pre\Deferred\Deferred([$handle = $handle ?? null, "fn" => function () use (&$handle) {
fclose($handle);
print "all done";
}]["fn"]);
9 changes: 9 additions & 0 deletions tests/stubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Yay
{
function md5($foo)
{
return $foo;
}
}

0 comments on commit 005655e

Please sign in to comment.