diff --git a/src/Printer.php b/src/Printer.php index 5a1491c..e7a07eb 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -52,6 +52,7 @@ class Printer $a = str_replace("\\", "\\\\", $a); $a = str_replace("\n", "\\n", $a); $a = str_replace("\r", "\\r", $a); + $a = str_replace("\t", "\\t", $a); $a = str_replace("\"", "\\\"", $a); return '"' . $a . '"'; } else { diff --git a/test/PrinterTest.php b/test/PrinterTest.php new file mode 100644 index 0000000..2fcd06e --- /dev/null +++ b/test/PrinterTest.php @@ -0,0 +1,89 @@ +createStub(Func::class); + $mc->method('isMacro')->willReturn(true); + + $fn = $this->createStub(Func::class); + $fn->method('isMacro')->willReturn(false); + + return [ + [$mc, ''], + [$fn, ''], + [new MList([new Symbol('aa'), new Symbol('bb'), new MList([new Symbol('cc')])]), '(aa bb (cc))'], + [new Vector([12, 34, new Vector([56])]), '[12 34 [56]]'], + [new Hash(['aa' => 'bb', 'cc' => new Hash(['dd' => 'ee'])]), '{aa:bb cc:{dd:ee}}'], + [new Symbol('abc'), 'abc'], + [new \stdClass(), '>'], + [true, 'true'], + [false, 'false'], + [null, 'null'], + [123, '123'], + [34.56, '34.56'], + + // Test strings + ['abc', 'abc'], + ["a\\b\nc\rd\te\"f", "a\\b\nc\rd\te\"f"], + ]; + } + + /** + * @dataProvider notReadableProvider + */ + public function testPrintNotReadable($input, string $expected) + { + $printer = new Printer(); + $result = $printer->pstr($input, false); + $this->assertSame($expected, $result); + } + + public function readableProvider(): array + { + return [ + [new Hash(['aa' => 'bb', 'cc' => new Hash(['dd' => 'ee'])]), '{"aa":"bb" "cc":{"dd":"ee"}}'], + [new Symbol('abc'), 'abc'], // symbol is not quoted + + // Test strings + ['abc', '"abc"'], + ["a\\b\nc\rd\te\"f", "\"a\\\\b\\nc\\rd\\te\\\"f\""], + ]; + } + + /** + * @dataProvider readableProvider + */ + public function testPrintReadable($input, string $expected) + { + $printer = new Printer(); + $result = $printer->pstr($input, true); + $this->assertSame($expected, $result); + } + + public function testPrintResource() + { + $file = tmpfile(); + + $printer = new Printer(); + $result = $printer->pstr($file, false); + $this->assertSame('', $result); + + fclose($file); + } +} diff --git a/test/ReaderTest.php b/test/ReaderTest.php index 87ea337..c62ddfc 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\TestCase; -use MadLisp\MadLispException; use MadLisp\Hash; use MadLisp\MList; use MadLisp\Reader;