Skip to content

Commit

Permalink
Merge pull request #12 from localheinz/feature/idempotency
Browse files Browse the repository at this point in the history
Enhancement: Ensure print() is idempotent
  • Loading branch information
localheinz authored Jan 5, 2018
2 parents ad702b6 + 858a713 commit cdd589a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public function print(string $original, bool $unEscapeUnicode, bool $unEscapeSla
continue;
}

/**
* Ignore whitespace.
*/
if ('' === \trim($character)) {
continue;
}

/**
* Process string literal if we are about to leave it.
*/
Expand Down
100 changes: 100 additions & 0 deletions test/Unit/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,106 @@ public function testPrintPrintsObjectPrettyWithUnEscapeUnicodeAndUnEscapeSlashes
$this->assertSame($expected, $printed);
}

public function testPrintPrintsObjectPrettyIdempotently()
{
$original = <<<'JSON'
{
"name": "Andreas M\u00f6ller",
"emoji": "🤓",
"urls": [
"https:\/\/localheinz.com",
"https:\/\/github.com\/localheinz",
"https:\/\/twitter.com\/localheinz"
]
}
JSON;

$printer = new Printer();

$printed = $printer->print(
$original,
false,
false
);

$this->assertSame($original, $printed);
}

public function testPrintPrintsObjectPrettyWithUnEscapeUnicodeIdempotently()
{
$original = <<<'JSON'
{
"name": "Andreas Möller",
"emoji": "🤓",
"urls": [
"https:\/\/localheinz.com",
"https:\/\/github.com\/localheinz",
"https:\/\/twitter.com\/localheinz"
]
}
JSON;

$printer = new Printer();

$printed = $printer->print(
$original,
true,
false
);

$this->assertSame($original, $printed);
}

public function testPrintPrintsObjectPrettyWithUnEscapeSlashesIdempotently()
{
$original = <<<'JSON'
{
"name": "Andreas M\u00f6ller",
"emoji": "🤓",
"urls": [
"https://localheinz.com",
"https://github.com/localheinz",
"https://twitter.com/localheinz"
]
}
JSON;

$printer = new Printer();

$printed = $printer->print(
$original,
false,
true
);

$this->assertSame($original, $printed);
}

public function testPrintPrintsObjectPrettyWithUnEscapeUnicodeAndUnEscapeSlashesIdempotently()
{
$original = <<<'JSON'
{
"name": "Andreas Möller",
"emoji": "🤓",
"urls": [
"https://localheinz.com",
"https://github.com/localheinz",
"https://twitter.com/localheinz"
]
}
JSON;

$printer = new Printer();

$printed = $printer->print(
$original,
true,
true
);

$this->assertSame($original, $printed);
}

/**
* @see https://github.com/zendframework/zend-json/pull/37
*/
Expand Down

0 comments on commit cdd589a

Please sign in to comment.