Skip to content

Commit

Permalink
Add test for gen_wrapper_util #296
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Oct 21, 2023
1 parent 3f368fd commit 09368ab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
9 changes: 9 additions & 0 deletions generator/gen_wrapper_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ const containsWhenContext = function(when, context) {
return false;
};

const removeWhenContext = function(when, context) {
return when.split('||').map(cond => {
return cond.split('&&').filter(cond => {
return cond.trim() !== context;
}).map(cond => cond.trim()).join(' && ');
}).filter(cond => cond !== '').join(' || ');
};

function negateContext(context) {
if (context) {
context = context.trim();
Expand Down Expand Up @@ -302,6 +310,7 @@ module.exports = {
makeCompactKeybindingsJSON,
addWhenContext,
containsWhenContext,
removeWhenContext,
negateContext,
copyKeybinding,
removeOSSpecificKeys,
Expand Down
28 changes: 10 additions & 18 deletions generator/verify_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ const defaultKeybindingsLoader = require('./default_keybindings_loader');
const PackageJsonPath = './package.json';
const ConfigPath = 'generator/config.json';

const removeWhenContext = function(when, context) {
return when.split('||').map(cond => {
return cond.split('&&').filter(cond => {
return cond.trim() !== context;
}).map(cond => cond.trim()).join(' && ');
}).filter(cond => cond !== '').join(' || ');
};

const availableOnWindows = function(keybinding) {
return (
!genWrapperUtil.containsWhenContext(keybinding.when, 'isLinux') &&
Expand Down Expand Up @@ -59,14 +51,14 @@ const unwrapCommon = function(keybinding) {
delete keybinding.args;
}
}
keybinding.when = removeWhenContext(keybinding.when, 'kb-macro.active');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, 'kb-macro.active');
return keybinding;
};
const unwrapForWindows = function(keybinding) {
keybinding = unwrapCommon(keybinding);
keybinding.when = removeWhenContext(keybinding.when, 'isWindows');
keybinding.when = removeWhenContext(keybinding.when, '!isLinux');
keybinding.when = removeWhenContext(keybinding.when, '!isMac');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, 'isWindows');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, '!isLinux');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, '!isMac');
if (keybinding.when === '') {
delete keybinding.when;
}
Expand All @@ -78,9 +70,9 @@ const unwrapForWindows = function(keybinding) {
};
const unwrapForLinux = function(keybinding) {
keybinding = unwrapCommon(keybinding);
keybinding.when = removeWhenContext(keybinding.when, 'isLinux');
keybinding.when = removeWhenContext(keybinding.when, '!isWindows');
keybinding.when = removeWhenContext(keybinding.when, '!isMac');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, 'isLinux');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, '!isWindows');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, '!isMac');
if (keybinding.when === '') {
delete keybinding.when;
}
Expand All @@ -92,9 +84,9 @@ const unwrapForLinux = function(keybinding) {
};
const unwrapForMac = function(keybinding) {
keybinding = unwrapCommon(keybinding);
keybinding.when = removeWhenContext(keybinding.when, 'isMac');
keybinding.when = removeWhenContext(keybinding.when, '!isWindows');
keybinding.when = removeWhenContext(keybinding.when, '!isLinux');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, 'isMac');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, '!isWindows');
keybinding.when = genWrapperUtil.removeWhenContext(keybinding.when, '!isLinux');
if (keybinding.when === '') {
delete keybinding.when;
}
Expand Down
37 changes: 37 additions & 0 deletions test/suite/gen_wrapper_util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('gen_wrapper_util', () => {
describe('containsWhenContext', () => {
const containsWhenContext = genWrapperUtil.containsWhenContext;
it('should return true if given when clause contains given context as an AND expression', () => {
assert.strictEqual(containsWhenContext('c1', 'c1'), true);
assert.strictEqual(containsWhenContext('c1 && c2', 'c1'), true);
assert.strictEqual(containsWhenContext('c2 && c1', 'c1'), true);
});
Expand Down Expand Up @@ -76,6 +77,42 @@ describe('gen_wrapper_util', () => {
});
*/
});
describe('removeWhenContext', () => {
const removeWhenContext = genWrapperUtil.removeWhenContext;
it('should remove given context from given when clause expression', () => {
assert.strictEqual(removeWhenContext('c1 && c2', 'c1'), 'c2');
assert.strictEqual(removeWhenContext('c2 && c1', 'c1'), 'c2');
assert.strictEqual(removeWhenContext('c1', 'c1'), '');
});
it('should leave given when clause unchanged when it does not contain given context', () => {
assert.strictEqual(removeWhenContext('c2 && c3', 'c1'), 'c2 && c3');
assert.strictEqual(removeWhenContext('c2', 'c1'), 'c2');
assert.strictEqual(removeWhenContext('', 'c1'), '');
});
it('should handle a negate operator as a part of context to match', () => {
assert.strictEqual(removeWhenContext('c2 && !c1', '!c1'), 'c2');
assert.strictEqual(removeWhenContext('!c2 && c1', 'c1'), '!c2');
assert.strictEqual(removeWhenContext('!c2 && c1', '!c1'), '!c2 && c1');
assert.strictEqual(removeWhenContext('!c2 && !c1', 'c1'), '!c2 && !c1');
});
it('should handle OR expression', () => {
assert.strictEqual(removeWhenContext('c1 && c2 || c1 && c3', 'c1'), 'c2 || c3');
});
it('should handle non-logical operators', () => {
assert.strictEqual(removeWhenContext('c1 && a == b || c == d && c1', 'c1'), 'a == b || c == d');
});
// TODO: https://github.com/tshino/vscode-kb-macro/issues/296
/*
it('should leave parenthesized portion unchanged', () => {
assert.strictEqual(removeWhenContext('(c1 && c2)', 'c1'), '(c1 && c2)');
assert.strictEqual(removeWhenContext('(c2 && c1 && c3)', 'c1'), '(c2 && c1 && c3)');
assert.strictEqual(removeWhenContext('c1 && (c2 || c3)', 'c1'), '(c2 || c3)');
assert.strictEqual(removeWhenContext('c1 && (c2 || c1 && c3)', 'c1'), '(c2 || c1 && c3)');
assert.strictEqual(removeWhenContext('(c2 || c3) && c1', 'c1'), '(c2 || c3)');
assert.strictEqual(removeWhenContext('(c2 && c1 || c3) && c1', 'c1'), '(c2 && c1 || c3)');
});
*/
});
describe('negateContext', () => {
const negateContext = genWrapperUtil.negateContext;
it('should append operator ! in front of given context', () => {
Expand Down

0 comments on commit 09368ab

Please sign in to comment.