CODE/UNICODE and CHAR/UNICHAR

PhpSpreadsheet treats CODE and UNICODE as equivalent, likewise for CHAR and UNICHAR. They are, in fact, different. CODE and CHAR deal only with single-byte character sets (Windows-1252 or MacRoman), while UNICODE and UNICHAR deal with all of Unicode. This PR separates them. The existing unit test for CODE was, in many cases, applicable to UNICODE (for which there was no separate test). The tests are corrected for CODE, new tests are added, and a separate test for UNICODE is added. CHAR was mostly okay, new tests are added, and a separate test for UNICHAR is added.
This commit is contained in:
oleibman
2025-11-28 13:21:27 -08:00
parent cd4e71ed77
commit b243f2f4e5
13 changed files with 310 additions and 41 deletions
@@ -5,13 +5,44 @@ declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\TextData\CharacterConvert as CC;
use PHPUnit\Framework\Attributes\DataProvider;
class CodeTest extends AllSetupTeardown
{
protected function tearDown(): void
{
parent::tearDown();
CC::setOneByteCharacterSet('Windows-1252');
}
#[DataProvider('providerCODE')]
public function testCODE(mixed $expectedResult, mixed $character = 'omitted'): void
{
// if espected is array, 1st is for code, 2nd for unicode
if (is_array($expectedResult)) {
$expectedResult = $expectedResult[0];
}
$this->mightHaveException($expectedResult);
$sheet = $this->getSheet();
if ($character === 'omitted') {
$sheet->getCell('B1')->setValue('=CODE()');
} else {
$this->setCell('A1', $character);
$sheet->getCell('B1')->setValue('=CODE(A1)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
#[DataProvider('providerCODE')]
public function testMacCODE(mixed $expectedResult, mixed $character = 'omitted'): void
{
CC::setOneByteCharacterSet('MACROMAN');
// if espected is array, 1st is for code, 2nd unicode, 3rd Mac CODE
if (is_array($expectedResult)) {
$expectedResult = $expectedResult[2] ?? $expectedResult[0];
}
$this->mightHaveException($expectedResult);
$sheet = $this->getSheet();
if ($character === 'omitted') {
@@ -48,4 +79,11 @@ class CodeTest extends AllSetupTeardown
'matrix' => [[[89, 111], [108, 111]], '{"Y", "o"; "l", "o"}'],
];
}
public function testCharacterSet(): void
{
self::assertFalse(CC::setOneByteCharacterSet('UTF-8'));
self::assertTrue(CC::setOneByteCharacterSet('MACROMAN'));
self::assertTrue(CC::setOneByteCharacterSet('Windows-1252'));
}
}