<?php declare(strict_types=1);

namespace Aviat\Kilo\Tests\Traits;

use Aviat\Kilo\Editor;
use Aviat\Kilo\Type\TerminalSize;
use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots;

class MockEditor extends Editor {
	public static function mock(string $filename = ''): Editor
	{
		$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->$name = $value;
		}
	}
}

class EditorTest extends TestCase {
	use MatchesSnapshots;

	public function testSanity(): void
	{
		$editor = MockEditor::mock();

		$this->assertEquals(0, $editor->numRows);
		$this->assertNull($editor->syntax);
	}

	public function test__debugInfo(): void
	{
		$editor = MockEditor::mock();

		$state = json_encode($editor->__debugInfo(), JSON_THROW_ON_ERROR);
		$this->assertMatchesJsonSnapshot($state);
	}

	public function testOpenPHP(): void
	{
		$editor = MockEditor::mock('test.php');

		$state = json_encode($editor, JSON_THROW_ON_ERROR);
		$this->assertMatchesJsonSnapshot($state);
	}

	public function testOpen(): void
	{
		$editor = MockEditor::mock('src/ffi.h');

		$state = json_encode($editor, JSON_THROW_ON_ERROR);
		$this->assertMatchesJsonSnapshot($state);
	}
}