unit tests for Printer class

This commit is contained in:
Pekka Laiho 2020-12-16 09:12:40 +07:00
parent 5ed0b0b531
commit b18fc9cde2
3 changed files with 90 additions and 1 deletions

View File

@ -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 {

89
test/PrinterTest.php Normal file
View File

@ -0,0 +1,89 @@
<?php
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
use PHPUnit\Framework\TestCase;
use MadLisp\Func;
use MadLisp\Hash;
use MadLisp\MList;
use MadLisp\Printer;
use MadLisp\Symbol;
use MadLisp\Vector;
class PrinterTest extends TestCase
{
public function notReadableProvider(): array
{
$mc = $this->createStub(Func::class);
$mc->method('isMacro')->willReturn(true);
$fn = $this->createStub(Func::class);
$fn->method('isMacro')->willReturn(false);
return [
[$mc, '<macro>'],
[$fn, '<function>'],
[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(), '<object<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('<resource>', $result);
fclose($file);
}
}

View File

@ -7,7 +7,6 @@
use PHPUnit\Framework\TestCase;
use MadLisp\MadLispException;
use MadLisp\Hash;
use MadLisp\MList;
use MadLisp\Reader;