Make Blacklist class
This commit is contained in:
parent
574692551a
commit
b86b4680b6
12 changed files with 139 additions and 149 deletions
|
@ -6,6 +6,7 @@ import { expect } from 'chai';
|
|||
import Keymaps from '../../src/shared/settings/Keymaps';
|
||||
import Search from '../../src/shared/settings/Search';
|
||||
import Properties from '../../src/shared/settings/Properties';
|
||||
import Blacklist from '../../src/shared/settings/Blacklist'
|
||||
|
||||
describe('shared/SettingData', () => {
|
||||
describe('FormKeymaps', () => {
|
||||
|
@ -64,7 +65,7 @@ describe('shared/SettingData', () => {
|
|||
keymaps: settings.keymaps.toJSON(),
|
||||
search: settings.search.toJSON(),
|
||||
properties: settings.properties.toJSON(),
|
||||
blacklist: settings.blacklist,
|
||||
blacklist: settings.blacklist.toJSON(),
|
||||
}).to.deep.equal(JSON.parse(o));
|
||||
});
|
||||
});
|
||||
|
@ -84,7 +85,7 @@ describe('shared/SettingData', () => {
|
|||
smoothscroll: false,
|
||||
complete: "sbh"
|
||||
}),
|
||||
blacklist: [],
|
||||
blacklist: Blacklist.fromJSON([]),
|
||||
};
|
||||
|
||||
let json = JSONTextSettings.fromSettings(o).toJSONText();
|
||||
|
@ -92,7 +93,7 @@ describe('shared/SettingData', () => {
|
|||
keymaps: o.keymaps.toJSON(),
|
||||
search: o.search.toJSON(),
|
||||
properties: o.properties.toJSON(),
|
||||
blacklist: o.blacklist,
|
||||
blacklist: o.blacklist.toJSON(),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -125,7 +126,7 @@ describe('shared/SettingData', () => {
|
|||
keymaps: settings.keymaps.toJSON(),
|
||||
search: settings.search.toJSON(),
|
||||
properties: settings.properties.toJSON(),
|
||||
blacklist: settings.blacklist,
|
||||
blacklist: settings.blacklist.toJSON(),
|
||||
}).to.deep.equal({
|
||||
keymaps: {
|
||||
'j': { type: 'scroll.vertically', count: 1 },
|
||||
|
@ -165,7 +166,7 @@ describe('shared/SettingData', () => {
|
|||
smoothscroll: false,
|
||||
complete: "sbh"
|
||||
}),
|
||||
blacklist: []
|
||||
blacklist: Blacklist.fromJSON([]),
|
||||
};
|
||||
|
||||
let json = FormSettings.fromSettings(data).toJSON();
|
||||
|
|
|
@ -2,32 +2,6 @@ import * as settings from '../../src/shared/Settings';
|
|||
import {expect} from 'chai';
|
||||
|
||||
describe('Settings', () => {
|
||||
|
||||
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({
|
||||
|
@ -46,7 +20,7 @@ describe('Settings', () => {
|
|||
keymaps: x.keymaps.toJSON(),
|
||||
search: x.search.toJSON(),
|
||||
properties: x.properties.toJSON(),
|
||||
blacklist: x.blacklist,
|
||||
blacklist: x.blacklist.toJSON(),
|
||||
}).to.deep.equal({
|
||||
keymaps: {},
|
||||
search: {
|
||||
|
@ -70,7 +44,7 @@ describe('Settings', () => {
|
|||
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).to.be.empty;
|
||||
expect(value.blacklist.toJSON()).to.be.empty;
|
||||
});
|
||||
|
||||
it('throws a TypeError with an unknown field', () => {
|
||||
|
|
|
@ -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;
|
||||
})
|
||||
});
|
77
test/shared/settings/Blacklist.test.ts
Normal file
77
test/shared/settings/Blacklist.test.ts
Normal file
|
@ -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;
|
||||
})
|
||||
})
|
||||
});
|
|
@ -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);
|
||||
})
|
||||
});
|
Reference in a new issue