aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-18 21:43:56 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-05-18 21:43:56 +0900
commita5518dce3d101cb1cb65724b82079f66f20c80c8 (patch)
tree79537b86e4a7bf231e8801c9c6bf2aeb94450343 /test
parent2ec912c262b51fe9523ebf74d5062d0b9bbdab71 (diff)
Define Key and KeySequence
Diffstat (limited to 'test')
-rw-r--r--test/content/domains/Key.test.ts (renamed from test/shared/utils/keys.test.ts)69
-rw-r--r--test/content/domains/KeySequence.test.ts72
2 files changed, 93 insertions, 48 deletions
diff --git a/test/shared/utils/keys.test.ts b/test/content/domains/Key.test.ts
index b2ad3cb..b3f9fb6 100644
--- a/test/shared/utils/keys.test.ts
+++ b/test/content/domains/Key.test.ts
@@ -1,11 +1,12 @@
-import * as keys from 'shared/utils/keys';
+import Key, * as keys from '../../../src/content/domains/Key';
+import { expect } from 'chai'
-describe("keys util", () => {
+describe("Key", () => {
describe('fromKeyboardEvent', () => {
it('returns from keyboard input Ctrl+X', () => {
- let k = keys.fromKeyboardEvent({
- key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true
- });
+ let k = keys.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;
@@ -14,9 +15,9 @@ describe("keys util", () => {
});
it('returns from keyboard input Shift+Esc', () => {
- let k = keys.fromKeyboardEvent({
+ let k = keys.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;
@@ -26,9 +27,9 @@ describe("keys util", () => {
it('returns from keyboard input Ctrl+$', () => {
// $ required shift pressing on most keyboards
- let k = keys.fromKeyboardEvent({
+ let k = keys.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;
@@ -37,9 +38,9 @@ describe("keys util", () => {
});
it('returns from keyboard input Crtl+Space', () => {
- let k = keys.fromKeyboardEvent({
+ let k = keys.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;
@@ -122,43 +123,15 @@ describe("keys util", () => {
});
});
- describe('fromMapKeys', () => {
- it('returns mapped keys for Shift+Esc', () => {
- let keyArray = keys.fromMapKeys('<S-Esc>');
- expect(keyArray).to.have.lengthOf(1);
- expect(keyArray[0].key).to.equal('Esc');
- expect(keyArray[0].shiftKey).to.be.true;
- });
-
- it('returns mapped keys for a<C-B><A-C>d<M-e>', () => {
- let keyArray = keys.fromMapKeys('a<C-B><A-C>d<M-e>');
- expect(keyArray).to.have.lengthOf(5);
- expect(keyArray[0].key).to.equal('a');
- expect(keyArray[1].ctrlKey).to.be.true;
- expect(keyArray[1].key).to.equal('b');
- expect(keyArray[2].altKey).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].key).to.equal('e');
- });
- })
-
describe('equals', () => {
- expect(keys.equals({
- key: 'x',
- ctrlKey: true,
- }, {
- key: 'x',
- ctrlKey: true,
- })).to.be.true;
-
- expect(keys.equals({
- key: 'X',
- shiftKey: true,
- }, {
- key: 'x',
- ctrlKey: true,
- })).to.be.false;
+ expect(keys.equals(
+ { key: 'x', ctrlKey: true, },
+ { key: 'x', ctrlKey: true, },
+ )).to.be.true;
+
+ expect(keys.equals(
+ { key: 'X', shiftKey: true, },
+ { key: 'x', ctrlKey: true, },
+ )).to.be.false;
});
});
diff --git a/test/content/domains/KeySequence.test.ts b/test/content/domains/KeySequence.test.ts
new file mode 100644
index 0000000..7387c06
--- /dev/null
+++ b/test/content/domains/KeySequence.test.ts
@@ -0,0 +1,72 @@
+import KeySequence, * as utils from '../../../src/content/domains/KeySequence';
+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 });
+
+ let array = seq.getKeyArray();
+ expect(array[0]).to.deep.equal({ key: 'g' });
+ expect(array[1]).to.deep.equal({ key: 'u', shiftKey: true });
+ })
+ });
+
+ describe('#startsWith', () => {
+ it('returns true if the key sequence starts with param', () => {
+ let seq = KeySequence.from([
+ { key: 'g' },
+ { key: 'u', shiftKey: true },
+ ]);
+
+ expect(seq.startsWith(KeySequence.from([
+ ]))).to.be.true;
+ expect(seq.startsWith(KeySequence.from([
+ { key: 'g' },
+ ]))).to.be.true;
+ expect(seq.startsWith(KeySequence.from([
+ { key: 'g' }, { key: 'u', shiftKey: true },
+ ]))).to.be.true;
+ expect(seq.startsWith(KeySequence.from([
+ { key: 'g' }, { key: 'u', shiftKey: true }, { key: 'x' },
+ ]))).to.be.false;
+ expect(seq.startsWith(KeySequence.from([
+ { key: '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' },
+ ]))).to.be.false;
+ })
+ });
+
+ describe('#fromMapKeys', () => {
+ it('returns mapped keys for Shift+Esc', () => {
+ 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;
+ });
+
+ 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].key).to.equal('b');
+ expect(keyArray[2].altKey).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].key).to.equal('e');
+ });
+ })
+
+});