unit tests for Func and UserFunc

This commit is contained in:
Pekka Laiho 2020-12-17 21:32:37 +07:00
parent d1052b9b32
commit a10dff1307
2 changed files with 73 additions and 0 deletions

37
test/FuncTest.php Normal file
View 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
View 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);
}
}