diff --git a/ChangeLog.md b/ChangeLog.md index 7e701fd..6c8d939 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,15 @@ JSON for the XP Framework ChangeLog ## ?.?.? / ????-??-?? +## 4.1.0 / 2021-04-18 + +* Fixed up file-based input and output tests to clean up temporary files + they create as their fixture + (@thekid) +* Made all of `text.json.Format`, `text.json.Input` and `text.json.Output` + implement `lang.Value` and provide string representations + (@thekid) + ## 4.0.1 / 2020-12-27 * Fixed warnings from *iconv* library on Un\*x systems - @thekid diff --git a/src/main/php/text/json/FileInput.class.php b/src/main/php/text/json/FileInput.class.php index 4efcdd8..7023bc1 100755 --- a/src/main/php/text/json/FileInput.class.php +++ b/src/main/php/text/json/FileInput.class.php @@ -54,4 +54,9 @@ public function close() { /** @return io.File */ public function file() { return $this->file; } + + /** @return string */ + public function toString() { + return nameof($this).'(file= '.$this->file->toString().', encoding= '.$this->encoding.')'; + } } \ No newline at end of file diff --git a/src/main/php/text/json/FileOutput.class.php b/src/main/php/text/json/FileOutput.class.php index 29189a5..f3c73f7 100755 --- a/src/main/php/text/json/FileOutput.class.php +++ b/src/main/php/text/json/FileOutput.class.php @@ -52,4 +52,9 @@ public function close() { /** @return io.File */ public function file() { return $this->file; } + + /** @return string */ + public function toString() { + return nameof($this).'(file= '.$this->file->toString().', format= '.$this->format->toString().')'; + } } \ No newline at end of file diff --git a/src/main/php/text/json/Format.class.php b/src/main/php/text/json/Format.class.php index 7163391..46887b6 100755 --- a/src/main/php/text/json/Format.class.php +++ b/src/main/php/text/json/Format.class.php @@ -1,13 +1,13 @@ encoding.')'; } + + /** @return string */ + public function hashCode() { return spl_object_id($this); } + + /** + * Comparison + * + * @param var $value + * @return int + */ + public function compareTo($value) { + return $value === $this ? 0 : 1; + } + /** @return void */ public function __destruct() { $this->close(); diff --git a/src/main/php/text/json/Output.class.php b/src/main/php/text/json/Output.class.php index e60092f..a706376 100755 --- a/src/main/php/text/json/Output.class.php +++ b/src/main/php/text/json/Output.class.php @@ -1,8 +1,8 @@ format->toString().')'; } + + /** @return string */ + public function hashCode() { return spl_object_id($this); } + + /** + * Comparison + * + * @param var $value + * @return int + */ + public function compareTo($value) { + return $value === $this ? 0 : 1; + } + /** @return void */ public function __destruct() { $this->close(); diff --git a/src/main/php/text/json/StreamInput.class.php b/src/main/php/text/json/StreamInput.class.php index fe64ac6..25329f6 100755 --- a/src/main/php/text/json/StreamInput.class.php +++ b/src/main/php/text/json/StreamInput.class.php @@ -173,6 +173,11 @@ public function nextToken() { return null; } + /** @return string */ + public function toString() { + return nameof($this).'(stream= '.$this->in->toString().', encoding= '.$this->encoding.')'; + } + /** @return void */ public function close() { $this->in->close(); } } \ No newline at end of file diff --git a/src/main/php/text/json/StreamOutput.class.php b/src/main/php/text/json/StreamOutput.class.php index c85c2de..74b2993 100755 --- a/src/main/php/text/json/StreamOutput.class.php +++ b/src/main/php/text/json/StreamOutput.class.php @@ -36,6 +36,11 @@ public function appendToken($bytes) { $this->stream->write($bytes); } + /** @return string */ + public function toString() { + return nameof($this).'(stream= '.$this->stream->toString().', format= '.$this->format->toString().')'; + } + /** @return void */ public function close() { $this->stream->close(); } diff --git a/src/test/php/text/json/unittest/FileInputTest.class.php b/src/test/php/text/json/unittest/FileInputTest.class.php index 2781036..29b4702 100755 --- a/src/test/php/text/json/unittest/FileInputTest.class.php +++ b/src/test/php/text/json/unittest/FileInputTest.class.php @@ -9,10 +9,18 @@ * Tests the FileInput implementation */ class FileInputTest extends JsonInputTest { + private $created= []; /** @param io.Path */ private function tempName() { - return Path::compose([Environment::tempDir(), md5(uniqid()).'-xp.json']); + return $this->created[]= Path::compose([Environment::tempDir(), md5(uniqid()).'-xp.json']); + } + + /** @return void */ + public function tearDown() { + foreach ($this->created as $path) { + $path->exists() && $path->asFile()->unlink(); + } } /** @@ -97,4 +105,13 @@ public function is_reopened_when_reset() { $input->reset(); $this->assertTrue($file->isOpen()); } + + #[Test] + public function string_representation() { + $file= $this->fileWith('{}', File::REWRITE); + $this->assertEquals( + 'text.json.FileInput(file= '.$file->toString().', encoding= utf-8)', + (new FileInput($file))->toString() + ); + } } \ No newline at end of file diff --git a/src/test/php/text/json/unittest/FileOutputTest.class.php b/src/test/php/text/json/unittest/FileOutputTest.class.php index 5eb179b..64d1b00 100755 --- a/src/test/php/text/json/unittest/FileOutputTest.class.php +++ b/src/test/php/text/json/unittest/FileOutputTest.class.php @@ -3,13 +3,21 @@ use io\{File, Path}; use lang\Environment; use text\json\{FileOutput, Types}; -use unittest\Test; +use unittest\{Test, AfterClass}; class FileOutputTest extends JsonOutputTest { + private $created= []; /** @param io.Path */ private function tempName() { - return Path::compose([Environment::tempDir(), md5(uniqid()).'-xp.json']); + return $this->created[]= Path::compose([Environment::tempDir(), md5(uniqid()).'-xp.json']); + } + + /** @return void */ + public function tearDown() { + foreach ($this->created as $path) { + $path->exists() && $path->asFile()->unlink(); + } } /** @return text.json.Output */ @@ -72,4 +80,13 @@ public function open_files_are_not_closed() { (new FileOutput($file))->write('test'); $this->assertTrue($file->isOpen()); } + + #[Test] + public function string_representation() { + $output= $this->output(); + $this->assertEquals( + 'text.json.FileOutput(file= '.$output->file()->toString().', format= text.json.DenseFormat)', + $output->toString() + ); + } } \ No newline at end of file diff --git a/src/test/php/text/json/unittest/StreamInputTest.class.php b/src/test/php/text/json/unittest/StreamInputTest.class.php index 318dca5..cf1a4f5 100755 --- a/src/test/php/text/json/unittest/StreamInputTest.class.php +++ b/src/test/php/text/json/unittest/StreamInputTest.class.php @@ -86,4 +86,12 @@ public function cannot_reset_unseekable() { // OK } } + + #[Test] + public function string_representation() { + $this->assertEquals( + 'text.json.StreamInput(stream= io.streams.MemoryInputStream(@2 of 2 bytes), encoding= utf-8)', + $this->input('{}')->toString() + ); + } } \ No newline at end of file diff --git a/src/test/php/text/json/unittest/StreamOutputTest.class.php b/src/test/php/text/json/unittest/StreamOutputTest.class.php index 18945bd..bfda900 100755 --- a/src/test/php/text/json/unittest/StreamOutputTest.class.php +++ b/src/test/php/text/json/unittest/StreamOutputTest.class.php @@ -17,4 +17,12 @@ protected function output() { return new StreamOutput(new MemoryOutputStream()); protected function result($out) { return $out->stream()->getBytes(); } + + #[Test] + public function string_representation() { + $this->assertEquals( + 'text.json.StreamOutput(stream= io.streams.MemoryOutputStream(@0 of 0 bytes), format= text.json.DenseFormat)', + $this->output()->toString() + ); + } } \ No newline at end of file