Skip to content

Commit

Permalink
Add isValidAwaitOption() to genWrapperUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Dec 1, 2021
1 parent 162ec6d commit 7e1a3a8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
16 changes: 15 additions & 1 deletion generator/gen_wrapper_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,26 @@ function combineBaseKeybingings(baseKeybindings) {
return keybindings;
}

const ValidAwaitTargets = new Set(['selection', 'document', 'clipboard']);

function isValidAwaitOption(awaitOption) {
if (typeof awaitOption !== 'string') {
return false;
}
const awaitList = awaitOption.split(' ').filter(target => target !== '');
return (
awaitList.length === 0 ||
awaitList.every(target => ValidAwaitTargets.has(target))
);
}

module.exports = {
readJSON,
writeJSON,
loadBaseKeybindings,
addWhenContext,
copyKeybinding,
keybindingsContains,
combineBaseKeybingings
combineBaseKeybingings,
isValidAwaitOption
};
25 changes: 25 additions & 0 deletions test/suite/gen_wrapper_util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,29 @@ describe('gen_wrapper_util', () => {
assert.deepStrictEqual(combineBaseKeybingings(input), expected);
});
});
describe('isValidAwaitOption', () => {
const isValidAwaitOption = genWrapperUtil.isValidAwaitOption;
it('should return true if passed string is a valid await option for a wrapper', () => {
assert.strictEqual(isValidAwaitOption('selection'), true);
assert.strictEqual(isValidAwaitOption('document'), true);
assert.strictEqual(isValidAwaitOption('clipboard'), true);
});
it('should return true on empty string', () => {
assert.strictEqual(isValidAwaitOption(''), true);
});
it('should return false on string with invalid await target', () => {
assert.strictEqual(isValidAwaitOption('hello'), false);
assert.strictEqual(isValidAwaitOption('selection world'), false);
});
it('should return false on any value of types other than string', () => {
assert.strictEqual(isValidAwaitOption(null), false);
assert.strictEqual(isValidAwaitOption(), false);
assert.strictEqual(isValidAwaitOption([]), false);
});
it('should return true on string with multiple valid await targets', () => {
assert.strictEqual(isValidAwaitOption('document selection'), true);
assert.strictEqual(isValidAwaitOption('selection clipboard'), true);
assert.strictEqual(isValidAwaitOption('selection document clipboard'), true);
});
});
});

0 comments on commit 7e1a3a8

Please sign in to comment.