{"size": 2511, "ext": "js", "max_stars_repo_path": "test/unit/io_keyboard.js", "max_stars_repo_name": "Cube-Enix/sn-edit-vm", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/unit/io_keyboard.js", "max_issues_repo_name": "Cube-Enix/sn-edit-vm", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/unit/io_keyboard.js", "max_forks_repo_name": "Cube-Enix/sn-edit-vm", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.6886792453, "max_line_length": 84, "alphanum_fraction": 0.5694942254}
const test = require('tap').test;
const Keyboard = require('../../src/io/keyboard');
const Runtime = require('../../src/engine/runtime');

test('spec', t => {
    const rt = new Runtime();
    const k = new Keyboard(rt);

    t.type(k, 'object');
    t.type(k.postData, 'function');
    t.type(k.getKeyIsDown, 'function');
    t.end();
});

test('space key', t => {
    const rt = new Runtime();
    const k = new Keyboard(rt);

    k.postData({
        key: ' ',
        isDown: true
    });
    t.strictDeepEquals(k._keysPressed, ['space']);
    t.strictEquals(k.getKeyIsDown('space'), true);
    t.strictEquals(k.getKeyIsDown('any'), true);
    t.end();
});

test('letter key', t => {
    const rt = new Runtime();
    const k = new Keyboard(rt);

    k.postData({
        key: 'a',
        isDown: true
    });
    t.strictDeepEquals(k._keysPressed, ['A']);
    t.strictEquals(k.getKeyIsDown(65), true);
    t.strictEquals(k.getKeyIsDown('a'), true);
    t.strictEquals(k.getKeyIsDown('A'), true);
    t.strictEquals(k.getKeyIsDown('any'), true);
    t.end();
});

test('number key', t => {
    const rt = new Runtime();
    const k = new Keyboard(rt);

    k.postData({
        key: '1',
        isDown: true
    });
    t.strictDeepEquals(k._keysPressed, ['1']);
    t.strictEquals(k.getKeyIsDown(49), true);
    t.strictEquals(k.getKeyIsDown('1'), true);
    t.strictEquals(k.getKeyIsDown('any'), true);
    t.end();
});

test('non-english key', t => {
    const rt = new Runtime();
    const k = new Keyboard(rt);

    k.postData({
        key: '日',
        isDown: true
    });
    t.strictDeepEquals(k._keysPressed, ['日']);
    t.strictEquals(k.getKeyIsDown('日'), true);
    t.strictEquals(k.getKeyIsDown('any'), true);
    t.end();
});

/* TW: This test is disabled because we intentionally add support for modifier keys.
test('ignore modifier key', t => {
    const rt = new Runtime();
    const k = new Keyboard(rt);

    k.postData({
        key: 'Shift',
        isDown: true
    });
    t.strictDeepEquals(k._keysPressed, []);
    t.strictEquals(k.getKeyIsDown('any'), false);
    t.end();
});
*/

test('keyup', t => {
    const rt = new Runtime();
    const k = new Keyboard(rt);

    k.postData({
        key: 'ArrowLeft',
        isDown: true
    });
    k.postData({
        key: 'ArrowLeft',
        isDown: false
    });
    t.strictDeepEquals(k._keysPressed, []);
    t.strictEquals(k.getKeyIsDown('left arrow'), false);
    t.strictEquals(k.getKeyIsDown('any'), false);
    t.end();
});
