From d1052b9b325227c8a7967752295063c4122a4d32 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Thu, 17 Dec 2020 09:45:37 +0700 Subject: [PATCH] more unit tests --- src/Env.php | 2 +- test/CollectionTest.php | 45 ++++++++++++++++++++++++++++ test/CoreFuncTest.php | 48 ++++++++++++++++++++++++++++++ test/EnvTest.php | 49 +++++++++++++++++++++++++++++++ test/HashTest.php | 36 +++++++++++++++++++++++ test/LispFactoryTest.php | 23 +++++++++++++++ test/MListTest.php | 29 ++++++++++++++++++ test/MadLispUserExceptionTest.php | 32 ++++++++++++++++++++ test/SymbolTest.php | 19 ++++++++++++ test/UtilTest.php | 39 ++++++++++++++++++++++++ test/VectorTest.php | 29 ++++++++++++++++++ 11 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 test/CollectionTest.php create mode 100644 test/CoreFuncTest.php create mode 100644 test/EnvTest.php create mode 100644 test/HashTest.php create mode 100644 test/LispFactoryTest.php create mode 100644 test/MListTest.php create mode 100644 test/MadLispUserExceptionTest.php create mode 100644 test/SymbolTest.php create mode 100644 test/UtilTest.php create mode 100644 test/VectorTest.php diff --git a/src/Env.php b/src/Env.php index 6aced54..6c4d9d4 100644 --- a/src/Env.php +++ b/src/Env.php @@ -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; } diff --git a/test/CollectionTest.php b/test/CollectionTest.php new file mode 100644 index 0000000..038ebe0 --- /dev/null +++ b/test/CollectionTest.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/test/CoreFuncTest.php b/test/CoreFuncTest.php new file mode 100644 index 0000000..3e6d12a --- /dev/null +++ b/test/CoreFuncTest.php @@ -0,0 +1,48 @@ +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); + } +} diff --git a/test/EnvTest.php b/test/EnvTest.php new file mode 100644 index 0000000..53e5849 --- /dev/null +++ b/test/EnvTest.php @@ -0,0 +1,49 @@ +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'); + } +} diff --git a/test/HashTest.php b/test/HashTest.php new file mode 100644 index 0000000..b73fb88 --- /dev/null +++ b/test/HashTest.php @@ -0,0 +1,36 @@ + 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'); + } +} diff --git a/test/LispFactoryTest.php b/test/LispFactoryTest.php new file mode 100644 index 0000000..4c9c932 --- /dev/null +++ b/test/LispFactoryTest.php @@ -0,0 +1,23 @@ +make(); + + $this->assertInstanceOf(Lisp::class, $lisp); + } +} diff --git a/test/MListTest.php b/test/MListTest.php new file mode 100644 index 0000000..d8694e3 --- /dev/null +++ b/test/MListTest.php @@ -0,0 +1,29 @@ +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); + } +} diff --git a/test/MadLispUserExceptionTest.php b/test/MadLispUserExceptionTest.php new file mode 100644 index 0000000..6c99439 --- /dev/null +++ b/test/MadLispUserExceptionTest.php @@ -0,0 +1,32 @@ +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()); + } + } +} diff --git a/test/SymbolTest.php b/test/SymbolTest.php new file mode 100644 index 0000000..cee6373 --- /dev/null +++ b/test/SymbolTest.php @@ -0,0 +1,19 @@ +assertSame('abc', $symbol->getName()); + } +} diff --git a/test/UtilTest.php b/test/UtilTest.php new file mode 100644 index 0000000..f87af57 --- /dev/null +++ b/test/UtilTest.php @@ -0,0 +1,39 @@ +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]); + } +} diff --git a/test/VectorTest.php b/test/VectorTest.php new file mode 100644 index 0000000..6e09da9 --- /dev/null +++ b/test/VectorTest.php @@ -0,0 +1,29 @@ +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); + } +}