Fix tests for refactored code
All checks were successful
timw4mail/php-kilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-03-11 17:11:00 -05:00
parent c5e1b6f1b2
commit 633094ea9f
4 changed files with 44 additions and 26 deletions

View File

@ -2,6 +2,8 @@
namespace Aviat\Kilo; namespace Aviat\Kilo;
use Aviat\Kilo\Type\Point;
/** /**
* The representation of the current document being edited * The representation of the current document being edited
* *
@ -10,6 +12,9 @@ namespace Aviat\Kilo;
class Document { class Document {
public ?Syntax $syntax = NULL; public ?Syntax $syntax = NULL;
// Tokens for highlighting PHP
public array $tokens = [];
private function __construct( private function __construct(
public array $rows = [], public array $rows = [],
public ?string $filename = NULL, public ?string $filename = NULL,

View File

@ -100,6 +100,7 @@ class Editor {
$this->cursor = Point::new(); $this->cursor = Point::new();
$this->offset = Point::new(); $this->offset = Point::new();
$this->terminalSize = Terminal::size(); $this->terminalSize = Terminal::size();
$this->document = Document::new();
if (is_string($filename)) if (is_string($filename))
{ {

View File

@ -3,16 +3,25 @@
namespace Aviat\Kilo\Tests\Traits; namespace Aviat\Kilo\Tests\Traits;
use Aviat\Kilo\Editor; use Aviat\Kilo\Editor;
use Aviat\Kilo\Type\TerminalSize;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots; use Spatie\Snapshots\MatchesSnapshots;
use function Aviat\Kilo\get_window_size;
class MockEditor extends Editor { class MockEditor extends Editor {
public function __set(string $key, $value): void public static function mock(string $filename = ''): Editor
{ {
if (property_exists($this, $key)) $self = self::new(2, ['kilo', $filename]);
$self->terminalSize = new TerminalSize(23, 80);
$self->statusMessage->time = 1234567890;
return $self;
}
public function __set(string $name, mixed $value): void
{
if (property_exists($this, $name))
{ {
$this->$key = $value; $this->$name = $value;
} }
} }
} }
@ -20,43 +29,35 @@ class MockEditor extends Editor {
class EditorTest extends TestCase { class EditorTest extends TestCase {
use MatchesSnapshots; use MatchesSnapshots;
protected Editor $editor;
public function setUp(): void
{
parent::setUp();
// Mock the screen size, since that can vary widely
$this->editor = MockEditor::new();
$this->editor->__set('screenRows', 23);
$this->editor->__set('screenCols', 80);
}
public function testSanity(): void public function testSanity(): void
{ {
$this->assertEquals(0, $this->editor->numRows); $editor = MockEditor::mock();
$this->assertNull($this->editor->syntax);
$this->assertEquals(0, $editor->numRows);
$this->assertNull($editor->syntax);
} }
public function test__debugInfo(): void public function test__debugInfo(): void
{ {
$state = json_encode($this->editor->__debugInfo(), JSON_THROW_ON_ERROR); $editor = MockEditor::mock();
$state = json_encode($editor->__debugInfo(), JSON_THROW_ON_ERROR);
$this->assertMatchesJsonSnapshot($state); $this->assertMatchesJsonSnapshot($state);
} }
public function testOpenPHP(): void public function testOpenPHP(): void
{ {
$this->editor->open('test.php'); $editor = MockEditor::mock('test.php');
$state = json_encode($this->editor, JSON_THROW_ON_ERROR);
$state = json_encode($editor, JSON_THROW_ON_ERROR);
$this->assertMatchesJsonSnapshot($state); $this->assertMatchesJsonSnapshot($state);
} }
public function testOpen(): void public function testOpen(): void
{ {
$this->editor->open('src/ffi.h'); $editor = MockEditor::mock('src/ffi.h');
$state = json_encode($this->editor, JSON_THROW_ON_ERROR);
$state = json_encode($editor, JSON_THROW_ON_ERROR);
$this->assertMatchesJsonSnapshot($state); $this->assertMatchesJsonSnapshot($state);
} }
} }

View File

@ -3,6 +3,12 @@
"x": 0, "x": 0,
"y": 0 "y": 0
}, },
"document": {
"syntax": null,
"tokens": [],
"rows": [],
"filename": null
},
"offset": { "offset": {
"x": 0, "x": 0,
"y": 0 "y": 0
@ -11,9 +17,14 @@
"filename": "", "filename": "",
"renderX": 0, "renderX": 0,
"rows": [], "rows": [],
"screenCols": 80, "terminalSize": {
"screenRows": 23, "rows": 21,
"statusMsg": "", "cols": 80
},
"statusMessage": {
"text": "HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find",
"time": 1234567890
},
"syntax": null, "syntax": null,
"tokens": [] "tokens": []
} }