Skip to content

Commit

Permalink
[minor] increase threshold to shorten strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Aug 12, 2021
1 parent e4ac0dc commit 972fee7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/Assert/AssertionFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
final class AssertionFailed extends \RuntimeException
{
private const MAX_SHORT_LENGTH = 100;

/** @var array */
private $context;

Expand Down Expand Up @@ -69,11 +71,12 @@ private static function normalizeContextValue($value): string

$value = \preg_replace('/\s+/', ' ', $value);

if (\mb_strlen($value) <= 40) {
if (\mb_strlen($value) <= self::MAX_SHORT_LENGTH) {
return $value;
}

return \sprintf('%s...%s', \mb_substr($value, 0, 27), \mb_substr($value, -10));
// shorten to max
return \sprintf('%s...%s', \mb_substr($value, 0, self::MAX_SHORT_LENGTH - 40 - 3), \mb_substr($value, -40));
}

private static function normalizeContext(array $context): array
Expand Down
13 changes: 8 additions & 5 deletions tests/AssertionFailedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
final class AssertionFailedTest extends TestCase
{
public const LONG = "If you're looking for random paragraphs, you've come to the right place. When a random word or a random sentence isn't quite enough, the next logical step is to find a random paragraph. We created the Random Paragraph Generator with you in mind. The process is quite simple. Choose the number of random paragraphs you'd like to see and click the button. Your chosen number of paragraphs will instantly appear.";
public const SHORT = "If you're looking for random paragraphs, you've come to t...ber of paragraphs will instantly appear.";

/**
* @test
*/
Expand All @@ -29,14 +32,14 @@ public function message_is_created_with_context_as_list_array(): void
new \stdClass(),
null,
['an', 'array'],
'The quick brown fox jumps over the lazy dog',
self::LONG,
"string\nwith\n\rline\nbreak",
false,
true,
]
);

$this->assertSame('message 1 string 4.3 stdClass (null) (array) The quick brown fox jumps o...e lazy dog string with line break (false) (true)', $exception->getMessage());
$this->assertSame(\sprintf('message 1 string 4.3 stdClass (null) (array) %s string with line break (false) (true)', self::SHORT), $exception->getMessage());
$this->assertSame($expected, $exception->context());
}

Expand All @@ -47,14 +50,14 @@ public function message_is_created_with_context_as_assoc_array(): void
{
$object = new \stdClass();
$messageTemplate = 'message {int} {string1} {float} {object} {array} {string2} {string3} {false} {true}';
$expectedMessage = 'message 1 value 4.3 stdClass (array) The quick brown fox jumps o...e lazy dog string with line break (false) (true)';
$expectedMessage = \sprintf('message 1 value 4.3 stdClass (array) %s string with line break (false) (true)', self::SHORT);
$expectedContext = [
'int' => 1,
'string1' => 'value',
'float' => 4.3,
'object' => $object,
'array' => ['foo'],
'string2' => 'The quick brown fox jumps over the lazy dog',
'string2' => self::LONG,
'string3' => "string\nwith\n\rline\nbreak",
'false' => false,
'true' => true,
Expand All @@ -71,7 +74,7 @@ public function message_is_created_with_context_as_assoc_array(): void
'float' => 4.3,
'{object}' => $object,
'{array}' => ['foo'],
'{string2}' => 'The quick brown fox jumps over the lazy dog',
'{string2}' => self::LONG,
'{string3}' => "string\nwith\n\rline\nbreak",
'{false}' => false,
'{true}' => true,
Expand Down
11 changes: 6 additions & 5 deletions tests/Handler/PHPUnitHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use Zenstruck\Assert;
use Zenstruck\Assert\Handler\PHPUnitHandler;
use Zenstruck\Assert\Tests\AssertionFailedTest;
use Zenstruck\Assert\Tests\Fixture\CountableObject;
use Zenstruck\Assert\Tests\Fixture\NegatableAssertion;
use Zenstruck\Assert\Tests\ResetHandler;
Expand Down Expand Up @@ -62,12 +63,12 @@ public function non_verbose_run_does_not_export_context(): void

try {
Assert::true(false, 'this fails {string} {array} {object}', [
'string' => 'The quick brown fox jumps over the lazy dog',
'string' => AssertionFailedTest::LONG,
'array' => ['an' => 'array'],
'object' => $this,
]);
} catch (AssertionFailedError $e) {
$this->assertSame(\sprintf('this fails The quick brown fox jumps o...e lazy dog (array) %s', __CLASS__), $e->getMessage());
$this->assertSame(\sprintf('this fails %s (array) %s', AssertionFailedTest::SHORT, __CLASS__), $e->getMessage());

return;
}
Expand All @@ -86,17 +87,17 @@ public function verbose_run_exports_context(): void

try {
Assert::true(false, 'this fails {string} {array} {object1} {object2}', [
'string' => 'The quick brown fox jumps over the lazy dog',
'string' => AssertionFailedTest::LONG,
'array' => ['an' => 'array'],
'object1' => $this,
'object2' => new NegatableAssertion(true),
]);
} catch (AssertionFailedError $e) {
$this->assertStringContainsString(
\sprintf("this fails The quick brown fox jumps o...e lazy dog (array) %s %s\n\nFailure Context:", __CLASS__, NegatableAssertion::class),
\sprintf("this fails %s (array) %s %s\n\nFailure Context:", AssertionFailedTest::SHORT, __CLASS__, NegatableAssertion::class),
$e->getMessage()
);
$this->assertStringContainsString("[string]\n'The quick brown fox jumps over the lazy dog'", $e->getMessage());
$this->assertStringContainsString(\sprintf("[string]\n'%s'", AssertionFailedTest::LONG), $e->getMessage());
$this->assertStringContainsString("[array]\nArray &0 (\n 'an' => 'array'\n)", $e->getMessage());
$this->assertStringContainsString(\sprintf("[object1]\n%s Object (...)", __CLASS__), $e->getMessage());
$this->assertStringContainsString(\sprintf("object2]\n%s Object &", NegatableAssertion::class), $e->getMessage());
Expand Down

0 comments on commit 972fee7

Please sign in to comment.