Skip to content

Commit

Permalink
v2.5.0
Browse files Browse the repository at this point in the history
Changelog excerpt:
- Added tests for the Demojibakefier class.
  • Loading branch information
Maikuolan committed Feb 22, 2021
1 parent 5d2b982 commit 7d6338f
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 35 deletions.
10 changes: 10 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
10 changes: 6 additions & 4 deletions tests/ComplexStringHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
54 changes: 54 additions & 0 deletions tests/DemojibakefierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Demojibakefier tests file.
*
* This file is a part of the "common classes package", utilised by a number of
* packages and projects, including CIDRAM and phpMussel.
* Source: https://github.com/Maikuolan/Common
*/

require $ClassesDir . $Case . '.php';

/** Korean placeholder text. Source: <http://yoondongju.yonsei.ac.kr/poet/poem.asp?ID=1>. */
$TextKO = '죽는 날까지 하늘을 우러러 한점 부끄럼이 없기를, 잎새에 이는 바람에도 나는 괴로워했다. 별을 노래하는 마음으로 모든 죽어가는 것을 사랑해야지 그리고 나한테 주어진 길을 걸어가야겠다. 오늘밤에도 별이 바람에 스치운다.';

/** Preamble of the United Nations Charter in Chinese <https://www.un.org/zh/sections/un-charter/preamble/index.html>. */
$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.
*/
24 changes: 14 additions & 10 deletions tests/L10NTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
}
}
14 changes: 8 additions & 6 deletions tests/NumberFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
}
28 changes: 17 additions & 11 deletions tests/YAMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,29 +47,33 @@
'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');

$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;
Expand All @@ -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);
}
6 changes: 3 additions & 3 deletions tests/fixtures/example.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# An example YAML file.

String foo: "Bar"
Integer foo: 1234
Float foo: 123.4
Expand All @@ -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
Expand All @@ -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

0 comments on commit 7d6338f

Please sign in to comment.