From 86c107ae5ebad5cf43f7a0c1bed122755cf139a1 Mon Sep 17 00:00:00 2001 From: salehhashemi1992 <81674631+salehhashemi1992@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:16:46 +0330 Subject: [PATCH] Extract ANSI escape sequence to a function --- src/Functions.php | 13 +++++++++++++ tests/Visual/Collision.php | 2 +- tests/Visual/Help.php | 2 +- tests/Visual/Parallel.php | 2 +- tests/Visual/SingleTestOrDirectory.php | 2 +- tests/Visual/Todo.php | 4 +--- tests/Visual/Version.php | 2 +- 7 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Functions.php b/src/Functions.php index 465f3add..d8769549 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -194,3 +194,16 @@ if (! function_exists('afterAll')) { TestSuite::getInstance()->afterAll->set($closure); } } + +if (! function_exists('removeAnsiEscapeSequences')) { + /** + * Remove ANSI escape sequences from a given string. + * + * @param string $input The string containing ANSI escape sequences. + * @return string|null A new string with all ANSI escape sequences removed. + */ + function removeAnsiEscapeSequences(string $input): ?string + { + return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $input); + } +} diff --git a/tests/Visual/Collision.php b/tests/Visual/Collision.php index 23f9b815..0b66652c 100644 --- a/tests/Visual/Collision.php +++ b/tests/Visual/Collision.php @@ -10,7 +10,7 @@ test('collision', function (array $arguments) { $process->run(); - return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); + return removeAnsiEscapeSequences($process->getOutput()); }; $outputContent = explode("\n", $output()); diff --git a/tests/Visual/Help.php b/tests/Visual/Help.php index 4f48171d..eea3fd99 100644 --- a/tests/Visual/Help.php +++ b/tests/Visual/Help.php @@ -6,7 +6,7 @@ test('visual snapshot of help command output', function () { $process->run(); - return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); + return removeAnsiEscapeSequences($process->getOutput()); }; expect($output())->toMatchSnapshot(); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index ef7e0e79..2086cd6a 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -11,7 +11,7 @@ $run = function () { $process->run(); - return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); + return removeAnsiEscapeSequences($process->getOutput()); }; test('parallel', function () use ($run) { diff --git a/tests/Visual/SingleTestOrDirectory.php b/tests/Visual/SingleTestOrDirectory.php index 21223275..63e29490 100644 --- a/tests/Visual/SingleTestOrDirectory.php +++ b/tests/Visual/SingleTestOrDirectory.php @@ -9,7 +9,7 @@ $run = function (string $target, $decorated = false) { $process->run(); - return $decorated ? $process->getOutput() : preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); + return $decorated ? $process->getOutput() : removeAnsiEscapeSequences($process->getOutput()); }; $snapshot = function ($name) { diff --git a/tests/Visual/Todo.php b/tests/Visual/Todo.php index a6307435..c1b183cd 100644 --- a/tests/Visual/Todo.php +++ b/tests/Visual/Todo.php @@ -11,9 +11,7 @@ $run = function (string $target, bool $parallel) { expect($process->getExitCode())->toBe(0); - $outputContent = preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); - - return $outputContent; + return removeAnsiEscapeSequences($process->getOutput()); }; $snapshot = function ($name) { diff --git a/tests/Visual/Version.php b/tests/Visual/Version.php index 50b156b8..abdb6cbf 100644 --- a/tests/Visual/Version.php +++ b/tests/Visual/Version.php @@ -6,7 +6,7 @@ test('visual snapshot of help command output', function () { $process->run(); - return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); + return removeAnsiEscapeSequences($process->getOutput()); }; expect($output())->toMatchSnapshot();