From 3f368fd86f9d09f08e3ad0cc7cb9e9e519a0666e Mon Sep 17 00:00:00 2001 From: tshino Date: Sat, 21 Oct 2023 02:47:36 +0900 Subject: [PATCH] Add test for gen_wrapper_util #296 --- generator/gen_wrapper_util.js | 12 ++++++++++++ generator/verify_wrapper.js | 30 ++++++++++------------------- test/suite/gen_wrapper_util.test.js | 30 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/generator/gen_wrapper_util.js b/generator/gen_wrapper_util.js index aa54fafe..25a406bc 100644 --- a/generator/gen_wrapper_util.js +++ b/generator/gen_wrapper_util.js @@ -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(); @@ -290,6 +301,7 @@ module.exports = { writeFile, makeCompactKeybindingsJSON, addWhenContext, + containsWhenContext, negateContext, copyKeybinding, removeOSSpecificKeys, diff --git a/generator/verify_wrapper.js b/generator/verify_wrapper.js index 507f13a3..04bb27a7 100644 --- a/generator/verify_wrapper.js +++ b/generator/verify_wrapper.js @@ -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 => { @@ -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) && @@ -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)) { diff --git a/test/suite/gen_wrapper_util.test.js b/test/suite/gen_wrapper_util.test.js index 8abe713d..eee654cb 100644 --- a/test/suite/gen_wrapper_util.test.js +++ b/test/suite/gen_wrapper_util.test.js @@ -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', () => {