Declare setting types

This commit is contained in:
Shin'ya Ueoka 2019-05-05 08:03:29 +09:00
parent d01db82c0d
commit a0882bbceb
48 changed files with 1618 additions and 903 deletions

View file

@ -0,0 +1,293 @@
import SettingData, {
FormKeymaps, JSONSettings, FormSettings,
} from '../../src/shared/SettingData';
import Settings, { Keymaps } from '../../src/shared/Settings';
import { expect } from 'chai';
describe('shared/SettingData', () => {
describe('FormKeymaps', () => {
describe('#valueOF to #toKeymaps', () => {
it('parses form keymaps and convert to operations', () => {
let data = {
'scroll.vertically?{"count":1}': 'j',
'scroll.home': '0',
}
let keymaps = FormKeymaps.valueOf(data).toKeymaps();
expect(keymaps).to.deep.equal({
'j': { type: 'scroll.vertically', count: 1 },
'0': { type: 'scroll.home' },
});
});
});
describe('#fromKeymaps to #toJSON', () => {
it('create from a Keymaps and create a JSON object', () => {
let data: Keymaps = {
'j': { type: 'scroll.vertically', count: 1 },
'0': { type: 'scroll.home' },
}
let keymaps = FormKeymaps.fromKeymaps(data).toJSON();
expect(keymaps).to.deep.equal({
'scroll.vertically?{"count":1}': 'j',
'scroll.home': '0',
});
});
});
});
describe('JSONSettings', () => {
describe('#valueOf to #toSettings', () => {
it('parse object and create a Settings', () => {
let o = `{
"keymaps": {},
"search": {
"default": "google",
"engines": {
"google": "https://google.com/search?q={}"
}
},
"properties": {
"hintchars": "abcdefghijklmnopqrstuvwxyz",
"smoothscroll": false,
"complete": "sbh"
},
"blacklist": []
}`;
let settings = JSONSettings.valueOf(o).toSettings();
expect(settings).to.deep.equal(JSON.parse(o));
});
});
describe('#fromSettings to #toJSON', () => {
it('create from a Settings and create a JSON string', () => {
let o = {
keymaps: {},
search: {
default: "google",
engines: {
google: "https://google.com/search?q={}",
},
},
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh"
},
blacklist: [],
};
let json = JSONSettings.fromSettings(o).toJSON();
expect(JSON.parse(json)).to.deep.equal(o);
});
});
});
describe('FormSettings', () => {
describe('#valueOf to #toSettings', () => {
it('parse object and create a Settings', () => {
let data = {
keymaps: {
'scroll.vertically?{"count":1}': 'j',
'scroll.home': '0',
},
search: {
default: "google",
engines: [
["google", "https://google.com/search?q={}"],
]
},
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh"
},
blacklist: []
};
let settings = FormSettings.valueOf(data).toSettings();
expect(settings).to.deep.equal({
keymaps: {
'j': { type: 'scroll.vertically', count: 1 },
'0': { type: 'scroll.home' },
},
search: {
default: "google",
engines: {
"google": "https://google.com/search?q={}"
}
},
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh"
},
blacklist: []
});
});
});
describe('#fromSettings to #toJSON', () => {
it('create from a Settings and create a JSON string', () => {
let data: Settings = {
keymaps: {
'j': { type: 'scroll.vertically', count: 1 },
'0': { type: 'scroll.home' },
},
search: {
default: "google",
engines: {
"google": "https://google.com/search?q={}"
}
},
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh"
},
blacklist: []
};
let json = FormSettings.fromSettings(data).toJSON();
expect(json).to.deep.equal({
keymaps: {
'scroll.vertically?{"count":1}': 'j',
'scroll.home': '0',
},
search: {
default: "google",
engines: [
["google", "https://google.com/search?q={}"],
]
},
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh"
},
blacklist: [],
});
});
});
});
describe('SettingData', () => {
describe('#valueOf to #toJSON', () => {
it('parse object from json source', () => {
let data = {
source: 'json',
json: `{
"keymaps": {},
"search": {
"default": "google",
"engines": {
"google": "https://google.com/search?q={}"
}
},
"properties": {
"hintchars": "abcdefghijklmnopqrstuvwxyz",
"smoothscroll": false,
"complete": "sbh"
},
"blacklist": []
}`,
};
let j = SettingData.valueOf(data).toJSON();
expect(j.source).to.equal('json');
expect(j.json).to.be.a('string');
});
it('parse object from form source', () => {
let data = {
source: 'form',
form: {
keymaps: {},
search: {
default: "yahoo",
engines: [
['yahoo', 'https://yahoo.com/search?q={}'],
],
},
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh"
},
blacklist: [],
},
};
let j = SettingData.valueOf(data).toJSON();
expect(j.source).to.equal('form');
expect(j.form).to.deep.equal({
keymaps: {},
search: {
default: "yahoo",
engines: [
['yahoo', 'https://yahoo.com/search?q={}'],
],
},
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh"
},
blacklist: [],
});
});
});
describe('#toSettings', () => {
it('parse object from json source', () => {
let data = {
source: 'json',
json: `{
"keymaps": {},
"search": {
"default": "google",
"engines": {
"google": "https://google.com/search?q={}"
}
},
"properties": {
"hintchars": "abcdefghijklmnopqrstuvwxyz",
"smoothscroll": false,
"complete": "sbh"
},
"blacklist": []
}`,
};
let settings = SettingData.valueOf(data).toSettings();
expect(settings.search.default).to.equal('google');
});
it('parse object from form source', () => {
let data = {
source: 'form',
form: {
keymaps: {},
search: {
default: "yahoo",
engines: [
['yahoo', 'https://yahoo.com/search?q={}'],
],
},
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh"
},
blacklist: [],
},
};
let settings = SettingData.valueOf(data).toSettings();
expect(settings.search.default).to.equal('yahoo');
});
});
});
});

View file

@ -0,0 +1,190 @@
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;
});
});
});

View file

@ -0,0 +1,18 @@
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');
});
});
});

View file

@ -0,0 +1,18 @@
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');
});
});
});

View file

@ -1,81 +0,0 @@
import { validate } from 'shared/settings/validator';
describe("setting validator", () => {
describe("unknown top keys", () => {
it('throws an error for unknown settings', () => {
let settings = { keymaps: {}, poison: 123 };
let fn = validate.bind(undefined, settings)
expect(fn).to.throw(Error, 'poison');
})
});
describe("keymaps settings", () => {
it('throws an error for unknown operation', () => {
let settings = {
keymaps: {
a: { 'type': 'scroll.home' },
b: { 'type': 'poison.dressing' },
}
};
let fn = validate.bind(undefined, settings)
expect(fn).to.throw(Error, 'poison.dressing');
});
});
describe("search settings", () => {
it('throws an error for invalid search engine name', () => {
let settings = {
search: {
default: 'google',
engines: {
'google': 'https://google.com/search?q={}',
'cherry pie': 'https://cherypie.com/search?q={}',
}
}
};
let fn = validate.bind(undefined, settings)
expect(fn).to.throw(Error, 'cherry pie');
});
it('throws an error for no {}-placeholder', () => {
let settings = {
search: {
default: 'google',
engines: {
'google': 'https://google.com/search?q={}',
'yahoo': 'https://search.yahoo.com/search',
}
}
};
let fn = validate.bind(undefined, settings)
expect(fn).to.throw(Error, 'yahoo');
});
it('throws an error for no default engines', () => {
let settings = {
search: {
engines: {
'google': 'https://google.com/search?q={}',
'yahoo': 'https://search.yahoo.com/search?q={}',
}
}
};
let fn = validate.bind(undefined, settings)
expect(fn).to.throw(Error, 'Default engine');
});
it('throws an error for invalid default engine', () => {
let settings = {
search: {
default: 'twitter',
engines: {
'google': 'https://google.com/search?q={}',
'yahoo': 'https://search.yahoo.com/search?q={}',
}
}
};
let fn = validate.bind(undefined, settings)
expect(fn).to.throw(Error, 'twitter');
});
});
});

View file

@ -1,138 +0,0 @@
import * as values from 'shared/settings/values';
describe("settings values", () => {
describe('valueFromJson', () => {
it('return object from json string', () => {
let json = `{
"keymaps": { "0": {"type": "scroll.home"}},
"search": { "default": "google", "engines": { "google": "https://google.com/search?q={}" }},
"blacklist": [ "*.slack.com"],
"properties": {
"mystr": "value",
"mynum": 123,
"mybool": true
}
}`;
let value = values.valueFromJson(json);
expect(value.keymaps).to.deep.equal({ 0: {type: "scroll.home"}});
expect(value.search).to.deep.equal({ default: "google", engines: { google: "https://google.com/search?q={}"} });
expect(value.blacklist).to.deep.equal(["*.slack.com"]);
expect(value.properties).to.have.property('mystr', 'value');
expect(value.properties).to.have.property('mynum', 123);
expect(value.properties).to.have.property('mybool', true);
});
});
describe('valueFromForm', () => {
it('returns value from form', () => {
let form = {
keymaps: {
'scroll.vertically?{"count":1}': 'j',
'scroll.home': '0',
},
search: {
default: 'google',
engines: [['google', 'https://google.com/search?q={}']],
},
blacklist: ['*.slack.com'],
"properties": {
"mystr": "value",
"mynum": 123,
"mybool": true,
}
};
let value = values.valueFromForm(form);
expect(value.keymaps).to.have.deep.property('j', { type: "scroll.vertically", count: 1 });
expect(value.keymaps).to.have.deep.property('0', { type: "scroll.home" });
expect(JSON.stringify(value.search)).to.deep.equal(JSON.stringify({ default: "google", engines: { google: "https://google.com/search?q={}"} }));
expect(value.search).to.deep.equal({ default: "google", engines: { google: "https://google.com/search?q={}"} });
expect(value.blacklist).to.deep.equal(["*.slack.com"]);
expect(value.properties).to.have.property('mystr', 'value');
expect(value.properties).to.have.property('mynum', 123);
expect(value.properties).to.have.property('mybool', true);
});
it('convert from empty form', () => {
let form = {};
let value = values.valueFromForm(form);
expect(value).to.not.have.key('keymaps');
expect(value).to.not.have.key('search');
expect(value).to.not.have.key('blacklist');
expect(value).to.not.have.key('properties');
});
it('override keymaps', () => {
let form = {
keymaps: {
'scroll.vertically?{"count":1}': 'j',
'scroll.vertically?{"count":-1}': 'j',
}
};
let value = values.valueFromForm(form);
expect(value.keymaps).to.have.key('j');
});
it('override search engine', () => {
let form = {
search: {
default: 'google',
engines: [
['google', 'https://google.com/search?q={}'],
['google', 'https://google.co.jp/search?q={}'],
]
}
};
let value = values.valueFromForm(form);
expect(value.search.engines).to.have.property('google', 'https://google.co.jp/search?q={}');
});
});
describe('jsonFromValue', () => {
});
describe('formFromValue', () => {
it('convert empty value to form', () => {
let value = {};
let form = values.formFromValue(value);
expect(value).to.not.have.key('keymaps');
expect(value).to.not.have.key('search');
expect(value).to.not.have.key('blacklist');
});
it('convert value to form', () => {
let value = {
keymaps: {
j: { type: 'scroll.vertically', count: 1 },
JJ: { type: 'scroll.vertically', count: 100 },
0: { type: 'scroll.home' },
},
search: { default: 'google', engines: { google: 'https://google.com/search?q={}' }},
blacklist: [ '*.slack.com'],
properties: {
"mystr": "value",
"mynum": 123,
"mybool": true,
}
};
let allowed = ['scroll.vertically?{"count":1}', 'scroll.home' ];
let form = values.formFromValue(value, allowed);
expect(form.keymaps).to.have.property('scroll.vertically?{"count":1}', 'j');
expect(form.keymaps).to.not.have.property('scroll.vertically?{"count":100}');
expect(form.keymaps).to.have.property('scroll.home', '0');
expect(Object.keys(form.keymaps)).to.have.lengthOf(2);
expect(form.search).to.have.property('default', 'google');
expect(form.search).to.have.deep.property('engines', [['google', 'https://google.com/search?q={}']]);
expect(form.blacklist).to.have.lengthOf(1);
expect(form.blacklist).to.include('*.slack.com');
expect(form.properties).to.have.property('mystr', 'value');
expect(form.properties).to.have.property('mynum', 123);
expect(form.properties).to.have.property('mybool', true);
});
});
});