Add KeyType::CTRL helper method
timw4mail/php-kilo/master There was a failure building this commit Details

This commit is contained in:
Timothy Warren 2020-02-05 15:20:32 -05:00
parent 366945bda1
commit afd6560db4
2 changed files with 37 additions and 1 deletions

View File

@ -38,7 +38,8 @@ class KeyType {
// a = 0x61
// z = 0x7a
if ($ord >= 0x61 && $ord <= 0x7a)
// So, 0x60 < $ord < 0x7b
if ($ord > 0x60 && $ord < 0x7b)
{
return chr(ctrl_key($char));
}

View File

@ -0,0 +1,35 @@
<?php declare(strict_types=1);
namespace Aviat\Kilo\Tests\Enum;
use Aviat\Kilo\Enum\KeyType;
use PHPUnit\Framework\TestCase;
class KeyTypeTest extends TestCase {
public function testSanityCheck(): void
{
for ($i = 1; $i < 27; $i++)
{
$char = chr(0x60 + $i);
$ord = $i;
$expected = chr($ord);
$actual = KeyType::CTRL($char);
$this->assertEquals($expected, $actual, "CTRL + '{$char}' should return chr($ord)");
}
}
public function testNullOnInvalidChar(): void
{
$this->assertNull(KeyType::CTRL("\t"));
}
public function testSameOutputOnUpperOrLower(): void
{
$lower = KeyType::CTRL('v');
$upper = KeyType::CTRL('V');
$this->assertEquals($lower, $upper);
}
}