more unit tests

This commit is contained in:
Pekka Laiho 2020-12-17 09:45:37 +07:00
parent d363700a39
commit d1052b9b32
11 changed files with 350 additions and 1 deletions

View File

@ -47,7 +47,7 @@ class Env extends Hash
return $this->parent;
}
public function getRoot(): ?Env
public function getRoot(): Env
{
return $this->parent ? $this->parent->getRoot() : $this;
}

45
test/CollectionTest.php Normal file
View File

@ -0,0 +1,45 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\Hash;
use MadLisp\MList;
use MadLisp\Vector;
class CollectionTest extends TestCase
{
public function testNew()
{
// Test that Collection::new returns the correct type
$a = new Vector();
$vector = $a::new();
$a = new MList();
$list = $a::new();
$a = new Hash();
$hash = $a::new();
$this->assertInstanceOf(Vector::class, $vector);
$this->assertInstanceOf(MList::class, $list);
$this->assertInstanceOf(Hash::class, $hash);
}
public function testCollection()
{
$a = new Hash(['a' => 1, 'b' => 2, 'c' => 3]);
$data = $a->getData();
$this->assertCount(3, $data);
$this->assertSame(3, $a->count());
$this->assertTrue($a->has('a'));
$this->assertFalse($a->has('d'));
$this->assertSame(['a' => 1, 'b' => 2, 'c' => 3], $data);
}
}

48
test/CoreFuncTest.php Normal file
View File

@ -0,0 +1,48 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\CoreFunc;
use MadLisp\MadLispException;
class CoreFuncTest extends TestCase
{
public function invalidArgsProvider(): array
{
return [
[[1], 2, 2, "name requires exactly 2 arguments"],
[[1, 2, 3], 2, 2, "name requires exactly 2 arguments"],
[[1], 2, 3, "name requires at least 2 arguments"],
[[1, 2, 3], 1, 2, "name requires at most 2 arguments"],
];
}
/**
* @dataProvider invalidArgsProvider
*/
public function testInvalidArgs(array $args, int $min, int $max, string $message)
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage($message);
$closure = fn () => 1;
$fn = new CoreFunc("name", "doc", $min, $max, $closure);
$fn->call($args);
}
public function testCall()
{
$closure = fn ($a, $b) => $a + $b;
$fn = new CoreFunc("name", "doc", 2, 2, $closure);
$result = $fn->call([2, 3]);
$this->assertSame(5, $result);
}
}

49
test/EnvTest.php Normal file
View File

@ -0,0 +1,49 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\Env;
use MadLisp\MadLispException;
class EnvTest extends TestCase
{
public function testEnv()
{
$aa = new Env('aa');
$bb = new Env('bb', $aa);
$cc = new Env('cc', $bb);
$this->assertSame('aa', $aa->getFullName());
$this->assertSame('aa/bb', $bb->getFullName());
$this->assertSame('aa/bb/cc', $cc->getFullName());
$aa->set('dd', 12);
$bb->set('ee', 34);
$cc->set('ff', 56);
// Make sure get finds values from parent
$this->assertSame(12, $cc->get('dd'));
$this->assertSame(34, $cc->get('ee'));
$this->assertSame(56, $cc->get('ff'));
$this->assertNull($aa->getParent());
$this->assertSame($aa, $bb->getParent());
$this->assertSame($bb, $cc->getParent());
$this->assertSame($aa, $cc->getRoot());
}
public function testNotFound()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('symbol abc not defined in env');
$env = new Env('env');
$env->get('abc');
}
}

36
test/HashTest.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\Hash;
use MadLisp\MadLispException;
class HashTest extends TestCase
{
public function testHash()
{
$hash = new Hash(['a' => 1]);
$hash->set('b', 2);
$this->assertSame(1, $hash->get('a'));
$this->assertSame(2, $hash->get('b'));
$this->assertSame(2, $hash->unset('b'));
$this->assertSame(['a' => 1], $hash->getData());
}
public function testNotFound()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('hash does not contain key abc');
$hash = new Hash();
$hash->get('abc');
}
}

23
test/LispFactoryTest.php Normal file
View File

@ -0,0 +1,23 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\Lisp;
use MadLisp\LispFactory;
class LispFactoryTest extends TestCase
{
public function testMake()
{
$factory = new LispFactory();
$lisp = $factory->make();
$this->assertInstanceOf(Lisp::class, $lisp);
}
}

29
test/MListTest.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\MadLispException;
use MadLisp\MList;
class MListTest extends TestCase
{
public function testGet()
{
$list = new MList([1, 2, 3]);
$this->assertSame(2, $list->get(1));
}
public function testNotFound()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('list does not contain index 3');
$list = new MList([1, 2, 3]);
$list->get(3);
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\MadLispUserException;
use MadLisp\Vector;
class MadLispUserExceptionTest extends TestCase
{
public function testException()
{
try {
throw new MadLispUserException('message');
} catch (MadLispUserException $ex) {
$this->assertSame('message', $ex->getMessage());
$this->assertSame('message', $ex->getValue());
}
$value = new Vector([1, 2, 3]);
try {
throw new MadLispUserException($value);
} catch (MadLispUserException $ex) {
$this->assertSame($value, $ex->getValue());
}
}
}

19
test/SymbolTest.php Normal file
View File

@ -0,0 +1,19 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\Symbol;
class SymbolTest extends TestCase
{
public function testSymbol()
{
$symbol = new Symbol('abc');
$this->assertSame('abc', $symbol->getName());
}
}

39
test/UtilTest.php Normal file
View File

@ -0,0 +1,39 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\Hash;
use MadLisp\MadLispException;
use MadLisp\Util;
class UtilTest extends TestCase
{
public function testMakeHash()
{
$hash = Util::makeHash(['a', 1, 'b', 2]);
$this->assertInstanceOf(Hash::class, $hash);
$this->assertSame(['a' => 1, 'b' => 2], $hash->getData());
}
public function testMakeHashUnevenArgs()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('uneven number of arguments for hash');
Util::makeHash(['a', 1, 'b', 2, 'c']);
}
public function testMakeHashInvalidKey()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('invalid key for hash (not string)');
Util::makeHash([1, 2]);
}
}

29
test/VectorTest.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\MadLispException;
use MadLisp\Vector;
class VectorTest extends TestCase
{
public function testGet()
{
$list = new Vector([1, 2, 3]);
$this->assertSame(2, $list->get(1));
}
public function testNotFound()
{
$this->expectException(MadLispException::class);
$this->expectExceptionMessage('vector does not contain index 3');
$list = new Vector([1, 2, 3]);
$list->get(3);
}
}