|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\PHP\JSString; |
| 6 | + |
| 7 | +use JSString; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use RangeError; |
| 10 | + |
| 11 | +final class fromCodePointTest extends TestCase { |
| 12 | + function test_Demo_String_fromCodePoint(): void { |
| 13 | + self::assertSame( |
| 14 | + 'ββ
β²π― ', |
| 15 | + (string) JSString::fromCodePoint(9731, 9733, 9842, 0x2f804) |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + function test_Using_fromCodePoint(): void { |
| 20 | + // Valid input: |
| 21 | + self::assertSame('*', (string) JSString::fromCodePoint(42)); |
| 22 | + self::assertSame('AZ', (string) JSString::fromCodePoint(65, 90)); |
| 23 | + self::assertSame('Π', (string) JSString::fromCodePoint(0x404)); |
| 24 | + self::assertSame('π― ', (string) JSString::fromCodePoint(0x2f804)); |
| 25 | + self::assertSame('π― ', (string) JSString::fromCodePoint(194564)); |
| 26 | + self::assertSame('πaπ', (string) JSString::fromCodePoint(0x1d306, 0x61, 0x1d307)); |
| 27 | + |
| 28 | + // Invalid input: |
| 29 | + self::expectException(RangeError::class); |
| 30 | + JSString::fromCodePoint('_'); |
| 31 | + JSString::fromCodePoint(Infinity); |
| 32 | + JSString::fromCodePoint(-1); |
| 33 | + JSString::fromCodePoint(3.14); |
| 34 | + JSString::fromCodePoint(3e-2); |
| 35 | + JSString::fromCodePoint(NaN); |
| 36 | + } |
| 37 | + |
| 38 | + function test_Compared_to_fromCharCode(): void { |
| 39 | + /* |
| 40 | + String.fromCharCode() cannot return supplementary characters (i.e. code |
| 41 | + points 0x010000 β 0x10FFFF) by specifying their code point. Instead, it |
| 42 | + requires the UTF-16 surrogate pair in order to return a supplementary |
| 43 | + character: |
| 44 | + */ |
| 45 | + // self::assertSame('π', (string) JSString::fromCodePoint(0xd83c, 0xdf03)); |
| 46 | + // self::assertSame('π', (string) JSString::fromCodePoint(55356, 57091)); |
| 47 | + |
| 48 | + /* |
| 49 | + String.fromCodePoint(), on the other hand, can return 4-byte supplementary |
| 50 | + characters, as well as the more common 2-byte BMP characters, by specifying |
| 51 | + their code point (which is equivalent to the UTF-32 code unit): |
| 52 | + */ |
| 53 | + self::assertSame('π', (string) JSString::fromCodePoint(0x1f303)); |
| 54 | + } |
| 55 | +} |
0 commit comments