Skip to content

Commit

Permalink
Add decomposeWhenClause() #296
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Oct 22, 2023
1 parent 09368ab commit bee13ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions generator/gen_wrapper_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ function makeCompactKeybindingsJSON(keybindings) {
return compactJson;
}

const decomposeWhenClause = function(when) {
// returns Array of Arrays.
// The outer array corresponds to '||' operators.
// The inner array corresponds to '&&' operators.
return when.split('||').map(
cond => cond.split('&&').map(
cond => cond.trim()
)
);
};

function addWhenContext(when, context) {
context = context || '';
if (when) {
Expand Down Expand Up @@ -308,6 +319,7 @@ module.exports = {
writeJSON,
writeFile,
makeCompactKeybindingsJSON,
decomposeWhenClause,
addWhenContext,
containsWhenContext,
removeWhenContext,
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 @@ -3,6 +3,36 @@ const assert = require('assert');
const genWrapperUtil = require('../../generator/gen_wrapper_util.js');

describe('gen_wrapper_util', () => {
describe('decomposeWhenClause', () => {
const decomposeWhenClause = genWrapperUtil.decomposeWhenClause;
it('should split OR-of-AND expressions into nested Array of conditions', () => {
assert.deepStrictEqual(decomposeWhenClause('a'), [['a']]);
assert.deepStrictEqual(decomposeWhenClause('a || b'), [['a'], ['b']]);
assert.deepStrictEqual(decomposeWhenClause('a && b'), [['a', 'b']]);
assert.deepStrictEqual(decomposeWhenClause('a && b || c'), [['a', 'b'], ['c']]);
assert.deepStrictEqual(decomposeWhenClause('a && b || c && d'), [['a', 'b'], ['c', 'd']]);
});
it('should return [[""]] if empty string is given', () => {
assert.deepStrictEqual(decomposeWhenClause(''), [['']]);
});
it('should retain non-logical expressions portion untouched', () => {
assert.deepStrictEqual(decomposeWhenClause('a == b'), [['a == b']]);
assert.deepStrictEqual(decomposeWhenClause('a == b && c'), [['a == b', 'c']]);
});
it('should leave negate operators being attached', () => {
assert.deepStrictEqual(decomposeWhenClause('!c'), [['!c']]);
assert.deepStrictEqual(decomposeWhenClause('c1 || !c2 || !!c3'), [['c1'], ['!c2'], ['!!c3']]);
});
// TODO: https://github.com/tshino/vscode-kb-macro/issues/296
/*
it('should leave parenthesized portion unchanged', () => {
assert.deepStrictEqual(decomposeWhenClause('(c1 || c2)'), [['(c1 || c2)']]);
assert.deepStrictEqual(decomposeWhenClause('!(c1 || c2)'), [['!(c1 || c2)']]);
assert.deepStrictEqual(decomposeWhenClause('c1 && (c2 || c3)'), [['c1', '(c2 || c3)']]);
assert.deepStrictEqual(decomposeWhenClause('c1 || (c2 && c3)'), [['c1'], ['(c2 && c3)']]);
});
*/
});
describe('addWhenContext', () => {
const addWhenContext = genWrapperUtil.addWhenContext;
it('should return given context if when clause is empty', () => {
Expand Down

0 comments on commit bee13ca

Please sign in to comment.