Skip to content

Commit

Permalink
Provide string representations of text.json.Format, `text.json.Inpu…
Browse files Browse the repository at this point in the history
…t` and `text.json.Output`
  • Loading branch information
thekid committed Apr 18, 2021
1 parent 1ce1a12 commit f7b3538
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 9 deletions.
9 changes: 9 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/main/php/text/json/FileInput.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')';
}
}
5 changes: 5 additions & 0 deletions src/main/php/text/json/FileOutput.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().')';
}
}
20 changes: 18 additions & 2 deletions src/main/php/text/json/Format.class.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php namespace text\json;

use lang\IllegalArgumentException;
use lang\{IllegalArgumentException, Value};

/**
* JSON format
*
* @test xp://text.json.unittest.FormatFactoryTest
*/
abstract class Format {
abstract class Format implements Value {
const ESCAPE_SLASHES = -65; // ~JSON_UNESCAPED_SLASHES
const ESCAPE_UNICODE = -257; // ~JSON_UNESCAPED_UNICODE
const ESCAPE_ENTITIES = 11; // JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT
Expand Down Expand Up @@ -130,4 +130,20 @@ public function representationOf($value) {
throw new IllegalArgumentException('Cannot represent instances of '.typeof($value));
}
}

/** @return string */
public function toString() { return nameof($this); }

/** @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;
}
}
20 changes: 18 additions & 2 deletions src/main/php/text/json/Input.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace text\json;

use lang\FormatException;
use lang\{FormatException, Value};
use util\Objects;

/**
Expand All @@ -9,7 +9,7 @@
* @see xp://text.json.JsonString
* @see xp://text.json.JsonStream
*/
abstract class Input {
abstract class Input implements Value {
protected $bytes;
protected $len;
protected $pos;
Expand Down Expand Up @@ -284,6 +284,22 @@ public function close() {
// Does nothing
}

/** @return string */
public function toString() { return nameof($this).'(encoding= '.$this->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();
Expand Down
20 changes: 18 additions & 2 deletions src/main/php/text/json/Output.class.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php namespace text\json;

use lang\IllegalArgumentException;
use lang\{IllegalArgumentException, Value};

abstract class Output {
abstract class Output implements Value {
public $format;

/**
Expand Down Expand Up @@ -79,6 +79,22 @@ public function close() {
// Does nothing
}

/** @return string */
public function toString() { return nameof($this).'(format= '.$this->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();
Expand Down
5 changes: 5 additions & 0 deletions src/main/php/text/json/StreamInput.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
}
5 changes: 5 additions & 0 deletions src/main/php/text/json/StreamOutput.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

Expand Down
19 changes: 18 additions & 1 deletion src/test/php/text/json/unittest/FileInputTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -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()
);
}
}
21 changes: 19 additions & 2 deletions src/test/php/text/json/unittest/FileOutputTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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()
);
}
}
8 changes: 8 additions & 0 deletions src/test/php/text/json/unittest/StreamInputTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
8 changes: 8 additions & 0 deletions src/test/php/text/json/unittest/StreamOutputTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}

0 comments on commit f7b3538

Please sign in to comment.