Skip to content

Commit

Permalink
fix(testing): Make Testing TextProcessing providers unicode safe
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Jul 26, 2024
1 parent 87dc061 commit 0c6fc86
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 15 additions & 1 deletion apps/testing/lib/Provider/FakeTextProcessingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@ public function getName(): string {
}

public function process(string $prompt): string {
return strrev($prompt) . ' (done with FakeTextProcessingProvider)';
return $this->mb_strrev($prompt) . ' (done with FakeTextProcessingProvider)';
}

public function getTaskType(): string {
return FreePromptTaskType::class;
}

/**
* Reverse a miltibyte string.
*
* @param string $string The string to be reversed.
* @param string|null $encoding The character encoding. If it is omitted, the internal character encoding value
* will be used.
* @return string The reversed string
*/
private function mb_strrev(string $string, string $encoding = null): string
{
$chars = mb_str_split($string, 1, $encoding ?: mb_internal_encoding());

Check notice

Code scanning / Psalm

RiskyTruthyFalsyComparison Note test

Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
return implode('', array_reverse($chars));
}
}
16 changes: 15 additions & 1 deletion apps/testing/lib/Provider/FakeTextProcessingProviderSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getName(): string {
}

public function process(string $prompt): string {
return strrev($prompt) . ' (done with FakeTextProcessingProviderSync)';
return $this->mb_strrev($prompt) . ' (done with FakeTextProcessingProviderSync)';
}

public function getTaskType(): string {
Expand All @@ -30,4 +30,18 @@ public function getTaskType(): string {
public function getExpectedRuntime(): int {
return 1;
}

/**
* Reverse a miltibyte string.
*
* @param string $string The string to be reversed.
* @param string|null $encoding The character encoding. If it is omitted, the internal character encoding value
* will be used.
* @return string The reversed string
*/
private function mb_strrev(string $string, string $encoding = null): string
{
$chars = mb_str_split($string, 1, $encoding ?: mb_internal_encoding());

Check notice

Code scanning / Psalm

RiskyTruthyFalsyComparison Note test

Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
return implode('', array_reverse($chars));
}
}

0 comments on commit 0c6fc86

Please sign in to comment.