IT
games/alis / tests / EditorTest.php · commit 9c722cf
<?phpnamespace Tests;use App\Alis\SysVars;use App\Editor\Highlighter;use App\Editor\SourceBuffer;use App\Editor\SourceStore;use App\Scene\EditorScene;use App\Scene\PlayScene;use JumPHP\Testing\GameTestCase;/** The ALIS editor (docs/editor.md): edit a script, compile it, play the game with it. */final class EditorTest extends GameTestCase{    private function needSources(): void    {        $root = dirname(__DIR__);        if (!is_dir("$root/data/robinson") || !is_file("$root/../../.alis-data/robinson/src/main.als")) {            self::markTestSkipped('the data and the ALIS sources of Robinson\'s Requiem are not here (private files)');        }    }    private function key(int $sym, string $text = ''): void    {        $kb = $this->game->peek('engine', $this->game)->input->getKeyboard();        $kb->down($sym);        if ($text !== '') {            $kb->addText($text);        }        $this->game->run(1);        $kb->up($sym);        $this->game->run(1);    }    /** A click (or a tap) at view pixels $x, $y. */    private function click(float $x, float $y): void    {        $mouse = $this->game->peek('engine', $this->game)->input->getMouse();        $mouse->move($x, $y);        $mouse->down(1);        $this->game->run(1);        $mouse->up(1);        $this->game->run(1);    }    public function testTheBackButtonLeavesWithoutEscape(): void    {        $this->needSources();        $this->game->start(EditorScene::class, ['game' => 'robinson', 'script' => 'flags']);        $this->game->run(2);                         // drawn once: the button has its place        self::assertSame('edit', $this->game->peek('mode'));        $this->click(2, 2);        self::assertSame('list', $this->game->peek('mode'), 'the text -> the list of scripts');        // unsaved changes: the first click warns, the second one leaves        $this->game->peek('buf')->dirty = true;        $this->click(2, 2);        self::assertInstanceOf(EditorScene::class, $this->game->scene());        self::assertStringContainsString('pas enregistré', $this->game->peek('status'));        $this->click(2, 2);        $this->game->run(40);                        // the fade        self::assertInstanceOf(\App\Scene\TitleScene::class, $this->game->scene());    }    public function testTheTextIsEditedLikeInATextEditor(): void    {        $b = new SourceBuffer("L0010:\n    put 3\n    stop");        $b->moveTo(1, 9);        $b->insert(', 4');        self::assertSame('    put 3, 4', $b->lines[1]);        $b->newline();        $b->insert('goto L0010');        self::assertSame('    goto L0010', $b->lines[2]);        $b->backspace(true);        self::assertSame('    goto ', $b->lines[2]);        self::assertTrue($b->undo());        self::assertSame('    goto L0010', $b->lines[2]);        $b->moveTo(0, 0);        self::assertTrue($b->find('STOP'));        self::assertSame('stop', $b->selected());        self::assertSame(3, $b->line);        // "{" opens a level: the next line is indented once more        $b->moveTo(0, PHP_INT_MAX);        $b->insert(' if 1 {');        $b->newline();        self::assertSame('    ', $b->lines[1]);    }    public function testTheSourceIsColouredByWhatEachWordIs(): void    {        $spans = (new Highlighter())->line('L0049:    putnat 108, 0, vb[0x52] // the flag');        $kinds = array_column($spans, 1, 0);        self::assertSame('label', $kinds['L0049:']);        self::assertSame('keyword', $kinds['putnat']);        self::assertSame('number', $kinds['108']);        self::assertSame('variable', $kinds['vb']);        self::assertSame('comment', $kinds['// the flag']);    }    public function testTheEditorOpensAScriptOfTheGame(): void    {        $this->needSources();        $this->game->start(EditorScene::class, ['game' => 'robinson']);        $scripts = $this->game->peek('scripts');        self::assertContains('main', $scripts);        self::assertContains('flags', $scripts);        self::assertSame('main', $scripts[0]);        // type "fl" then Return: the flags screen's script        $this->key(\SDLK_f, 'f');        $this->key(\SDLK_l, 'l');        $this->key(\SDLK_RETURN);        self::assertSame('edit', $this->game->peek('mode'));        self::assertSame('flags', $this->game->peek('script'));        // shown in the ALIS 90 form (App\Alis\Lang\Human): its header, its declared variables        self::assertStringContainsString('module "flags"', $this->game->scene()->buffer()->text());        self::assertStringContainsString('fin_variables', $this->game->scene()->buffer()->text());        $this->game->run(2);        $this->game->shot('editor-flags');    }    public function testAMistakeShowsItsLineAndIsNotSaved(): void    {        $this->needSources();        $this->game->start(EditorScene::class, ['game' => 'robinson', 'script' => 'flags']);        $editor = $this->game->scene();        $b = $editor->buffer();        $b->moveTo(12, 0);        $b->insert("    frobnicate 1, 2\n", 'other');        self::assertFalse($editor->save());        self::assertSame(12, $this->game->peek('errorLine'));        self::assertSame('bad', $this->game->peek('statusKind'));        self::assertFalse((new SourceStore(SysVars::$storage, 'robinson', ''))->isChanged('flags'));        $this->game->run(2);        $this->game->shot('editor-error');    }    public function testAChangedScriptChangesTheGame(): void    {        $this->needSources();        // the flags screen (after the Silmarils film), with the original script        $this->game->start(PlayScene::class, ['game' => 'robinson', 'edition' => 'source']);        $this->game->run(self::FLAGS);        $before = md5(SysVars::$frameBmp);        $this->game->shot('editor-flags-before');        // the first flag 40 pixels lower        $this->game->start(EditorScene::class, ['game' => 'robinson', 'script' => 'flags']);        $editor = $this->game->scene();        $b = $editor->buffer();        self::assertTrue($b->find('place_naturel 108, 0, 137'));        $b->insert('place_naturel 108, 0, 177');        self::assertTrue($editor->save());        self::assertTrue((new SourceStore(SysVars::$storage, 'robinson', ''))->isChanged('flags'));        // F5: the game with it        $this->key(\SDLK_F5);        $this->game->run(60);      // the fade        self::assertInstanceOf(PlayScene::class, $this->game->scene());        $this->game->start(PlayScene::class, ['game' => 'robinson', 'edition' => 'source', 'back' => 'editor', 'script' => 'flags']);        $this->game->run(self::FLAGS);        self::assertNull($this->game->peek('machine')->error);        self::assertNotSame($before, md5(SysVars::$frameBmp), 'the changed script is what runs');        $this->game->shot('editor-flags-after');        // back to the editor (F10), then back to the original script (Ctrl+R)        $this->key(\SDLK_F10);        $this->game->run(60);        self::assertInstanceOf(EditorScene::class, $this->game->scene());        self::assertSame('flags', $this->game->peek('script'));        $kb = $this->game->peek('engine', $this->game)->input->getKeyboard();        $kb->down(\SDLK_LCTRL);        $this->key(\SDLK_r);        $kb->up(\SDLK_LCTRL);        self::assertFalse((new SourceStore(SysVars::$storage, 'robinson', ''))->isChanged('flags'));    }    /** frames from the start of the game to its flags screen (after the Silmarils film) */    private const FLAGS = 2400;    public function testF2ShowsAlisBasicThenTheMachinesWordsAndBack(): void    {        $this->needSources();        $this->game->start(EditorScene::class, ['game' => 'robinson', 'script' => 'flags']);        $human = $this->game->scene()->buffer()->text();        $this->key(\SDLK_F2);        $basic = $this->game->scene()->buffer()->text();        self::assertStringContainsString('ACTOR "flags"', $basic);        self::assertStringContainsString('PUTNAT 108, 0, 137, 5 + vague|, 1', $basic);        $this->game->shot('editor-basic');        $this->key(\SDLK_F2);        $canonical = $this->game->scene()->buffer()->text();        self::assertStringContainsString('script "flags"', $canonical);        self::assertStringContainsString('putnat 108, 0, 137, 5 + vb[0x52], 1', $canonical);        $this->key(\SDLK_F2);        self::assertSame($human, $this->game->scene()->buffer()->text());        $this->game->shot('editor-90');    }}