mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 13:24:46 +00:00
unit tests for Func and UserFunc
This commit is contained in:
parent
d1052b9b32
commit
a10dff1307
37
test/FuncTest.php
Normal file
37
test/FuncTest.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* MadLisp language
|
||||||
|
* @link http://madlisp.com/
|
||||||
|
* @copyright Copyright (c) 2020 Pekka Laiho
|
||||||
|
*/
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
use MadLisp\Env;
|
||||||
|
use MadLisp\MList;
|
||||||
|
use MadLisp\UserFunc;
|
||||||
|
|
||||||
|
class FuncTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testFunc()
|
||||||
|
{
|
||||||
|
$closure = fn ($a, $b) => $a + $b;
|
||||||
|
$ast = new MList();
|
||||||
|
$env = new Env("env");
|
||||||
|
$bindings = new MList();
|
||||||
|
|
||||||
|
$fn = new UserFunc($closure, $ast, $env, $bindings, false);
|
||||||
|
|
||||||
|
$this->assertSame($closure, $fn->getClosure());
|
||||||
|
|
||||||
|
// test docstrings
|
||||||
|
$this->assertNull($fn->getDoc());
|
||||||
|
$fn->setDoc('docstring');
|
||||||
|
$this->assertSame('docstring', $fn->getDoc());
|
||||||
|
|
||||||
|
// test isMacro
|
||||||
|
$this->assertFalse($fn->isMacro());
|
||||||
|
$fn = new UserFunc($closure, $ast, $env, $bindings, true);
|
||||||
|
$this->assertTrue($fn->isMacro());
|
||||||
|
}
|
||||||
|
}
|
36
test/UserFuncTest.php
Normal file
36
test/UserFuncTest.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* MadLisp language
|
||||||
|
* @link http://madlisp.com/
|
||||||
|
* @copyright Copyright (c) 2020 Pekka Laiho
|
||||||
|
*/
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
use MadLisp\Env;
|
||||||
|
use MadLisp\MList;
|
||||||
|
use MadLisp\Symbol;
|
||||||
|
use MadLisp\UserFunc;
|
||||||
|
|
||||||
|
class UserFuncTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testUserFunc()
|
||||||
|
{
|
||||||
|
$closure = fn ($a, $b) => $a + $b;
|
||||||
|
$ast = new MList();
|
||||||
|
$env = new Env("env");
|
||||||
|
$bindings = new MList([new Symbol('a'), new Symbol('b')]);
|
||||||
|
|
||||||
|
$fn = new UserFunc($closure, $ast, $env, $bindings, false);
|
||||||
|
|
||||||
|
$this->assertSame($ast, $fn->getAst());
|
||||||
|
$this->assertSame($bindings, $fn->getBindings());
|
||||||
|
|
||||||
|
$newEnv = $fn->getEnv([1, 2]);
|
||||||
|
$this->assertInstanceOf(Env::class, $newEnv);
|
||||||
|
$this->assertSame(['a' => 1, 'b' => 2], $newEnv->getData());
|
||||||
|
|
||||||
|
$result = $fn->call([1, 2]);
|
||||||
|
$this->assertSame(3, $result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user