diff options
author | Shin'ya UEOKA <ueokande@i-beam.org> | 2019-10-03 12:15:12 +0000 |
---|---|---|
committer | Shin'ya UEOKA <ueokande@i-beam.org> | 2019-10-06 12:58:59 +0000 |
commit | 62a86c525378610444a9976dd4409ea207174d20 (patch) | |
tree | cad0928f7cafc0eb95e60bb2814a6d7b350a1a0b /test/content/domains | |
parent | 620d4bc03e11ae88e2162cb4acdf88b6bded50e5 (diff) |
Make key class
Diffstat (limited to 'test/content/domains')
-rw-r--r-- | test/content/domains/Key.test.ts | 140 | ||||
-rw-r--r-- | test/content/domains/KeySequence.test.ts | 35 |
2 files changed, 89 insertions, 86 deletions
diff --git a/test/content/domains/Key.test.ts b/test/content/domains/Key.test.ts index b3f9fb6..8e62f80 100644 --- a/test/content/domains/Key.test.ts +++ b/test/content/domains/Key.test.ts @@ -1,137 +1,139 @@ -import Key, * as keys from '../../../src/content/domains/Key'; +import Key from '../../../src/content/domains/Key'; import { expect } from 'chai' describe("Key", () => { describe('fromKeyboardEvent', () => { it('returns from keyboard input Ctrl+X', () => { - let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', { + let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', { key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true, })); expect(k.key).to.equal('x'); - expect(k.shiftKey).to.be.false; - expect(k.ctrlKey).to.be.true; - expect(k.altKey).to.be.false; - expect(k.metaKey).to.be.true; + expect(k.shift).to.be.false; + expect(k.ctrl).to.be.true; + expect(k.alt).to.be.false; + expect(k.meta).to.be.true; }); it('returns from keyboard input Shift+Esc', () => { - let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', { + let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', { key: 'Escape', shiftKey: true, ctrlKey: false, altKey: false, metaKey: true })); expect(k.key).to.equal('Esc'); - expect(k.shiftKey).to.be.true; - expect(k.ctrlKey).to.be.false; - expect(k.altKey).to.be.false; - expect(k.metaKey).to.be.true; + expect(k.shift).to.be.true; + expect(k.ctrl).to.be.false; + expect(k.alt).to.be.false; + expect(k.meta).to.be.true; }); it('returns from keyboard input Ctrl+$', () => { // $ required shift pressing on most keyboards - let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', { + let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', { key: '$', shiftKey: true, ctrlKey: true, altKey: false, metaKey: false })); expect(k.key).to.equal('$'); - expect(k.shiftKey).to.be.false; - expect(k.ctrlKey).to.be.true; - expect(k.altKey).to.be.false; - expect(k.metaKey).to.be.false; + expect(k.shift).to.be.false; + expect(k.ctrl).to.be.true; + expect(k.alt).to.be.false; + expect(k.meta).to.be.false; }); it('returns from keyboard input Crtl+Space', () => { - let k = keys.fromKeyboardEvent(new KeyboardEvent('keydown', { + let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', { key: ' ', shiftKey: false, ctrlKey: true, altKey: false, metaKey: false })); expect(k.key).to.equal('Space'); - expect(k.shiftKey).to.be.false; - expect(k.ctrlKey).to.be.true; - expect(k.altKey).to.be.false; - expect(k.metaKey).to.be.false; + expect(k.shift).to.be.false; + expect(k.ctrl).to.be.true; + expect(k.alt).to.be.false; + expect(k.meta).to.be.false; }); }); describe('fromMapKey', () => { it('return for X', () => { - let key = keys.fromMapKey('x'); + let key = Key.fromMapKey('x'); expect(key.key).to.equal('x'); - expect(key.shiftKey).to.be.false; - expect(key.ctrlKey).to.be.false; - expect(key.altKey).to.be.false; - expect(key.metaKey).to.be.false; + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.false; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; }); it('return for Shift+X', () => { - let key = keys.fromMapKey('X'); + let key = Key.fromMapKey('X'); expect(key.key).to.equal('X'); - expect(key.shiftKey).to.be.true; - expect(key.ctrlKey).to.be.false; - expect(key.altKey).to.be.false; - expect(key.metaKey).to.be.false; + expect(key.shift).to.be.true; + expect(key.ctrl).to.be.false; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; }); it('return for Ctrl+X', () => { - let key = keys.fromMapKey('<C-X>'); + let key = Key.fromMapKey('<C-X>'); expect(key.key).to.equal('x'); - expect(key.shiftKey).to.be.false; - expect(key.ctrlKey).to.be.true; - expect(key.altKey).to.be.false; - expect(key.metaKey).to.be.false; + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; }); it('returns for Ctrl+Meta+X', () => { - let key = keys.fromMapKey('<C-M-X>'); + let key = Key.fromMapKey('<C-M-X>'); expect(key.key).to.equal('x'); - expect(key.shiftKey).to.be.false; - expect(key.ctrlKey).to.be.true; - expect(key.altKey).to.be.false; - expect(key.metaKey).to.be.true; + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.true; }); it('returns for Ctrl+Shift+x', () => { - let key = keys.fromMapKey('<C-S-x>'); + let key = Key.fromMapKey('<C-S-x>'); expect(key.key).to.equal('X'); - expect(key.shiftKey).to.be.true; - expect(key.ctrlKey).to.be.true; - expect(key.altKey).to.be.false; - expect(key.metaKey).to.be.false; + expect(key.shift).to.be.true; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; }); it('returns for Shift+Esc', () => { - let key = keys.fromMapKey('<S-Esc>'); + let key = Key.fromMapKey('<S-Esc>'); expect(key.key).to.equal('Esc'); - expect(key.shiftKey).to.be.true; - expect(key.ctrlKey).to.be.false; - expect(key.altKey).to.be.false; - expect(key.metaKey).to.be.false; + expect(key.shift).to.be.true; + expect(key.ctrl).to.be.false; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; }); it('returns for Ctrl+Esc', () => { - let key = keys.fromMapKey('<C-Esc>'); + let key = Key.fromMapKey('<C-Esc>'); expect(key.key).to.equal('Esc'); - expect(key.shiftKey).to.be.false; - expect(key.ctrlKey).to.be.true; - expect(key.altKey).to.be.false; - expect(key.metaKey).to.be.false; + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; }); it('returns for Ctrl+Esc', () => { - let key = keys.fromMapKey('<C-Space>'); + let key = Key.fromMapKey('<C-Space>'); expect(key.key).to.equal('Space'); - expect(key.shiftKey).to.be.false; - expect(key.ctrlKey).to.be.true; - expect(key.altKey).to.be.false; - expect(key.metaKey).to.be.false; + expect(key.shift).to.be.false; + expect(key.ctrl).to.be.true; + expect(key.alt).to.be.false; + expect(key.meta).to.be.false; }); }); describe('equals', () => { - expect(keys.equals( - { key: 'x', ctrlKey: true, }, - { key: 'x', ctrlKey: true, }, - )).to.be.true; + expect(new Key({ + key: 'x', shift: false, ctrl: true, alt: false, meta: false, + }).equals(new Key({ + key: 'x', shift: false, ctrl: true, alt: false, meta: false, + }))).to.be.true; - expect(keys.equals( - { key: 'X', shiftKey: true, }, - { key: 'x', ctrlKey: true, }, - )).to.be.false; + expect(new Key({ + key: 'x', shift: false, ctrl: false, alt: false, meta: false, + }).equals(new Key({ + key: 'X', shift: true, ctrl: false, alt: false, meta: false, + }))).to.be.false; }); }); diff --git a/test/content/domains/KeySequence.test.ts b/test/content/domains/KeySequence.test.ts index 7387c06..9afc360 100644 --- a/test/content/domains/KeySequence.test.ts +++ b/test/content/domains/KeySequence.test.ts @@ -1,48 +1,50 @@ import KeySequence, * as utils from '../../../src/content/domains/KeySequence'; +import Key from '../../../src/content/domains/Key'; import { expect } from 'chai' describe("KeySequence", () => { describe('#push', () => { it('append a key to the sequence', () => { let seq = KeySequence.from([]); - seq.push({ key: 'g' }); - seq.push({ key: 'u', shiftKey: true }); + seq.push(Key.fromMapKey('g')); + seq.push(Key.fromMapKey('<S-U>')); let array = seq.getKeyArray(); - expect(array[0]).to.deep.equal({ key: 'g' }); - expect(array[1]).to.deep.equal({ key: 'u', shiftKey: true }); + expect(array[0].key).to.equal('g'); + expect(array[1].key).to.equal('U'); + expect(array[1].shift).to.be.true; }) }); describe('#startsWith', () => { it('returns true if the key sequence starts with param', () => { let seq = KeySequence.from([ - { key: 'g' }, - { key: 'u', shiftKey: true }, + Key.fromMapKey('g'), + Key.fromMapKey('<S-U>'), ]); expect(seq.startsWith(KeySequence.from([ ]))).to.be.true; expect(seq.startsWith(KeySequence.from([ - { key: 'g' }, + Key.fromMapKey('g'), ]))).to.be.true; expect(seq.startsWith(KeySequence.from([ - { key: 'g' }, { key: 'u', shiftKey: true }, + Key.fromMapKey('g'), Key.fromMapKey('<S-U>'), ]))).to.be.true; expect(seq.startsWith(KeySequence.from([ - { key: 'g' }, { key: 'u', shiftKey: true }, { key: 'x' }, + Key.fromMapKey('g'), Key.fromMapKey('<S-U>'), Key.fromMapKey('x'), ]))).to.be.false; expect(seq.startsWith(KeySequence.from([ - { key: 'h' }, + Key.fromMapKey('h'), ]))).to.be.false; - }) + }); it('returns true if the empty sequence starts with an empty sequence', () => { let seq = KeySequence.from([]); expect(seq.startsWith(KeySequence.from([]))).to.be.true; expect(seq.startsWith(KeySequence.from([ - { key: 'h' }, + Key.fromMapKey('h'), ]))).to.be.false; }) }); @@ -52,21 +54,20 @@ describe("KeySequence", () => { let keyArray = utils.fromMapKeys('<S-Esc>').getKeyArray(); expect(keyArray).to.have.lengthOf(1); expect(keyArray[0].key).to.equal('Esc'); - expect(keyArray[0].shiftKey).to.be.true; + expect(keyArray[0].shift).to.be.true; }); it('returns mapped keys for a<C-B><A-C>d<M-e>', () => { let keyArray = utils.fromMapKeys('a<C-B><A-C>d<M-e>').getKeyArray(); expect(keyArray).to.have.lengthOf(5); expect(keyArray[0].key).to.equal('a'); - expect(keyArray[1].ctrlKey).to.be.true; + expect(keyArray[1].ctrl).to.be.true; expect(keyArray[1].key).to.equal('b'); - expect(keyArray[2].altKey).to.be.true; + expect(keyArray[2].alt).to.be.true; expect(keyArray[2].key).to.equal('c'); expect(keyArray[3].key).to.equal('d'); - expect(keyArray[4].metaKey).to.be.true; + expect(keyArray[4].meta).to.be.true; expect(keyArray[4].key).to.equal('e'); }); }) - }); |