diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-10-07 12:54:32 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-07 12:54:32 +0000 |
commit | 8eddcc1785a85bbe74be254d1055ebe5125dad10 (patch) | |
tree | f3f51320d12a90a1b421ed8b1f811c576996ea8e /test/shared | |
parent | 7fc2bb615f530fc6adfade54b9553568f5d50ceb (diff) | |
parent | b77a4734985722e96066e713f3b1b9e81a6e1811 (diff) |
Merge pull request #654 from ueokande/settings-as-a-class
Refactor settings on shared logics
Diffstat (limited to 'test/shared')
-rw-r--r-- | test/shared/SettingData.test.ts | 51 | ||||
-rw-r--r-- | test/shared/Settings.test.ts | 194 | ||||
-rw-r--r-- | test/shared/blacklists.test.ts | 49 | ||||
-rw-r--r-- | test/shared/properties.test.js | 18 | ||||
-rw-r--r-- | test/shared/property-defs.test.js | 18 | ||||
-rw-r--r-- | test/shared/settings/Blacklist.test.ts | 77 | ||||
-rw-r--r-- | test/shared/settings/Key.test.ts | 92 | ||||
-rw-r--r-- | test/shared/settings/KeySequence.test.ts | 72 | ||||
-rw-r--r-- | test/shared/settings/Keymaps.test.ts | 66 | ||||
-rw-r--r-- | test/shared/settings/Properties.test.ts | 30 | ||||
-rw-r--r-- | test/shared/settings/Search.test.ts | 68 | ||||
-rw-r--r-- | test/shared/settings/Settings.test.ts | 54 | ||||
-rw-r--r-- | test/shared/urls.test.ts | 8 | ||||
-rw-r--r-- | test/shared/utils/re.test.ts | 19 |
14 files changed, 490 insertions, 326 deletions
diff --git a/test/shared/SettingData.test.ts b/test/shared/SettingData.test.ts index 8736ecb..5de7770 100644 --- a/test/shared/SettingData.test.ts +++ b/test/shared/SettingData.test.ts @@ -1,8 +1,9 @@ import SettingData, { - FormKeymaps, JSONSettings, FormSettings, + FormKeymaps, JSONTextSettings, FormSettings, } from '../../src/shared/SettingData'; -import Settings, { Keymaps } from '../../src/shared/Settings'; +import Settings from '../../src/shared/settings/Settings'; import { expect } from 'chai'; +import Keymaps from '../../src/shared/settings/Keymaps'; describe('shared/SettingData', () => { describe('FormKeymaps', () => { @@ -11,9 +12,9 @@ describe('shared/SettingData', () => { let data = { 'scroll.vertically?{"count":1}': 'j', 'scroll.home': '0', - } + }; - let keymaps = FormKeymaps.valueOf(data).toKeymaps(); + let keymaps = FormKeymaps.fromJSON(data).toKeymaps().toJSON(); expect(keymaps).to.deep.equal({ 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, @@ -23,13 +24,13 @@ describe('shared/SettingData', () => { describe('#fromKeymaps to #toJSON', () => { it('create from a Keymaps and create a JSON object', () => { - let data: Keymaps = { + let keymaps: Keymaps = Keymaps.fromJSON({ 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, - } + }); - let keymaps = FormKeymaps.fromKeymaps(data).toJSON(); - expect(keymaps).to.deep.equal({ + let form = FormKeymaps.fromKeymaps(keymaps).toJSON(); + expect(form).to.deep.equal({ 'scroll.vertically?{"count":1}': 'j', 'scroll.home': '0', }); @@ -56,14 +57,14 @@ describe('shared/SettingData', () => { "blacklist": [] }`; - let settings = JSONSettings.valueOf(o).toSettings(); - expect(settings).to.deep.equal(JSON.parse(o)); + let settings = JSONTextSettings.fromText(o).toSettings(); + expect(settings.toJSON()).to.deep.equal(JSON.parse(o)); }); }); describe('#fromSettings to #toJSON', () => { it('create from a Settings and create a JSON string', () => { - let o = { + let o = Settings.fromJSON({ keymaps: {}, search: { default: "google", @@ -77,10 +78,10 @@ describe('shared/SettingData', () => { complete: "sbh" }, blacklist: [], - }; + }); - let json = JSONSettings.fromSettings(o).toJSON(); - expect(JSON.parse(json)).to.deep.equal(o); + let json = JSONTextSettings.fromSettings(o).toJSONText(); + expect(JSON.parse(json)).to.deep.equal(o.toJSON()); }); }); }); @@ -107,8 +108,8 @@ describe('shared/SettingData', () => { blacklist: [] }; - let settings = FormSettings.valueOf(data).toSettings(); - expect(settings).to.deep.equal({ + let settings = FormSettings.fromJSON(data).toSettings(); + expect(settings.toJSON()).to.deep.equal({ keymaps: { 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, @@ -131,7 +132,7 @@ describe('shared/SettingData', () => { describe('#fromSettings to #toJSON', () => { it('create from a Settings and create a JSON string', () => { - let data: Settings = { + let data: Settings = Settings.fromJSON({ keymaps: { 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, @@ -147,8 +148,8 @@ describe('shared/SettingData', () => { smoothscroll: false, complete: "sbh" }, - blacklist: [] - }; + blacklist: [], + }); let json = FormSettings.fromSettings(data).toJSON(); expect(json).to.deep.equal({ @@ -195,7 +196,7 @@ describe('shared/SettingData', () => { }`, }; - let j = SettingData.valueOf(data).toJSON(); + let j = SettingData.fromJSON(data).toJSON(); expect(j.source).to.equal('json'); expect(j.json).to.be.a('string'); }); @@ -220,7 +221,7 @@ describe('shared/SettingData', () => { }, }; - let j = SettingData.valueOf(data).toJSON(); + let j = SettingData.fromJSON(data).toJSON(); expect(j.source).to.equal('form'); expect(j.form).to.deep.equal({ keymaps: {}, @@ -261,8 +262,8 @@ describe('shared/SettingData', () => { }`, }; - let settings = SettingData.valueOf(data).toSettings(); - expect(settings.search.default).to.equal('google'); + let settings = SettingData.fromJSON(data).toSettings(); + expect(settings.search.defaultEngine).to.equal('google'); }); it('parse object from form source', () => { @@ -285,8 +286,8 @@ describe('shared/SettingData', () => { }, }; - let settings = SettingData.valueOf(data).toSettings(); - expect(settings.search.default).to.equal('yahoo'); + let settings = SettingData.fromJSON(data).toSettings(); + expect(settings.search.defaultEngine).to.equal('yahoo'); }); }); }); diff --git a/test/shared/Settings.test.ts b/test/shared/Settings.test.ts deleted file mode 100644 index 04b28c4..0000000 --- a/test/shared/Settings.test.ts +++ /dev/null @@ -1,194 +0,0 @@ -import * as settings from '../../src/shared/Settings'; -import { expect } from 'chai'; - -describe('Settings', () => { - describe('#keymapsValueOf', () => { - it('returns empty object by empty settings', () => { - let keymaps = settings.keymapsValueOf({}); - expect(keymaps).to.be.empty; - }); - - it('returns keymaps by valid settings', () => { - let keymaps = settings.keymapsValueOf({ - k: { type: "scroll.vertically", count: -1 }, - j: { type: "scroll.vertically", count: 1 }, - }); - - 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(() => settings.keymapsValueOf(null)).to.throw(TypeError); - expect(() => settings.keymapsValueOf({ - k: { type: "invalid.operation" }, - })).to.throw(TypeError); - }); - }); - - describe('#searchValueOf', () => { - it('returns search settings by valid settings', () => { - let search = settings.searchValueOf({ - default: "google", - engines: { - "google": "https://google.com/search?q={}", - "yahoo": "https://search.yahoo.com/search?p={}", - } - }); - - expect(search).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(() => settings.searchValueOf(null)).to.throw(TypeError); - expect(() => settings.searchValueOf({})).to.throw(TypeError); - expect(() => settings.searchValueOf([])).to.throw(TypeError); - expect(() => settings.searchValueOf({ - default: 123, - engines: {} - })).to.throw(TypeError); - expect(() => settings.searchValueOf({ - default: "google", - engines: { - "google": 123456, - } - })).to.throw(TypeError); - expect(() => settings.searchValueOf({ - default: "wikipedia", - engines: { - "google": "https://google.com/search?q={}", - "yahoo": "https://search.yahoo.com/search?p={}", - } - })).to.throw(TypeError); - expect(() => settings.searchValueOf({ - default: "g o o g l e", - engines: { - "g o o g l e": "https://google.com/search?q={}", - } - })).to.throw(TypeError); - expect(() => settings.searchValueOf({ - default: "google", - engines: { - "google": "https://google.com/search", - } - })).to.throw(TypeError); - expect(() => settings.searchValueOf({ - default: "google", - engines: { - "google": "https://google.com/search?q={}&r={}", - } - })).to.throw(TypeError); - }); - }); - - describe('#propertiesValueOf', () => { - it('returns with default properties by empty settings', () => { - let props = settings.propertiesValueOf({}); - expect(props).to.deep.equal({ - hintchars: "abcdefghijklmnopqrstuvwxyz", - smoothscroll: false, - complete: "sbh" - }) - }); - - it('returns properties by valid settings', () => { - let props = settings.propertiesValueOf({ - hintchars: "abcdefgh", - smoothscroll: false, - complete: "sbh" - }); - - expect(props).to.deep.equal({ - hintchars: "abcdefgh", - smoothscroll: false, - complete: "sbh" - }); - }); - - it('throws a TypeError by invalid settings', () => { - expect(() => settings.keymapsValueOf(null)).to.throw(TypeError); - expect(() => settings.keymapsValueOf({ - smoothscroll: 'false', - })).to.throw(TypeError); - expect(() => settings.keymapsValueOf({ - unknown: 'xyz' - })).to.throw(TypeError); - }); - }); - - describe('#blacklistValueOf', () => { - it('returns empty array by empty settings', () => { - let blacklist = settings.blacklistValueOf([]); - expect(blacklist).to.be.empty; - }); - - it('returns blacklist by valid settings', () => { - let blacklist = settings.blacklistValueOf([ - "github.com", - "circleci.com", - ]); - - expect(blacklist).to.deep.equal([ - "github.com", - "circleci.com", - ]); - }); - - it('throws a TypeError by invalid settings', () => { - expect(() => settings.blacklistValueOf(null)).to.throw(TypeError); - expect(() => settings.blacklistValueOf({})).to.throw(TypeError); - expect(() => settings.blacklistValueOf([1,2,3])).to.throw(TypeError); - }); - }); - - describe('#valueOf', () => { - it('returns settings by valid settings', () => { - let x = settings.valueOf({ - keymaps: {}, - "search": { - "default": "google", - "engines": { - "google": "https://google.com/search?q={}", - } - }, - "properties": {}, - "blacklist": [] - }); - - expect(x).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.valueOf({}); - expect(value.keymaps).to.not.be.empty; - expect(value.properties).to.not.be.empty; - expect(value.search.default).to.be.a('string'); - expect(value.search.engines).to.be.an('object'); - expect(value.blacklist).to.be.empty; - }); - - it('throws a TypeError with an unknown field', () => { - expect(() => settings.valueOf({ name: 'alice' })).to.throw(TypeError) - }); - }); -}); diff --git a/test/shared/blacklists.test.ts b/test/shared/blacklists.test.ts deleted file mode 100644 index 289ea0f..0000000 --- a/test/shared/blacklists.test.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { includes } from 'shared/blacklists'; - -describe("shared/blacklist", () => { - it('matches by *', () => { - let blacklist = ['*']; - - expect(includes(blacklist, 'https://github.com/abc')).to.be.true; - }) - - it('matches by hostname', () => { - let blacklist = ['github.com']; - - expect(includes(blacklist, 'https://github.com')).to.be.true; - expect(includes(blacklist, 'https://gist.github.com')).to.be.false; - expect(includes(blacklist, 'https://github.com/ueokande')).to.be.true; - expect(includes(blacklist, 'https://github.org')).to.be.false; - expect(includes(blacklist, 'https://google.com/search?q=github.org')).to.be.false; - }) - - it('matches by hostname with wildcard', () => { - let blacklist = ['*.github.com']; - - expect(includes(blacklist, 'https://github.com')).to.be.false; - expect(includes(blacklist, 'https://gist.github.com')).to.be.true; - }) - - it('matches by path', () => { - let blacklist = ['github.com/abc']; - - expect(includes(blacklist, 'https://github.com/abc')).to.be.true; - expect(includes(blacklist, 'https://github.com/abcdef')).to.be.false; - expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; - }) - - it('matches by path with wildcard', () => { - let blacklist = ['github.com/abc*']; - - expect(includes(blacklist, 'https://github.com/abc')).to.be.true; - expect(includes(blacklist, 'https://github.com/abcdef')).to.be.true; - expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; - }) - - it('matches address and port', () => { - let blacklist = ['127.0.0.1:8888']; - - expect(includes(blacklist, 'http://127.0.0.1:8888/')).to.be.true; - expect(includes(blacklist, 'http://127.0.0.1:8888/hello')).to.be.true; - }) -}); diff --git a/test/shared/properties.test.js b/test/shared/properties.test.js deleted file mode 100644 index 37903d8..0000000 --- a/test/shared/properties.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import * as settings from 'shared/settings'; - -describe('properties', () => { - describe('Def class', () => { - it('returns property definitions', () => { - let def = new proerties.Def( - 'smoothscroll', - 'smooth scroll', - false); - - expect(def.name).to.equal('smoothscroll'); - expect(def.describe).to.equal('smooth scroll'); - expect(def.defaultValue).to.equal(false); - expect(def.type).to.equal('boolean'); - }); - }); -}); - diff --git a/test/shared/property-defs.test.js b/test/shared/property-defs.test.js deleted file mode 100644 index 37903d8..0000000 --- a/test/shared/property-defs.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import * as settings from 'shared/settings'; - -describe('properties', () => { - describe('Def class', () => { - it('returns property definitions', () => { - let def = new proerties.Def( - 'smoothscroll', - 'smooth scroll', - false); - - expect(def.name).to.equal('smoothscroll'); - expect(def.describe).to.equal('smooth scroll'); - expect(def.defaultValue).to.equal(false); - expect(def.type).to.equal('boolean'); - }); - }); -}); - 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) + }); + }); +}); diff --git a/test/shared/urls.test.ts b/test/shared/urls.test.ts index f2950b6..3a3eea6 100644 --- a/test/shared/urls.test.ts +++ b/test/shared/urls.test.ts @@ -1,14 +1,16 @@ -import * as parsers from 'shared/urls'; +import * as parsers from '../../src/shared/urls'; +import { expect } from 'chai'; +import Search from '../../src/shared/settings/Search'; describe("shared/commands/parsers", () => { describe('#searchUrl', () => { - const config = { + const config = Search.fromJSON({ default: 'google', engines: { google: 'https://google.com/search?q={}', yahoo: 'https://yahoo.com/search?q={}', } - }; + }); it('convertes search url', () => { expect(parsers.searchUrl('google.com', config)) diff --git a/test/shared/utils/re.test.ts b/test/shared/utils/re.test.ts deleted file mode 100644 index d12ceb7..0000000 --- a/test/shared/utils/re.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as re from 'shared/utils/re'; - -describe("re util", () => { - it('matches by pattern', () => { - let regex = re.fromWildcard('*.example.com/*'); - expect('foo.example.com/bar').to.match(regex); - expect('foo.example.com').not.to.match(regex); - expect('example.com/bar').not.to.match(regex); - - regex = re.fromWildcard('example.com/*') - expect('example.com/foo').to.match(regex); - expect('example.com/').to.match(regex); - - regex = re.fromWildcard('example.com/*bar') - expect('example.com/foobar').to.match(regex); - expect('example.com/bar').to.match(regex); - expect('example.com/foobarfoo').not.to.match(regex); - }) -}); |