From e2c8e9dafddd631fd6599dfe963f4f18462e48c2 Mon Sep 17 00:00:00 2001 From: Paul Crovella Date: Mon, 26 Mar 2018 21:35:41 -0700 Subject: [PATCH] Improve test coverage --- test/InputStream/StreamTest.php | 11 +++++++++ test/InputStream/StringInputTest.php | 6 +++++ test/JsonReaderTest.php | 15 +++++++++--- test/Parser/JsonParserTest.php | 13 ++++++++++ test/Parser/LexerTest.php | 36 ++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 3 deletions(-) diff --git a/test/InputStream/StreamTest.php b/test/InputStream/StreamTest.php index 64c8817..0c2bb1f 100644 --- a/test/InputStream/StreamTest.php +++ b/test/InputStream/StreamTest.php @@ -21,6 +21,17 @@ public function testStreamInput() fclose($handle); } + public function testEmptyStreamInput() + { + $handle = fopen('php://memory','r+'); + fwrite($handle, ""); + rewind($handle); + $stream = new Stream($handle); + + $this->assertNull($stream->read()); + fclose($handle); + } + public function testNotResourceFailure() { $this->expectException(InvalidArgumentException::class); diff --git a/test/InputStream/StringInputTest.php b/test/InputStream/StringInputTest.php index 0b1b5e4..d5658cd 100644 --- a/test/InputStream/StringInputTest.php +++ b/test/InputStream/StringInputTest.php @@ -12,4 +12,10 @@ public function testStringInput() $stringInput = new StringInput($string); $this->assertSame($string, $stringInput->read()); } + + public function testEmptyString() + { + $stringInput = new StringInput(""); + $this->assertNull($stringInput->read()); + } } diff --git a/test/JsonReaderTest.php b/test/JsonReaderTest.php index 8392297..d3f4071 100644 --- a/test/JsonReaderTest.php +++ b/test/JsonReaderTest.php @@ -52,28 +52,37 @@ public function read() }; } - /** @doesNotPerformAssertions */ public function testJson() { $reader = $this->reader; $reader->json(file_get_contents(__DIR__ . "/../composer.json")); + $this->assertSame(0, $reader->depth()); + $this->assertSame(JsonReader::NONE, $reader->type()); + $this->assertNull($reader->name()); + $this->assertNull($reader->value()); while ($reader->read()); } - /** @doesNotPerformAssertions */ public function testOpen() { $reader = $this->reader; $reader->open(__DIR__ . "/../composer.json"); + $this->assertSame(0, $reader->depth()); + $this->assertSame(JsonReader::NONE, $reader->type()); + $this->assertNull($reader->name()); + $this->assertNull($reader->value()); while ($reader->read()); } - /** @doesNotPerformAssertions */ public function testStream() { $reader = $this->reader; $handle = fopen((__DIR__ . "/../composer.json"), "rb"); $reader->stream($handle); + $this->assertSame(0, $reader->depth()); + $this->assertSame(JsonReader::NONE, $reader->type()); + $this->assertNull($reader->name()); + $this->assertNull($reader->value()); while ($reader->read()); fclose($handle); } diff --git a/test/Parser/JsonParserTest.php b/test/Parser/JsonParserTest.php index bb6dce0..76719ab 100644 --- a/test/Parser/JsonParserTest.php +++ b/test/Parser/JsonParserTest.php @@ -274,6 +274,19 @@ public function provideTestParserError() ], "Line 42: Unexpected token T_FALSE." ], + "object members with missing comma" => [ + [ + [Tokenizer::T_BEGIN_OBJECT, null, 42], + [Tokenizer::T_STRING, "name", 42], + [Tokenizer::T_COLON, null, 42], + [Tokenizer::T_STRING, "value", 42], + [Tokenizer::T_STRING, "name", 42], + [Tokenizer::T_COLON, null, 42], + [Tokenizer::T_STRING, "value", 42], + [Tokenizer::T_END_OBJECT, null, 42], + ], + "Line 42: Unexpected token T_STRING." + ], ]; } diff --git a/test/Parser/LexerTest.php b/test/Parser/LexerTest.php index 96deb00..dfe733f 100644 --- a/test/Parser/LexerTest.php +++ b/test/Parser/LexerTest.php @@ -138,6 +138,10 @@ public function provideTestTokenization() "\"\x7f\"", [Tokenizer::T_STRING, "\x7f", 1] ], + "string with escape sequence followed by text" => [ + '"\t foo"', + [Tokenizer::T_STRING, "\t foo", 1] + ], "simple number" => [ '42', [Tokenizer::T_NUMBER, "42", 1] @@ -224,6 +228,14 @@ public function provideTestLexerError() '-', "Line 1: Unexpected end of file." ], + "negative sign followed by non-digit" => [ + '-a', + "Line 1: Unexpected 'a'." + ], + "number truncated after decimal point" => [ + '0.', + "Line 1: Unexpected end of file." + ], "number with malformed fractional part" => [ '0.a', "Line 1: Unexpected 'a'." @@ -232,6 +244,14 @@ public function provideTestLexerError() '0.4eb', "Line 1: Unexpected 'b'." ], + "number with truncated exponent" => [ + '0.4e', + "Line 1: Unexpected end of file." + ], + "number with truncated signed exponent" => [ + '0.4e+', + "Line 1: Unexpected end of file." + ], "invalid escape sequence" => [ '"\h"', "Line 1: Unexpected 'h'." @@ -244,6 +264,10 @@ public function provideTestLexerError() '"\u454' . "\u{1F418}\"", "Line 1: Unexpected '\u{1F418}'." ], + "truncated unicode escape sequence" => [ + '"\u', + "Line 1: Unexpected end of file." + ], "string with record separator control character" => [ "\"\x1e\"", "Line 1: Unexpected control character \\u{1E}." @@ -292,6 +316,10 @@ public function provideTestLexerError() "\u{1F418}", "Line 1: Unexpected '\u{1F418}'." ], + "bare invalid UTF-8 byte, high out of range" => [ + "\xFF", + "Line 1: Ill-formed UTF-8 sequence 0xFF." + ], "bare invalid UTF-8 first byte, two byte sequence" => [ "\xC0\x80", "Line 1: Ill-formed UTF-8 sequence 0xC0 0x80." @@ -299,6 +327,14 @@ public function provideTestLexerError() "bare invalid UTF-8 two byte sequence, second byte not a continuation byte" => [ "\xE0\x00", "Line 1: Ill-formed UTF-8 sequence 0xE0." + ], + "bare invalid UTF-8 four byte sequence, second byte high out of range" => [ + "\xF4\x90\x80\x80", + "Line 1: Ill-formed UTF-8 sequence 0xF4 0x90 0x80 0x80." + ], + "string truncated at escape sequence" => [ + '"\\', + "Line 1: Unexpected end of file." ] ]; }