Fix: Pass parameter to generate() and add composite format tests

- Fixed bug where render() was not passing caller-supplied  to
  generate(), causing ad-hoc tokens to be ignored
- Added %F (yyyy-MM-dd) and %D (MM/dd/yy) composite date formats to
  the IntlDateFormatter pattern map
- Added test coverage for composite date format directives (%F, %D, %T, %R)
This commit is contained in:
Ollama
2026-03-13 21:35:31 +00:00
parent 095e7a4c36
commit 609cb323d4
2 changed files with 41 additions and 3 deletions

View File

@@ -20,7 +20,9 @@ class Token_lib
'%b' => 'MMM',
'%B' => 'MMMM',
'%d' => 'dd',
'%D' => 'MM/dd/yy',
'%e' => 'd',
'%F' => 'yyyy-MM-dd',
'%j' => 'D',
'%m' => 'MM',
'%U' => 'w',
@@ -71,7 +73,7 @@ class Token_lib
$token_values = [];
$tokens_to_replace = [];
$this->generate($token_tree, $tokens_to_replace, $token_values, $save);
$this->generate($token_tree, $tokens, $tokens_to_replace, $token_values, $save);
return str_replace($tokens_to_replace, $token_values, $tokened_text);
}
@@ -192,10 +194,10 @@ class Token_lib
return $results;
}
private function generate(array $used_tokens, array &$tokens_to_replace, array &$token_values, bool $save = true): void
private function generate(array $used_tokens, array $tokens, array &$tokens_to_replace, array &$token_values, bool $save = true): void
{
foreach ($used_tokens as $token_code => $token_info) {
$token_value = $this->resolve_token($token_code, [], $save);
$token_value = $this->resolve_token($token_code, $tokens, $save);
foreach ($token_info as $length => $token_spec) {
$tokens_to_replace[] = $token_spec;

View File

@@ -228,4 +228,40 @@ class Token_libTest extends CIUnitTestCase
$this->assertArrayHasKey('token1', $result);
$this->assertArrayHasKey('token2', $result);
}
public function testRenderReplacesCompositeDirectivePercentF(): void
{
$input = 'Date: %F';
$result = $this->tokenLib->render($input, [], false);
$this->assertNotEmpty($result);
$this->assertStringNotContainsString('%F', $result);
$this->assertMatchesRegularExpression('/Date: \d{4}-\d{2}-\d{2}/', $result);
}
public function testRenderReplacesCompositeDirectivePercentD(): void
{
$input = 'Date: %D';
$result = $this->tokenLib->render($input, [], false);
$this->assertNotEmpty($result);
$this->assertStringNotContainsString('%D', $result);
$this->assertMatchesRegularExpression('/Date: \d{2}\/\d{2}\/\d{2}/', $result);
}
public function testRenderHandlesPercentT(): void
{
$input = 'Time: %T';
$result = $this->tokenLib->render($input, [], false);
$this->assertNotEmpty($result);
$this->assertStringNotContainsString('%T', $result);
$this->assertMatchesRegularExpression('/Time: \d{2}:\d{2}:\d{2}/', $result);
}
public function testRenderHandlesPercentR(): void
{
$input = 'Time: %R';
$result = $this->tokenLib->render($input, [], false);
$this->assertNotEmpty($result);
$this->assertStringNotContainsString('%R', $result);
$this->assertMatchesRegularExpression('/Time: \d{2}:\d{2}/', $result);
}
}