From: widmogrod Date: Wed, 13 Dec 2017 00:14:58 +0000 (+0100) Subject: Update readme about new example X-Git-Tag: 3.4.0~1^2~1 X-Git-Url: http://cameron1729.xyz/?a=commitdiff_plain;h=cbfc128ff160b6e4df8e271f3f149d8989b75e60;p=php-functional.git Update readme about new example --- diff --git a/README.md b/README.md index 86c31dc..0682681 100644 --- a/README.md +++ b/README.md @@ -242,10 +242,41 @@ Free monad enables you to do exactly that, and more: - Write your own DLS (domain specific language) - Decouple implementation from interpretation. +#### Echo program Example Free Monad example of `echo program` can be found here: - [FreeMonadTest.php](/example/FreeMonadTest.php) - example based on first implementation of Free - [Free2MonadTest.php](/example/Free2MonadTest.php) - example based on second implementation of Free, based on [Haskell implementation](https://hackage.haskell.org/package/free-4.12.4/docs/Control-Monad-Free-Class.html) +#### DSL for `BDD` tests +Example that use `Free Monad` to creates simple DSL (Domain Specific Language) to define BDD type of framework: + +- [Free2BddStyleDSLTest.php](/example/Free2BddStyleDSLTest.php) - example based on second implementation of Free, based on [Haskell implementation](https://hackage.haskell.org/package/free-4.12.4/docs/Control-Monad-Free-Class.html) + +```php +$state = [ + 'productsCount' => 0, + 'products' => [], +]; + +$scenario = + Given('Product in cart', $state) + ->When("I add product 'coca-cola'") + ->When("I add product 'milk'") + ->Then("The number of products is '2'"); + +$result = $scenario->Run([ + "/^I add product '(.*)'/" => function ($state, $productName) { + $state['productsCount'] += 1; + $state['products'][] = $productName; + + return $state; + }, +], [ + "/^The number of products is '(\d+)'/" => function ($state, int $expected) { + return $state['productsCount'] === $expected; + }, +]); +``` ### Haskell Do Notation in PHP **NOTE:** This is experimental feature.