From 5ed0b0b531352c99e8098ee87aae5630b160a3fe Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Wed, 16 Dec 2020 08:20:35 +0700 Subject: [PATCH] add unit tests for Reader class --- test/ReaderTest.php | 127 +++++++++++++++++++++++++++++++++++++++++ test/TokenizerTest.php | 5 +- 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 test/ReaderTest.php diff --git a/test/ReaderTest.php b/test/ReaderTest.php new file mode 100644 index 0000000..87ea337 --- /dev/null +++ b/test/ReaderTest.php @@ -0,0 +1,127 @@ +read([]); + $this->assertNull($result); + } + + public function specialFormProvider(): array + { + return [ + ["'", 'quote'], + ['`', 'quasiquote'], + ['~', 'unquote'], + ]; + } + + /** + * Test readSpecialForm. + * @dataProvider specialFormProvider + */ + public function testReadSpecialForm(string $specialChar, string $symbolName) + { + $reader = new Reader(); + + $result = $reader->read([$specialChar, 'abc']); + + $this->assertInstanceOf(MList::class, $result); + $data = $result->getData(); + $this->assertCount(2, $data); + $this->assertInstanceOf(Symbol::class, $data[0]); + $this->assertInstanceOf(Symbol::class, $data[1]); + $this->assertSame($symbolName, $data[0]->getName()); + $this->assertSame('abc', $data[1]->getName()); + } + + public function testReadList() + { + $reader = new Reader(); + $input = ['(', 'aa', 'bb', '(', 'cc', ')', ')']; + $result = $reader->read($input); + + $this->assertInstanceOf(MList::class, $result); + $data = $result->getData(); + $this->assertCount(3, $data); + + $this->assertInstanceOf(Symbol::class, $data[0]); + $this->assertInstanceOf(Symbol::class, $data[1]); + $this->assertSame('aa', $data[0]->getName()); + $this->assertSame('bb', $data[1]->getName()); + + $this->assertInstanceOf(MList::class, $data[2]); + $data2 = $data[2]->getData(); + $this->assertCount(1, $data2); + $this->assertInstanceOf(Symbol::class, $data2[0]); + $this->assertSame('cc', $data2[0]->getName()); + } + + public function testReadVector() + { + $reader = new Reader(); + $input = ['[', 1, 2, '[', 3, ']', ']']; + $result = $reader->read($input); + + $this->assertInstanceOf(Vector::class, $result); + $data = $result->getData(); + $this->assertCount(3, $data); + + $this->assertSame(1, $data[0]); + $this->assertSame(2, $data[1]); + + $this->assertInstanceOf(Vector::class, $data[2]); + $data2 = $data[2]->getData(); + $this->assertCount(1, $data2); + $this->assertSame(3, $data2[0]); + } + + public function testReadHash() + { + $reader = new Reader(); + $input = ['{', '"aa"', '"bb"', '"cc"', '{', '"dd"', 123, '}', '}']; + $result = $reader->read($input); + + $this->assertInstanceOf(Hash::class, $result); + $data = $result->getData(); + $this->assertCount(2, $data); + $this->assertSame('bb', $data['aa']); + + $this->assertInstanceOf(Hash::class, $data['cc']); + $data2 = $data['cc']->getData(); + $this->assertCount(1, $data2); + $this->assertSame(123, $data2['dd']); + } + + public function testReadAtom() + { + $reader = new Reader(); + + $this->assertTrue($reader->read(['true'])); + $this->assertFalse($reader->read(['false'])); + $this->assertNull($reader->read(['null'])); + $this->assertSame('abc', $reader->read(['"abc"'])); + $this->assertSame(123, $reader->read(['123'])); + $this->assertSame(34.56, $reader->read(['34.56'])); + + $symbol = $reader->read(['abc']); + $this->assertInstanceOf(Symbol::class, $symbol); + $this->assertSame('abc', $symbol->getName()); + } +} diff --git a/test/TokenizerTest.php b/test/TokenizerTest.php index f8e9bbc..583a675 100644 --- a/test/TokenizerTest.php +++ b/test/TokenizerTest.php @@ -89,7 +89,10 @@ class TokenizerTest extends TestCase ["aa\"bb\\\\\\\"cc\"dd", ['aa', "\"bb\\\"cc\"", 'dd']], // Test everything together - ["(abc<+=-_!?>\"str\n\\r;\\\"\";com\"ment\r{\"a\":\"b\"})", ['(', 'abc<+=-_!?>', "\"str\n\r;\"\"", '{', '"a"', '"b"', '}', ')']], + [ + "(abc<+=-_!?>\"str\n\\r;\\\"\";com\"ment\r{\"a\":\"b\"})", + ['(', 'abc<+=-_!?>', "\"str\n\r;\"\"", '{', '"a"', '"b"', '}', ')'] + ], ]; }