php-kilo/tests/FunctionTest.php

144 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2019-11-08 16:27:08 -05:00
<?php declare(strict_types=1);
namespace Aviat\Kilo\Tests;
2019-11-19 13:48:12 -05:00
use Aviat\Kilo\Enum\Color;
use Aviat\Kilo\Enum\Highlight;
2019-11-08 16:27:08 -05:00
use PHPUnit\Framework\TestCase;
2019-11-19 13:48:12 -05:00
use function Aviat\Kilo\array_replace_range;
use function Aviat\Kilo\ctrl_key;
use function Aviat\Kilo\is_ascii;
2020-02-05 14:50:31 -05:00
use function Aviat\Kilo\is_ctrl;
2019-11-19 13:48:12 -05:00
use function Aviat\Kilo\is_digit;
use function Aviat\Kilo\is_separator;
use function Aviat\Kilo\is_space;
2021-04-14 16:38:41 -04:00
use function Aviat\Kilo\str_has;
2019-11-19 13:48:12 -05:00
use function Aviat\Kilo\syntax_to_color;
use function Aviat\Kilo\tabs_to_spaces;
2019-11-08 16:27:08 -05:00
class FunctionTest extends TestCase {
2019-11-14 11:12:32 -05:00
public function test_is_ascii(): void
{
$this->assertFalse(is_ascii('©'));
$this->assertFalse(is_ascii("\x80"));
$this->assertTrue(is_ascii('a'));
2019-11-19 13:48:12 -05:00
$this->assertTrue(is_ascii("\x7b"));
}
public function test_ctrl_key(): void
{
$this->assertEquals(-1, ctrl_key("\x80"));
$this->assertEquals(0x01, ctrl_key('a'));
}
2020-02-05 14:50:31 -05:00
public function test_is_ctrl(): void
2019-11-19 13:48:12 -05:00
{
for ($i = 0x0; $i < 0x20; $i++)
{
$char = chr($i);
2020-02-05 14:50:31 -05:00
$this->assertTrue(is_ctrl($char), 'Should be a control character');
2019-11-19 13:48:12 -05:00
}
for ($n = 0x20; $n < 0x7f; $n++)
{
$char = chr($n);
2020-02-05 14:50:31 -05:00
$this->assertFalse(is_ctrl($char), 'Should not be a control character');
2019-11-19 13:48:12 -05:00
}
// Escape, code 7f, is an outlier
2020-02-05 14:50:31 -05:00
$this->assertTrue(is_ctrl(chr(0x7f)));
2019-11-19 13:48:12 -05:00
}
public function test_is_space(): void
{
$this->assertFalse(is_space("\x80"), 'Non-ascii character is not considered a space');
foreach ([' ', "\t", "\n", "\r", "\xa", "\xb", "\xc"] as $char)
{
$this->assertTrue(is_space($char), 'Should be considered a space character');
}
}
public function test_is_digit(): void
{
$this->assertFalse(is_separator("\x80"), 'Non-ascii character is not a digit');
$digits = str_split('01234567890');
foreach ($digits as $digit)
{
$this->assertTrue(is_digit($digit));
}
}
public function test_is_separator(): void
{
$this->assertFalse(is_separator("\x80"), 'Non-ascii character is not a separator');
$chars = str_split(',.()+-/*=~%<>[];');
foreach ($chars as $char)
{
$this->assertTrue(is_separator($char), 'The character should be considered a separator');
}
// Null byte is considered a separator
$this->assertTrue(is_separator("\0"));
}
public function test_syntax_to_color(): void
{
// Nonsense input returns FG::White
$this->assertEquals(syntax_to_color(999), Color::FG_WHITE);
$this->assertNotEquals(syntax_to_color(Highlight::OPERATOR), Color::FG_WHITE);
}
public function test_str_contains(): void
{
// Search from string offset
2021-04-14 16:38:41 -04:00
$this->assertTrue(str_has(' vcd', 'vcd', 2));
2019-11-19 13:48:12 -05:00
2021-04-14 16:38:41 -04:00
$this->assertFalse(str_has('', "\0"));
2019-11-19 13:48:12 -05:00
// An empty search string returns false
2021-04-14 16:38:41 -04:00
$this->assertFalse(str_has('', ''));
2019-11-19 13:48:12 -05:00
2021-04-14 16:38:41 -04:00
$this->assertTrue(str_has('alphabet', 'phab'));
2019-11-19 13:48:12 -05:00
}
public function test_tabs_to_spaces(): void
{
$original = "\t\t\t{";
2021-04-14 16:38:41 -04:00
$this->assertFalse(str_has(tabs_to_spaces($original), "\t"));
2019-11-19 13:48:12 -05:00
}
public function test_array_replace_range_length_1(): void
{
$original = [
'a', 'b', 'c', 'd', 'e', 'f'
];
$expected = ['a', 'b', 'c', 'foo', 'e', 'f'];
array_replace_range($original, 3, 1, 'foo');
$this->assertEquals($expected, $original);
}
public function test_array_replace_range_length_gt_1(): void
{
$original = [
'a', 'b', 'c', 'd', 'e', 'f'
];
$expected = [
'a', 'foo', 'foo', 'foo', 'e', 'f'
];
array_replace_range($original, 1, 3, 'foo');
$this->assertEquals($expected, $original);
2019-11-14 11:12:32 -05:00
}
2019-11-08 16:27:08 -05:00
}