improved tests and test docs

This commit is contained in:
Dennis Eichhorn 2020-09-01 23:51:59 +02:00
parent bd97cee551
commit b084791809
8 changed files with 80 additions and 15 deletions

View File

@ -283,44 +283,43 @@ final class MbStringUtils
/**
* Get the utf-8 boundary of a string
*
* @param string $text To search for utf-8 boundary
* @param int $offset Search offset
* @param string $text QP text to search for utf-8 boundary
* @param int $length Last character boundary prior to this length
*
* @return int
*
* @since 1.0.0
*/
public static function utf8CharBoundary(string $text, int $offset = 0) : int
public static function utf8CharBoundary(string $text, int $length = 0) : int
{
$reset = 3;
$pos = $offset;
do {
$lastChunk = \substr($text, $pos - $reset, $reset);
$lastChunk = \substr($text, $length - $reset, $reset);
$encodedPos = \strpos($lastChunk, '=');
if ($encodedPos === false) {
break;
}
$hex = \substr($text, $pos - $reset + $encodedPos + 1, 2);
$hex = \substr($text, $length - $reset + $encodedPos + 1, 2);
$dec = \hexdec($hex);
if ($dec < 128) {
if ($encodedPos > 0) {
$pos -= $reset - $encodedPos;
$length -= $reset - $encodedPos;
}
break;
} elseif ($dec >= 192) {
$pos -= $reset - $encodedPos;
$length -= $reset - $encodedPos;
break;
} elseif ($dec < 192) {
} else { /* $dec < 192 */
$reset += 3;
}
} while (true);
return $pos;
return $length;
}
/**

View File

@ -12,7 +12,7 @@
*/
declare(strict_types=1);
namespace phpOMS\tests\Algorithm\PathFinding;
namespace phpOMS\tests\Algorithm\Maze;
use phpOMS\Algorithm\Maze\MazeGenerator;

View File

@ -140,8 +140,12 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase
self::assertEquals('USD 1.235', $this->l11nManager->getCurrency($l11n, 1.2345, 'long'));
$this->l11nManager->loadLanguage('en', '0', ['0' => ['CurrencyK' => 'K']]);
$this->l11nManager->loadLanguage('en', '0', ['0' => ['CurrencyM' => 'M']]);
$this->l11nManager->loadLanguage('en', '0', ['0' => ['CurrencyB' => 'B']]);
self::assertEquals('K$ 12.345', $this->l11nManager->getCurrency($l11n, 12345.0, 'long', '$', 1000));
self::assertEquals('KUSD 12.345', $this->l11nManager->getCurrency($l11n, 12345.0, 'long', null, 1000));
self::assertEquals('M$ 123.5', $this->l11nManager->getCurrency($l11n, 123456789.0, 'short', '$', 1000000));
self::assertEquals('B$ 1.2', $this->l11nManager->getCurrency($l11n, 1234567890.0, 'short', '$', 1000000000));
}
/**

View File

@ -12,7 +12,7 @@
*/
declare(strict_types=1);
namespace phpOMS\tests\Math\Number;
namespace phpOMS\tests\Math\Topology;
use phpOMS\Math\Topology\Metrics2D;

View File

@ -12,7 +12,7 @@
*/
declare(strict_types=1);
namespace phpOMS\tests\Math\Number;
namespace phpOMS\tests\Math\Topology;
use phpOMS\Math\Topology\Metrics2D;
use phpOMS\Math\Topology\MetricsND;

View File

@ -61,6 +61,32 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase
self::assertEquals('ab0', ArrayUtils::getArray('a/ab/1', $expected));
}
/**
* @testdox A invalid array path returns null
* @covers phpOMS\Utils\ArrayUtils
* @group framework
*/
public function testArrayInvalidArrayPath() : void
{
$expected = [
'a' => [
'aa' => 1,
'ab' => [
'aba',
'ab0',
[
3,
'c',
],
4,
],
],
2 => '2a',
];
self::assertEquals(null, ArrayUtils::getArray('a/zzz/1', $expected));
}
/**
* @testdox Test recursively if a value is in an array
* @covers phpOMS\Utils\ArrayUtils
@ -106,6 +132,27 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase
self::assertFalse(ArrayUtils::inArrayRecursive('aba', ArrayUtils::unsetArray('a/ab', $expected, '/')));
}
/**
* @testdox Deleting an invalid array path returns the original array
* @covers phpOMS\Utils\ArrayUtils
* @group framework
*/
public function testInvalidArrayDelete() : void
{
$expected = [
'a' => [
'aa' => 1,
'ab' => [
'aba',
'ab0',
],
],
2 => '2a',
];
self::assertEquals($expected, ArrayUtils::unsetArray('a/zzz', $expected, '/'));
}
/**
* @testdox The recursive sum of all values in an array can be calculated
* @covers phpOMS\Utils\ArrayUtils

View File

@ -19,7 +19,7 @@ use phpOMS\Utils\MbStringUtils;
require_once __DIR__ . '/../Autoloader.php';
/**
* @testdox phpOMS\tests\Utils\MbStringUtilsTest: String utilities
* @testdox phpOMS\tests\Utils\MbStringUtilsTest: Multi-Byte string utilities
*
* @internal
*/
@ -152,8 +152,13 @@ class MbStringUtilsTest extends \PHPUnit\Framework\TestCase
self::assertEquals(5, MbStringUtils::mb_count_chars('αααααΕεΙιΜμΨψ')['α']);
}
/**
* @testdox The previous boundary of a utf-8 encoded quoted printable is identified correctly
* @covers phpOMS\Utils\MbStringUtils
* @group framework
*/
public function testUtf8CharBoundary() : void
{
self::markTestIncomplete();
self::assertEquals(10, MbStringUtils::utf8CharBoundary('H=E4tten H=FCte ein =DF im Namen, w=E4ren sie m=F6glicherweise k', 12));
}
}

View File

@ -166,6 +166,16 @@ class StringUtilsTest extends \PHPUnit\Framework\TestCase
}));
}
/**
* @testdox Stringify/rendering a unknown data type returns null
* @covers phpOMS\Utils\StringUtils
* @group framework
*/
public function testInvalidStringify() : void
{
self::assertEquals(null, StringUtils::stringify(new class() {}));
}
/**
* @testdox The difference between two strings can be evaluated
* @covers phpOMS\Utils\StringUtils