diff --git a/Changelog.txt b/Changelog.txt index 7ea2646..ae2d092 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -11,6 +11,11 @@ found at: === Changes made since last versioned release === +(none) + +=== Version/Release 2.5.0 === +MINOR RELEASE. + - [2020.12.07; Maikuolan]: Updated the number formatter to prevent leading decimal separators in the absence of representing any whole digits. @@ -27,6 +32,11 @@ found at: - [2021.02.20; Maikuolan]: Added support for __toString and for anchors to the YAML handler. Also updated some PHPDocs. +- [2021.02.22; Maikuolan]: Added tests for the Demojibakefier class. + +Caleb M (Maikuolan), +February 22, 2020. + === Version/Release 2.4.0 === MINOR RELEASE. diff --git a/tests.php b/tests.php index da7c8c1..0bd8fde 100644 --- a/tests.php +++ b/tests.php @@ -21,11 +21,12 @@ $ClassesDir = __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR; // Run tests. -foreach (['ComplexStringHandler', 'L10N', 'NumberFormatter', 'YAML'] as $Case) { +foreach (['ComplexStringHandler', 'Demojibakefier', 'L10N', 'NumberFormatter', 'YAML'] as $Case) { if (!is_readable($ClassesDir . $Case . '.php') || !is_readable($TestsDir . $Case . 'Test.php')) { echo $Case . '.php is not readable.' . PHP_EOL; exit(1); } + $ExitCode = 2; require $TestsDir . $Case . 'Test.php'; } diff --git a/tests/ComplexStringHandlerTest.php b/tests/ComplexStringHandlerTest.php index 966faa8..bf7af87 100644 --- a/tests/ComplexStringHandlerTest.php +++ b/tests/ComplexStringHandlerTest.php @@ -20,10 +20,12 @@ }, true); if ('(ab) "1" (cd) "2" (ef) "3" (gh) "4" (ij) "5" (kl) "6" (mn) "7" (op) "8" (qr) "9" (st) "10" (uv)' !== $ComplexStringHandler->recompile()) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(2); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } +$ExitCode++; + $ComplexStringHandler = new \Maikuolan\Common\ComplexStringHandler(); $ComplexStringHandler->Input = $TheString; $ComplexStringHandler->generateMarkers($ThePattern); @@ -35,6 +37,6 @@ }, true); if ('(ab) "1" (cd) "2" (ef) "3" (gh) "4" (ij) "5" (kl) "6" (mn) "7" (op) "8" (qr) "9" (st) "10" (uv)' !== $ComplexStringHandler->recompile()) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(3); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } diff --git a/tests/DemojibakefierTest.php b/tests/DemojibakefierTest.php new file mode 100644 index 0000000..e9087ca --- /dev/null +++ b/tests/DemojibakefierTest.php @@ -0,0 +1,54 @@ +. */ +$TextKO = '죽는 날까지 하늘을 우러러 한점 부끄럼이 없기를, 잎새에 이는 바람에도 나는 괴로워했다. 별을 노래하는 마음으로 모든 죽어가는 것을 사랑해야지 그리고 나한테 주어진 길을 걸어가야겠다. 오늘밤에도 별이 바람에 스치운다.'; + +/** Preamble of the United Nations Charter in Chinese . */ +$TextZH = '序言我联合国人民同兹决心欲免后世再遭今代人类两度身历惨不堪言之战祸,重申基本人权,人格尊严与价值,以及男女与大小各国平等权利之信念,创造适当环境,俾克维持正义,尊重由条约与国际法其他渊源而起之义务,久而弗懈,促成大自由中之社会进步及较善之民生,并为达此目的力行容恕,彼此以善邻之道,和睦相处,集中力量,以维持国际和平及安全,接受原则,确立方法,以保证非为公共利益,不得使用武力,运用国际机构,以促成全球人民经济及社会之进展,用是发愤立志,务当同心协力,以竟厥功爰由我各本国政府,经齐集金山市之代表各将所奉全权证书,互相校阅,均属妥善,议定本联合国宪章,并设立国际组织,定名联合国。'; + +$DataKO = iconv('UTF-8', 'UTF-16BE', $TextKO); +$DataZH = iconv('UTF-8', 'GB18030', $TextZH); + +/** Firstly, confirm it's naturally reversible (without Demojibakefier's help). */ +if (iconv('UTF-16BE', 'UTF-8', $DataKO) !== $TextKO) { + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); +} +$ExitCode++; +if (iconv('GB18030', 'UTF-8', $DataZH) !== $TextZH) { + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); +} + +/** Now we'll build our compound "wrong" data. */ +$Sample = $DataKO . "\n" . $DataZH; + +/** Instantiate the object. */ +$Demojibakefier = new \Maikuolan\Common\Demojibakefier(); + +$ExitCode++; +$Reversed = explode("\n", $Demojibakefier->guard($Sample), 2); + +/** Now let's check whether Demojibakefier can reverse it itself. */ +$ExitCode++; +if ($Reversed[0] !== $TextKO || $Reversed[1] !== $TextZH) { + var_dump($Reversed); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); +} + +/** + * There are still a bunch of things that don't quite entirely work as intended + * with the Demojibakefier class, but I can't test for those things until I fix + * them properly anyway, hence why the tests here are quite small right now. + * I'll update the tests later, after those oddities have been weeded out. + */ diff --git a/tests/L10NTest.php b/tests/L10NTest.php index cc6fa22..b25be00 100644 --- a/tests/L10NTest.php +++ b/tests/L10NTest.php @@ -27,16 +27,18 @@ $L10N = new \Maikuolan\Common\L10N($DataFR, $DataEN); if ('Bonjour ! Je m\'appelle Mary Sue.' !== sprintf($L10N->getString('MyName'), 'Mary Sue')) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(4); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } +$ExitCode++; if ('Quel est votre nom ?' !== $L10N->getString('YourName')) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(5); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } +$ExitCode++; if ('Do you speak English?' !== $L10N->getString('DoYouSpeak')) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(6); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } $DataEN = [ @@ -83,15 +85,17 @@ ]; foreach (range(0, 5) as $Number) { + $ExitCode++; if ($ExpectedRU[$Number] !== sprintf($L10N->getPlural($Number, 'apples'), $Number)) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(7); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } } foreach (range(0, 5) as $Number) { + $ExitCode++; if ($ExpectedEN[$Number] !== sprintf($L10N->getPlural($Number, 'oranges'), $Number)) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(8); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } } diff --git a/tests/NumberFormatterTest.php b/tests/NumberFormatterTest.php index 42fed0f..44d952a 100644 --- a/tests/NumberFormatterTest.php +++ b/tests/NumberFormatterTest.php @@ -55,8 +55,8 @@ } if ($Actual !== $Expected) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(12); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } $Expected = ' @@ -105,9 +105,10 @@ } $Actual .= "\n"; +$ExitCode++; if ($Actual !== $Expected) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(13); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } $Formats = [ @@ -209,7 +210,8 @@ } $Actual .= "\n"; +$ExitCode++; if ($Actual !== $Expected) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(14); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } diff --git a/tests/YAMLTest.php b/tests/YAMLTest.php index 6b55d64..0ed91d8 100644 --- a/tests/YAMLTest.php +++ b/tests/YAMLTest.php @@ -37,7 +37,7 @@ ], ], ], - 'Example hex-encoded data' => 'Hello World (but in hex)', + 'Example hex-encoded data' => "Hello World (but in hex)\0", 'Multi-line example' => "h e l l o - w o r l d\nhello-world", 'Example booleans and null' => [ 'This is true' => true, @@ -47,6 +47,8 @@ 'This is null' => null, 'This is also null' => null ], + 'Anchored text push' => 'Some placeholder text.', + 'Anchored text pull' => 'Some placeholder text.' ]; $RawYAML = file_get_contents($TestsDir . 'fixtures' . DIRECTORY_SEPARATOR . 'example.yaml'); @@ -54,22 +56,24 @@ $Object = new \Maikuolan\Common\YAML($RawYAML); if ($Expected !== $Object->Data) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(9); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } $Object = new \Maikuolan\Common\YAML(); $ProcessResult = $Object->process($RawYAML, $Object->Data); +$ExitCode++; if ($ProcessResult !== true) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(10); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } +$ExitCode++; if ($Expected !== $Object->Data) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(11); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } $InvalidYAML = 1000; @@ -78,12 +82,14 @@ $Object = new \Maikuolan\Common\YAML(); +$ExitCode++; if ($Object->process($InvalidYAML, $Object->Data) !== false) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(12); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } +$ExitCode++; if ($Object->process($NoNewLineYAML, $Object->Data) !== false) { - echo 'Test failed: ' . $Case . '().' . PHP_EOL; - exit(13); + echo 'Test failed: ' . $Case . ':L' . __LINE__ . '().' . PHP_EOL; + exit($ExitCode); } diff --git a/tests/fixtures/example.yaml b/tests/fixtures/example.yaml index ac72f3f..d51d1fd 100644 --- a/tests/fixtures/example.yaml +++ b/tests/fixtures/example.yaml @@ -1,5 +1,3 @@ -# An example YAML file. - String foo: "Bar" Integer foo: 1234 Float foo: 123.4 @@ -22,7 +20,7 @@ Example mixed multi-dimensional array: Hello: "World" Sub-sub array: Foobar: "Barfoo" -Example hex-encoded data: 0x48656c6c6f20576f726c64202862757420696e2068657829 +Example hex-encoded data: 0x48656c6c6f20576f726c64202862757420696e206865782900 Multi-line example: | h e l l o - w o r l d hello-world @@ -33,3 +31,5 @@ Example booleans and null: This is also false: - This is null: null This is also null: ~ +Anchored text push: &TestAnchor Some placeholder text. +Anchored text pull: *TestAnchor