|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\PHP\JSString; |
| 6 | + |
| 7 | +use JSString; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +final class StringTest extends TestCase { |
| 11 | + function test_Creating_strings(): void { |
| 12 | + $string1 = String("A string primitive"); |
| 13 | + $string2 = String('Also a string primitive'); |
| 14 | + // $string3 = String(`Yet another string primitive`); |
| 15 | + $string4 = new JSString("A String object"); |
| 16 | + |
| 17 | + self::assertInstanceOf(JSString::class, $string1); |
| 18 | + self::assertInstanceOf(JSString::class, $string2); |
| 19 | + // self::assertInstanceOf(JSString::class, $string3); |
| 20 | + self::assertInstanceOf(JSString::class, $string4); |
| 21 | + } |
| 22 | + |
| 23 | + function test_Character_access(): void { |
| 24 | + self::assertEquals('a', String("cat")->charAt(1)); |
| 25 | + self::assertEquals('a', String("cat")[1]); |
| 26 | + } |
| 27 | + |
| 28 | + function test_Comparing_strings(): void { |
| 29 | + $a = String("a"); |
| 30 | + $b = String("b"); |
| 31 | + |
| 32 | + self::assertTrue($a < $b); |
| 33 | + |
| 34 | + $areEqualInUpperCase = function (string $str1, string $str2): bool { |
| 35 | + return String($str1)->toUpperCase() === String($str2)->toUpperCase(); |
| 36 | + }; |
| 37 | + |
| 38 | + $areEqualInLowerCase = function (string $str1, string $str2): bool { |
| 39 | + return String($str1)->toLowerCase() === String($str2)->toLowerCase(); |
| 40 | + }; |
| 41 | + |
| 42 | + // TODO |
| 43 | + // self::assertTrue($areEqualInUpperCase("Γ", "ss")); |
| 44 | + self::assertFalse($areEqualInLowerCase("Δ±", "I")); |
| 45 | + |
| 46 | + $areEqual = function (string $str1, string $str2, string $locale = "en-US"): bool { |
| 47 | + return String($str1)->localeCompare($str2, $locale, ['sensitivity' => 'accent']) === 0; |
| 48 | + }; |
| 49 | + |
| 50 | + self::assertFalse($areEqual("Γ", "ss", "de")); |
| 51 | + // TODO |
| 52 | + // self::assertTrue($areEqual("Δ±", "I", "tr")); |
| 53 | + } |
| 54 | + |
| 55 | + function test_String_primitives_and_String_objects(): void { |
| 56 | + $strPrim = String("foo"); |
| 57 | + $strPrim2 = String(1); |
| 58 | + $strPrim3 = String(true); |
| 59 | + $strObj = new JSString($strPrim); |
| 60 | + |
| 61 | + self::assertEquals('1', $strPrim2); |
| 62 | + self::assertEquals('true', $strPrim3); |
| 63 | + |
| 64 | + self::assertSame('string', gettype((string) $strPrim)); |
| 65 | + self::assertSame('string', gettype((string) $strPrim2)); |
| 66 | + self::assertSame('string', gettype((string) $strPrim3)); |
| 67 | + self::assertSame('object', gettype($strObj)); |
| 68 | + |
| 69 | + $s1 = String("2 + 2"); // creates a string primitive |
| 70 | + $s2 = new JSString("2 + 2"); // creates a String object |
| 71 | + |
| 72 | + self::assertSame(4, jsEval($s1)); |
| 73 | + self::assertSame('2 + 2', jsEval($s2)); |
| 74 | + |
| 75 | + self::assertSame(4, jsEval($s2->valueOf())); |
| 76 | + } |
| 77 | + |
| 78 | + function test_String_coercion(): void { |
| 79 | + self::assertEquals('string', String('string')); |
| 80 | + self::assertEquals('undefined', String(undefined)); |
| 81 | + self::assertEquals('null', String(null)); |
| 82 | + self::assertEquals('true', String(true)); |
| 83 | + self::assertEquals('false', String(false)); |
| 84 | + // TODO: Numbers are converted with the same algorithm as toString(10). |
| 85 | + // TODO: BigInts are converted with the same algorithm as toString(10). |
| 86 | + // TODO: Symbols throw a TypeError. |
| 87 | + /* |
| 88 | + TODO: Objects are first converted to a primitive by calling its |
| 89 | + [Symbol.toPrimitive]() (with "string" as hint), toString(), and valueOf() |
| 90 | + methods, in that order. The resulting primitive is then converted to a |
| 91 | + string. |
| 92 | + */ |
| 93 | + |
| 94 | + /* |
| 95 | + TODO: Template literal: `${x}` does exactly the string coercion steps |
| 96 | + explained above for the embedded expression. |
| 97 | + */ |
| 98 | + /* |
| 99 | + TODO: The String() function: String(x) uses the same algorithm to convert |
| 100 | + x, except that Symbols don't throw a TypeError, but return |
| 101 | + "Symbol(description)", where description is the description of the Symbol. |
| 102 | + */ |
| 103 | + /* |
| 104 | + TODO: Using the + operator: "" + x coerces its operand to a primitive |
| 105 | + instead of a string, and, for some objects, has entirely different |
| 106 | + behaviors from normal string coercion. See its reference page for more |
| 107 | + details. |
| 108 | + */ |
| 109 | + } |
| 110 | + |
| 111 | + /*function test_UTF_16_characters_Unicode_code_points_and_grapheme_clusters(): void { |
| 112 | + String("π")->split(""); // ['\ud83d', '\ude04']; splits into two lone surrogates |
| 113 | +
|
| 114 | + // "Backhand Index Pointing Right: Dark Skin Tone" |
| 115 | + // [..."ππΏ"]; // ['π', 'πΏ'] |
| 116 | + // splits into the basic "Backhand Index Pointing Right" emoji and |
| 117 | + // the "Dark skin tone" emoji |
| 118 | +
|
| 119 | + // "Family: Man, Boy" |
| 120 | + // [..."π¨βπ¦"]; // [ 'π¨', 'β', 'π¦' ] |
| 121 | + // splits into the "Man" and "Boy" emoji, joined by a ZWJ |
| 122 | +
|
| 123 | + // The United Nations flag |
| 124 | + // [..."πΊπ³"]; // [ 'πΊ', 'π³' ] |
| 125 | + // splits into two "region indicator" letters "U" and "N". |
| 126 | + // All flag emojis are formed by joining two region indicator letters |
| 127 | + }*/ |
| 128 | + |
| 129 | + function test_HTML_wrapper_methods(): void { |
| 130 | + self::assertEquals('<b></b></b>', String('</b>')->bold()); |
| 131 | + |
| 132 | + self::assertEquals( |
| 133 | + '<a name=""Hello"">foo</a>', |
| 134 | + String("foo")->anchor('"Hello"') |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + function test_String_conversion(): void { |
| 139 | + $nullVar = null; |
| 140 | + // TODO |
| 141 | + // $nullVar->toString(); // TypeError: Cannot read properties of null |
| 142 | + self::assertEquals('null', String($nullVar)); |
| 143 | + |
| 144 | + $undefinedVar = undefined; |
| 145 | + // TODO |
| 146 | + // $undefinedVar->toString(); // TypeError: Cannot read properties of undefined |
| 147 | + self::assertEquals('undefined', String($undefinedVar)); |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + private function areEqualCaseInsensitive(JSString $str1, JSString $str2): bool { |
| 152 | + return $str1->toUpperCase() === $str2->toUpperCase(); |
| 153 | + } |
| 154 | +} |
0 commit comments