From 22f4e0bb61453685a05186b4e0d80b8fb4ea5368 Mon Sep 17 00:00:00 2001 From: Christopher Pitt Date: Sat, 28 Oct 2017 22:57:37 +0300 Subject: [PATCH 1/2] Refactor for new plugin --- composer.json | 22 +++++----- phpunit.xml | 2 +- {src => source}/Deferred.php | 0 source/macros.yay | 78 ++++++++++++++++++++++++++++++++++++ src/macros.yay | 37 ----------------- tests/MacroTest.php | 41 ++++++++++++++++--- tests/bootstrap.php | 18 --------- tests/specs/deferred.spec | 33 ++++++--------- tests/stubs.php | 9 +++++ 9 files changed, 150 insertions(+), 90 deletions(-) rename {src => source}/Deferred.php (100%) create mode 100644 source/macros.yay delete mode 100644 src/macros.yay delete mode 100644 tests/bootstrap.php create mode 100644 tests/stubs.php diff --git a/composer.json b/composer.json index 6d6cec5..ed579ed 100644 --- a/composer.json +++ b/composer.json @@ -2,25 +2,29 @@ "type": "pre-macro", "name": "pre/deferred", "license": "MIT", + "repositories": [ + { + "type": "path", + "url": "../pre-plugin" + } + ], "require": { - "pre/plugin": "^0.7.3" + "pre/plugin": "*" }, "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"] + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/phpunit.xml b/phpunit.xml index 829414b..5543800 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,7 +2,7 @@ > 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))); +} diff --git a/src/macros.yay b/src/macros.yay deleted file mode 100644 index dd4e234..0000000 --- a/src/macros.yay +++ /dev/null @@ -1,37 +0,0 @@ -> { - $deferred = new \Pre\Deferred\Deferred(call_user_func(function($context) { - return function() use ($context) { - extract($context); - ·body - }; - }, get_defined_vars())); -} - -macro ·recursion { - ·chain( - defer, - ·ns()·function, - ·between( - ·token("("), ·layer()·parameters, ·token(")") - ), - ·token(";") - ) -} >> { - $deferred = new \Pre\Deferred\Deferred(call_user_func(function() { - $context = func_get_args(); - - return function() use ($context) { - call_user_func_array(··stringify(·function), $context); - }; - }, ·parameters)); -} diff --git a/tests/MacroTest.php b/tests/MacroTest.php index ee7d41e..430dd3e 100644 --- a/tests/MacroTest.php +++ b/tests/MacroTest.php @@ -1,13 +1,44 @@ format($from))); + $this->assertEquals($this->format($expected), $actual); + } + + private function format($code) + { + return " function () use (&$handle) { + fclose($handle); + print "all done"; +}]["fn"]); diff --git a/tests/stubs.php b/tests/stubs.php new file mode 100644 index 0000000..d714f68 --- /dev/null +++ b/tests/stubs.php @@ -0,0 +1,9 @@ + Date: Sun, 29 Oct 2017 00:07:10 +0300 Subject: [PATCH 2/2] Use tagged plugin --- composer.json | 12 ++---------- tests/MacroTest.php | 2 -- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index ed579ed..ada73ac 100644 --- a/composer.json +++ b/composer.json @@ -2,14 +2,8 @@ "type": "pre-macro", "name": "pre/deferred", "license": "MIT", - "repositories": [ - { - "type": "path", - "url": "../pre-plugin" - } - ], "require": { - "pre/plugin": "*" + "pre/plugin": "^0.10.0" }, "autoload": { "psr-4": { @@ -24,7 +18,5 @@ }, "extra": { "macros": ["source/macros.yay"] - }, - "minimum-stability": "dev", - "prefer-stable": true + } } diff --git a/tests/MacroTest.php b/tests/MacroTest.php index 430dd3e..7e12dbd 100644 --- a/tests/MacroTest.php +++ b/tests/MacroTest.php @@ -2,8 +2,6 @@ use PHPUnit\Framework\TestCase; -putenv("PRE_BASE_DIR=" . realpath(__DIR__ . "/../")); - class MacroTest extends TestCase { /**