aboutsummaryrefslogtreecommitdiff
path: root/test/shared/settings
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-10-07 12:54:32 +0000
committerGitHub <noreply@github.com>2019-10-07 12:54:32 +0000
commit8eddcc1785a85bbe74be254d1055ebe5125dad10 (patch)
treef3f51320d12a90a1b421ed8b1f811c576996ea8e /test/shared/settings
parent7fc2bb615f530fc6adfade54b9553568f5d50ceb (diff)
parentb77a4734985722e96066e713f3b1b9e81a6e1811 (diff)
Merge pull request #654 from ueokande/settings-as-a-class
Refactor settings on shared logics
Diffstat (limited to 'test/shared/settings')
-rw-r--r--test/shared/settings/Blacklist.test.ts77
-rw-r--r--test/shared/settings/Key.test.ts92
-rw-r--r--test/shared/settings/KeySequence.test.ts72
-rw-r--r--test/shared/settings/Keymaps.test.ts66
-rw-r--r--test/shared/settings/Properties.test.ts30
-rw-r--r--test/shared/settings/Search.test.ts68
-rw-r--r--test/shared/settings/Settings.test.ts54
7 files changed, 459 insertions, 0 deletions
diff --git a/test/shared/settings/Blacklist.test.ts b/test/shared/settings/Blacklist.test.ts
new file mode 100644
index 0000000..fbacf5d
--- /dev/null
+++ b/test/shared/settings/Blacklist.test.ts
@@ -0,0 +1,77 @@
+import Blacklist from '../../../src/shared/settings/Blacklist';
+import { expect } from 'chai';
+
+describe('Blacklist', () => {
+ describe('fromJSON', () => {
+ it('returns empty array by empty settings', () => {
+ let blacklist = Blacklist.fromJSON([]);
+ expect(blacklist.toJSON()).to.be.empty;
+ });
+
+ it('returns blacklist by valid settings', () => {
+ let blacklist = Blacklist.fromJSON([
+ 'github.com',
+ 'circleci.com',
+ ]);
+
+ expect(blacklist.toJSON()).to.deep.equal([
+ 'github.com',
+ 'circleci.com',
+ ]);
+ });
+
+ it('throws a TypeError by invalid settings', () => {
+ expect(() => Blacklist.fromJSON(null)).to.throw(TypeError);
+ expect(() => Blacklist.fromJSON({})).to.throw(TypeError);
+ expect(() => Blacklist.fromJSON([1,2,3])).to.throw(TypeError);
+ });
+ });
+
+ describe('#includes', () => {
+ it('matches by *', () => {
+ let blacklist = new Blacklist(['*']);
+
+ expect(blacklist.includes('https://github.com/abc')).to.be.true;
+ });
+
+ it('matches by hostname', () => {
+ let blacklist = new Blacklist(['github.com']);
+
+ expect(blacklist.includes('https://github.com')).to.be.true;
+ expect(blacklist.includes('https://gist.github.com')).to.be.false;
+ expect(blacklist.includes('https://github.com/ueokande')).to.be.true;
+ expect(blacklist.includes('https://github.org')).to.be.false;
+ expect(blacklist.includes('https://google.com/search?q=github.org')).to.be.false;
+ });
+
+ it('matches by hostname with wildcard', () => {
+ let blacklist = new Blacklist(['*.github.com']);
+
+ expect(blacklist.includes('https://github.com')).to.be.false;
+ expect(blacklist.includes('https://gist.github.com')).to.be.true;
+ })
+
+ it('matches by path', () => {
+ let blacklist = new Blacklist(['github.com/abc']);
+
+ expect(blacklist.includes('https://github.com/abc')).to.be.true;
+ expect(blacklist.includes('https://github.com/abcdef')).to.be.false;
+ expect(blacklist.includes('https://gist.github.com/abc')).to.be.false;
+ })
+
+ it('matches by path with wildcard', () => {
+ let blacklist = new Blacklist(['github.com/abc*']);
+
+ expect(blacklist.includes('https://github.com/abc')).to.be.true;
+ expect(blacklist.includes('https://github.com/abcdef')).to.be.true;
+ expect(blacklist.includes('https://gist.github.com/abc')).to.be.false;
+ })
+
+ it('matches address and port', () => {
+ let blacklist = new Blacklist(['127.0.0.1:8888']);
+
+ expect(blacklist.includes('http://127.0.0.1:8888/')).to.be.true;
+ expect(blacklist.includes('http://127.0.0.1:8888/hello')).to.be.true;
+ })
+ })
+});
diff --git a/test/shared/settings/Key.test.ts b/test/shared/settings/Key.test.ts
new file mode 100644
index 0000000..8222d5a
--- /dev/null
+++ b/test/shared/settings/Key.test.ts
@@ -0,0 +1,92 @@
+import { expect } from 'chai'
+import Key from '../../../src/shared/settings/Key';
+
+describe("Key", () => {
+ describe('fromMapKey', () => {
+ it('return for X', () => {
+ let key = Key.fromMapKey('x');
+ expect(key.key).to.equal('x');
+ 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 = Key.fromMapKey('X');
+ expect(key.key).to.equal('X');
+ 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 = Key.fromMapKey('<C-X>');
+ expect(key.key).to.equal('x');
+ 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 = Key.fromMapKey('<C-M-X>');
+ expect(key.key).to.equal('x');
+ 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 = Key.fromMapKey('<C-S-x>');
+ expect(key.key).to.equal('X');
+ 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 = Key.fromMapKey('<S-Esc>');
+ expect(key.key).to.equal('Esc');
+ 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 = Key.fromMapKey('<C-Esc>');
+ expect(key.key).to.equal('Esc');
+ 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 = Key.fromMapKey('<C-Space>');
+ expect(key.key).to.equal('Space');
+ 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(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(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/shared/settings/KeySequence.test.ts b/test/shared/settings/KeySequence.test.ts
new file mode 100644
index 0000000..361cbd1
--- /dev/null
+++ b/test/shared/settings/KeySequence.test.ts
@@ -0,0 +1,72 @@
+import KeySequence from '../../../src/shared/settings/KeySequence';
+import { expect } from 'chai'
+import Key from "../../../src/shared/settings/Key";
+
+describe("KeySequence", () => {
+ describe('#push', () => {
+ it('append a key to the sequence', () => {
+ let seq = new KeySequence([]);
+ seq.push(Key.fromMapKey('g'));
+ seq.push(Key.fromMapKey('<S-U>'));
+
+ expect(seq.keys[0].key).to.equal('g');
+ expect(seq.keys[1].key).to.equal('U');
+ expect(seq.keys[1].shift).to.be.true;
+ })
+ });
+
+ describe('#startsWith', () => {
+ it('returns true if the key sequence starts with param', () => {
+ let seq = new KeySequence([
+ Key.fromMapKey('g'),
+ Key.fromMapKey('<S-U>'),
+ ]);
+
+ expect(seq.startsWith(new KeySequence([
+ ]))).to.be.true;
+ expect(seq.startsWith(new KeySequence([
+ Key.fromMapKey('g'),
+ ]))).to.be.true;
+ expect(seq.startsWith(new KeySequence([
+ Key.fromMapKey('g'), Key.fromMapKey('<S-U>'),
+ ]))).to.be.true;
+ expect(seq.startsWith(new KeySequence([
+ Key.fromMapKey('g'), Key.fromMapKey('<S-U>'), Key.fromMapKey('x'),
+ ]))).to.be.false;
+ expect(seq.startsWith(new KeySequence([
+ Key.fromMapKey('h'),
+ ]))).to.be.false;
+ });
+
+ it('returns true if the empty sequence starts with an empty sequence', () => {
+ let seq = new KeySequence([]);
+
+ expect(seq.startsWith(new KeySequence([]))).to.be.true;
+ expect(seq.startsWith(new KeySequence([
+ Key.fromMapKey('h'),
+ ]))).to.be.false;
+ })
+ });
+
+ describe('#fromMapKeys', () => {
+ it('returns mapped keys for Shift+Esc', () => {
+ let keys = KeySequence.fromMapKeys('<S-Esc>').keys;
+ expect(keys).to.have.lengthOf(1);
+ expect(keys[0].key).to.equal('Esc');
+ expect(keys[0].shift).to.be.true;
+ });
+
+ it('returns mapped keys for a<C-B><A-C>d<M-e>', () => {
+ let keys = KeySequence.fromMapKeys('a<C-B><A-C>d<M-e>').keys;
+ expect(keys).to.have.lengthOf(5);
+ expect(keys[0].key).to.equal('a');
+ expect(keys[1].ctrl).to.be.true;
+ expect(keys[1].key).to.equal('b');
+ expect(keys[2].alt).to.be.true;
+ expect(keys[2].key).to.equal('c');
+ expect(keys[3].key).to.equal('d');
+ expect(keys[4].meta).to.be.true;
+ expect(keys[4].key).to.equal('e');
+ });
+ })
+});
diff --git a/test/shared/settings/Keymaps.test.ts b/test/shared/settings/Keymaps.test.ts
new file mode 100644
index 0000000..7896a63
--- /dev/null
+++ b/test/shared/settings/Keymaps.test.ts
@@ -0,0 +1,66 @@
+import Keymaps from '../../../src/shared/settings/Keymaps';
+import { expect } from 'chai';
+
+describe('Keymaps', () => {
+ describe('#valueOf', () => {
+ it('returns empty object by empty settings', () => {
+ let keymaps = Keymaps.fromJSON({}).toJSON();
+ expect(keymaps).to.be.empty;
+ });
+
+ it('returns keymaps by valid settings', () => {
+ let keymaps = Keymaps.fromJSON({
+ k: { type: "scroll.vertically", count: -1 },
+ j: { type: "scroll.vertically", count: 1 },
+ }).toJSON();
+
+ expect(keymaps['k']).to.deep.equal({ type: "scroll.vertically", count: -1 });
+ expect(keymaps['j']).to.deep.equal({ type: "scroll.vertically", count: 1 });
+ });
+
+ it('throws a TypeError by invalid settings', () => {
+ expect(() => Keymaps.fromJSON(null)).to.throw(TypeError);
+ expect(() => Keymaps.fromJSON({
+ k: { type: "invalid.operation" },
+ })).to.throw(TypeError);
+ });
+ });
+
+ describe('#combine', () => {
+ it('returns combined keymaps', () => {
+ let keymaps = Keymaps.fromJSON({
+ k: { type: "scroll.vertically", count: -1 },
+ j: { type: "scroll.vertically", count: 1 },
+ }).combine(Keymaps.fromJSON({
+ n: { type: "find.next" },
+ N: { type: "find.prev" },
+ }));
+
+ let entries = keymaps.entries().sort(([name1], [name2]) => name1.localeCompare(name2));
+ expect(entries).deep.equals([
+ ['j', { type: "scroll.vertically", count: 1 }],
+ ['k', { type: "scroll.vertically", count: -1 }],
+ ['n', { type: "find.next" }],
+ ['N', { type: "find.prev" }],
+ ]);
+ });
+
+ it('overrides current keymaps', () => {
+ let keymaps = Keymaps.fromJSON({
+ k: { type: "scroll.vertically", count: -1 },
+ j: { type: "scroll.vertically", count: 1 },
+ }).combine(Keymaps.fromJSON({
+ n: { type: "find.next" },
+ j: { type: "find.prev" },
+ }));
+
+ let entries = keymaps.entries().sort(([name1], [name2]) => name1.localeCompare(name2));
+ expect(entries).deep.equals([
+ ['j', { type: "find.prev" }],
+ ['k', { type: "scroll.vertically", count: -1 }],
+ ['n', { type: "find.next" }],
+ ]);
+ });
+ });
+});
+
diff --git a/test/shared/settings/Properties.test.ts b/test/shared/settings/Properties.test.ts
new file mode 100644
index 0000000..609a565
--- /dev/null
+++ b/test/shared/settings/Properties.test.ts
@@ -0,0 +1,30 @@
+import Properties from '../../../src/shared/settings/Properties';
+import { expect } from 'chai';
+
+describe('Properties', () => {
+ describe('#propertiesValueOf', () => {
+ it('returns with default properties by empty settings', () => {
+ let props = Properties.fromJSON({});
+ expect(props).to.deep.equal({
+ hintchars: "abcdefghijklmnopqrstuvwxyz",
+ smoothscroll: false,
+ complete: "sbh"
+ })
+ });
+
+ it('returns properties by valid settings', () => {
+ let props = Properties.fromJSON({
+ hintchars: "abcdefgh",
+ smoothscroll: false,
+ complete: "sbh"
+ });
+
+ expect(props).to.deep.equal({
+ hintchars: "abcdefgh",
+ smoothscroll: false,
+ complete: "sbh"
+ });
+ });
+ });
+});
+
diff --git a/test/shared/settings/Search.test.ts b/test/shared/settings/Search.test.ts
new file mode 100644
index 0000000..7c9134d
--- /dev/null
+++ b/test/shared/settings/Search.test.ts
@@ -0,0 +1,68 @@
+import Search from '../../../src/shared/settings/Search';
+import { expect } from 'chai';
+
+describe('Search', () => {
+ it('returns search settings by valid settings', () => {
+ let search = Search.fromJSON({
+ default: 'google',
+ engines: {
+ 'google': 'https://google.com/search?q={}',
+ 'yahoo': 'https://search.yahoo.com/search?p={}',
+ }
+ });
+
+ expect(search.defaultEngine).to.equal('google')
+ expect(search.engines).to.deep.equals({
+ 'google': 'https://google.com/search?q={}',
+ 'yahoo': 'https://search.yahoo.com/search?p={}',
+ });
+ expect(search.toJSON()).to.deep.equal({
+ default: 'google',
+ engines: {
+ 'google': 'https://google.com/search?q={}',
+ 'yahoo': 'https://search.yahoo.com/search?p={}',
+ }
+ });
+ });
+
+ it('throws a TypeError by invalid settings', () => {
+ expect(() => Search.fromJSON(null)).to.throw(TypeError);
+ expect(() => Search.fromJSON({})).to.throw(TypeError);
+ expect(() => Search.fromJSON([])).to.throw(TypeError);
+ expect(() => Search.fromJSON({
+ default: 123,
+ engines: {}
+ })).to.throw(TypeError);
+ expect(() => Search.fromJSON({
+ default: 'google',
+ engines: {
+ 'google': 123456,
+ }
+ })).to.throw(TypeError);
+ expect(() => Search.fromJSON({
+ default: 'wikipedia',
+ engines: {
+ 'google': 'https://google.com/search?q={}',
+ 'yahoo': 'https://search.yahoo.com/search?p={}',
+ }
+ })).to.throw(TypeError);
+ expect(() => Search.fromJSON({
+ default: 'g o o g l e',
+ engines: {
+ 'g o o g l e': 'https://google.com/search?q={}',
+ }
+ })).to.throw(TypeError);
+ expect(() => Search.fromJSON({
+ default: 'google',
+ engines: {
+ 'google': 'https://google.com/search',
+ }
+ })).to.throw(TypeError);
+ expect(() => Search.fromJSON({
+ default: 'google',
+ engines: {
+ 'google': 'https://google.com/search?q={}&r={}',
+ }
+ })).to.throw(TypeError);
+ });
+});
diff --git a/test/shared/settings/Settings.test.ts b/test/shared/settings/Settings.test.ts
new file mode 100644
index 0000000..ab6af04
--- /dev/null
+++ b/test/shared/settings/Settings.test.ts
@@ -0,0 +1,54 @@
+import Settings from '../../../src/shared/settings/Settings';
+import { expect } from 'chai';
+
+describe('Settings', () => {
+ describe('#valueOf', () => {
+ it('returns settings by valid settings', () => {
+ let x = Settings.fromJSON({
+ keymaps: {},
+ "search": {
+ "default": "google",
+ "engines": {
+ "google": "https://google.com/search?q={}",
+ }
+ },
+ "properties": {},
+ "blacklist": []
+ });
+
+ expect({
+ keymaps: x.keymaps.toJSON(),
+ search: x.search.toJSON(),
+ properties: x.properties.toJSON(),
+ blacklist: x.blacklist.toJSON(),
+ }).to.deep.equal({
+ keymaps: {},
+ search: {
+ default: "google",
+ engines: {
+ google: "https://google.com/search?q={}",
+ }
+ },
+ properties: {
+ hintchars: "abcdefghijklmnopqrstuvwxyz",
+ smoothscroll: false,
+ complete: "sbh"
+ },
+ blacklist: []
+ });
+ });
+
+ it('sets default settings', () => {
+ let value = Settings.fromJSON({});
+ expect(value.keymaps.toJSON()).to.not.be.empty;
+ expect(value.properties.toJSON()).to.not.be.empty;
+ expect(value.search.defaultEngine).to.be.a('string');
+ expect(value.search.engines).to.be.an('object');
+ expect(value.blacklist.toJSON()).to.be.empty;
+ });
+
+ it('throws a TypeError with an unknown field', () => {
+ expect(() => Settings.fromJSON({ name: 'alice' })).to.throw(TypeError)
+ });
+ });
+});