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 20, 2023
1 parent d68531b commit 3f368fd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
12 changes: 12 additions & 0 deletions generator/gen_wrapper_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ function addWhenContext(when, context) {
}
}

const containsWhenContext = function(when, context) {
if (when) {
return when.split('||').every(cond => {
return cond.split('&&').some(cond => {
return cond.trim() === context;
});
});
}
return false;
};

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

const containsWhenContext = function(when, context) {
if (when) {
return when.split('||').every(cond => {
return cond.split('&&').some(cond => {
return cond.trim() === context;
});
});
}
return false;
};
const removeWhenContext = function(when, context) {
return when.split('||').map(cond => {
return cond.split('&&').filter(cond => {
Expand All @@ -27,27 +17,27 @@ const removeWhenContext = function(when, context) {

const availableOnWindows = function(keybinding) {
return (
!containsWhenContext(keybinding.when, 'isLinux') &&
!containsWhenContext(keybinding.when, 'isMac') &&
!containsWhenContext(keybinding.when, '!isWindows') &&
!genWrapperUtil.containsWhenContext(keybinding.when, 'isLinux') &&
!genWrapperUtil.containsWhenContext(keybinding.when, 'isMac') &&
!genWrapperUtil.containsWhenContext(keybinding.when, '!isWindows') &&
!/\bmeta\b/.test(keybinding.key) &&
!/\bcmd\b/.test(keybinding.key)
);
};
const availableOnLinux = function(keybinding) {
return (
!containsWhenContext(keybinding.when, 'isWindows') &&
!containsWhenContext(keybinding.when, 'isMac') &&
!containsWhenContext(keybinding.when, '!isLinux') &&
!genWrapperUtil.containsWhenContext(keybinding.when, 'isWindows') &&
!genWrapperUtil.containsWhenContext(keybinding.when, 'isMac') &&
!genWrapperUtil.containsWhenContext(keybinding.when, '!isLinux') &&
!/\bwin\b/.test(keybinding.key) &&
!/\bcmd\b/.test(keybinding.key)
);
};
const availableOnMac = function(keybinding) {
return (
!containsWhenContext(keybinding.when, 'isWindows') &&
!containsWhenContext(keybinding.when, 'isLinux') &&
!containsWhenContext(keybinding.when, '!isMac') &&
!genWrapperUtil.containsWhenContext(keybinding.when, 'isWindows') &&
!genWrapperUtil.containsWhenContext(keybinding.when, 'isLinux') &&
!genWrapperUtil.containsWhenContext(keybinding.when, '!isMac') &&
(
(
!/\bwin\b/.test(keybinding.key) &&
Expand Down Expand Up @@ -251,7 +241,7 @@ async function verifyWrapper() {

for (const wrapper of wrappers) {
assert.ok(
containsWhenContext(wrapper.when, 'kb-macro.active'),
genWrapperUtil.containsWhenContext(wrapper.when, 'kb-macro.active'),
'"when" in a wrapper should contain "kb-macro.active &&" context'
);
if (isWrapped(wrapper)) {
Expand Down
30 changes: 30 additions & 0 deletions test/suite/gen_wrapper_util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ 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 && c2', 'c1'), true);
assert.strictEqual(containsWhenContext('c2 && c1', 'c1'), true);
});
it('should return false if given when clause can evaluate true without given context being true', () => {
assert.strictEqual(containsWhenContext('c2', 'c1'), false);
assert.strictEqual(containsWhenContext('c2 && c3', 'c1'), false);
});
it('should return false if given when clause is empty', () => {
assert.strictEqual(containsWhenContext('', 'c1'), false);
});
it('should handle OR expression', () => {
assert.strictEqual(containsWhenContext('c1 || c2', 'c1'), false);
assert.strictEqual(containsWhenContext('c2 || c3', 'c1'), false);
assert.strictEqual(containsWhenContext('c1 && c2 || c3 && c1', 'c1'), true);
});
it('should handle non-logical operators', () => {
assert.strictEqual(containsWhenContext('c1 && a == b || c == d && c1', 'c1'), true);
});
// TODO: https://github.com/tshino/vscode-kb-macro/issues/296
/*
it('should not recognise inside parenthesized portion in given when clause', () => {
assert.strictEqual(containsWhenContext('(c1 && c2)', 'c1'), false);
assert.strictEqual(containsWhenContext('c1 && (c2 || c3)', 'c1'), true);
assert.strictEqual(containsWhenContext('(c2 || c3) && c1', 'c1'), true);
});
*/
});
describe('negateContext', () => {
const negateContext = genWrapperUtil.negateContext;
it('should append operator ! in front of given context', () => {
Expand Down

0 comments on commit 3f368fd

Please sign in to comment.