Refactor examples, to compare generate code much more nicer
authorwidmogrod <widmogrod@gmail.com>
Sat, 24 Feb 2018 18:14:24 +0000 (19:14 +0100)
committerwidmogrod <widmogrod@gmail.com>
Sat, 24 Feb 2018 18:14:24 +0000 (19:14 +0100)
example/ParserTest.php
example/_Parser_assets/A.txt [new file with mode: 0644]
example/_Parser_assets/Either.txt [new file with mode: 0644]
example/_Parser_assets/FreeT.txt [new file with mode: 0644]
example/_Parser_assets/Maybe.txt [new file with mode: 0644]

index ad262a5..0c4f7b0 100644 (file)
@@ -262,7 +262,6 @@ function maybeP(callable $matcher, Listt $a = null)
 }
 
 
-
 // lazyP :: ([a] -> Maybe b) -> [a] -> Maybe [b]
 function lazyP(callable $fn, Listt $a = null)
 {
@@ -630,7 +629,7 @@ class ParserTest extends \PHPUnit\Framework\TestCase
         );
     }
 
-    public function test_generate_data_types_as_free_string()
+    public function buildParserForDataTypes()
     {
         // lexeme :: ([a] -> Maybe (a, [a])) -> [a] -> Maybe (a, [a])
         $lexeme = function (callable $fn, Listt $a = null) {
@@ -753,8 +752,9 @@ class ParserTest extends \PHPUnit\Framework\TestCase
         ]), function (Listt $a) {
             list(, list($tname, $targ), , $rep) = $a->extract();
 
-            return declaree(data_($tname, $targ), fromIterable($rep)->map(function($t) {
+            return declaree(data_($tname, $targ), fromIterable($rep)->map(function ($t) {
                 list($tname, $targ) = $t;
+
                 return type($tname, $targ);
             }));
         });
@@ -765,12 +765,6 @@ class ParserTest extends \PHPUnit\Framework\TestCase
             return declaree($declaration, fromValue($derived));
         });
 
-        $tokens = tokens('
-        data A = B deriving (Show)
-        data Maybe a = Just a | Nothing
-        data Either a b = Left a | Right b
-        data Free f a = Pure a | Free f (Free f a)
-        ');
 
         $expression = manyP(fromIterable([
             $declarationDerived,
@@ -779,21 +773,46 @@ class ParserTest extends \PHPUnit\Framework\TestCase
             return $a->extract();
         });
 
+        return $expression;
+    }
+
+    /**
+     * @dataProvider provideGeneratedCode
+     */
+    public function test_generate_data_types_as_free_string(string $input, string $expectedFileContents)
+    {
+        $tokens = tokens($input);
+
+        $expression = $this->buildParserForDataTypes();
         $result = $expression($tokens);
         $ast = $result->extract()[0][0];
 
-        $expected = 'interface A extends Show {}
-class B implements A {
-    
-    public function patternMatched(callable $fn) {
-        return $fn();
-    }
-}
-';
+        $expected = file_get_contents(sprintf(__DIR__ . '/_Parser_assets/%s', $expectedFileContents));
 
         $result = foldFree(interpretTypesAndGenerate, $ast, Identity::of);
         $generated = $result->extract()->generate();
         $this->assertEquals($expected, $generated);
+    }
 
+    public function provideGeneratedCode()
+    {
+        return [
+            'data A = B deriving (Show)' => [
+                '$declaration' => 'data A = B deriving (Show)',
+                '$toImplementation' => 'A.txt',
+            ],
+            'data Maybe a = Just a | Nothing' => [
+                '$declaration' => 'data Maybe a = Just a | Nothing',
+                '$toImplementation' => 'Maybe.txt',
+            ],
+            'data Either a b = Left a | Right b' => [
+                '$declaration' => 'data Either a b = Left a | Right b',
+                '$toImplementation' => 'Either.txt',
+            ],
+            'data FreeT f a = Pure a | Free f (FreeT f a)' => [
+                '$declaration' => 'data FreeT f a = Pure a | Free f (FreeT f a)',
+                '$toImplementation' => 'FreeT.txt',
+            ],
+        ];
     }
 }
diff --git a/example/_Parser_assets/A.txt b/example/_Parser_assets/A.txt
new file mode 100644 (file)
index 0000000..86e873d
--- /dev/null
@@ -0,0 +1,7 @@
+interface A extends Show {}
+class B implements A {
+    
+    public function patternMatched(callable $fn) {
+        return $fn();
+    }
+}
diff --git a/example/_Parser_assets/Either.txt b/example/_Parser_assets/Either.txt
new file mode 100644 (file)
index 0000000..e5baa9a
--- /dev/null
@@ -0,0 +1,23 @@
+interface Either {}
+class Left implements Either {
+    private $a;
+
+    public function __construct($a) {
+        $this->a = $a;
+    }
+    public function patternMatched(callable $fn) {
+        return $fn($this->a);
+    }
+}
+class Right implements Either {
+    private $b;
+
+    public function __construct($b) {
+        $this->b = $b;
+    }
+    public function patternMatched(callable $fn) {
+        return $fn($this->b);
+    }
+}
diff --git a/example/_Parser_assets/FreeT.txt b/example/_Parser_assets/FreeT.txt
new file mode 100644 (file)
index 0000000..b89ed83
--- /dev/null
@@ -0,0 +1,25 @@
+interface FreeT {}
+class Pure implements FreeT {
+    private $a;
+
+    public function __construct($a) {
+        $this->a = $a;
+    }
+    public function patternMatched(callable $fn) {
+        return $fn($this->a);
+    }
+}
+class Free implements FreeT {
+    private $f;
+private $FreeT;
+
+    public function __construct($f,$FreeT) {
+        $this->f = $f;
+$this->FreeT = $FreeT;
+    }
+    public function patternMatched(callable $fn) {
+        return $fn($this->f, $this->FreeT);
+    }
+}
diff --git a/example/_Parser_assets/Maybe.txt b/example/_Parser_assets/Maybe.txt
new file mode 100644 (file)
index 0000000..cafa814
--- /dev/null
@@ -0,0 +1,18 @@
+interface Maybe {}
+class Just implements Maybe {
+    private $a;
+
+    public function __construct($a) {
+        $this->a = $a;
+    }
+    public function patternMatched(callable $fn) {
+        return $fn($this->a);
+    }
+}
+class Nothing implements Maybe {
+    
+    public function patternMatched(callable $fn) {
+        return $fn();
+    }
+}