Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/contextkey/test/common/contextkey.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import assert from 'assert';
6
import { isLinux, isMacintosh, isWindows } from '../../../../base/common/platform.js';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
8
import { ContextKeyExpr, ContextKeyExpression, implies } from '../../common/contextkey.js';
9
10
function createContext(ctx: any) {
11
return {
12
getValue: (key: string) => {
13
return ctx[key];
14
}
15
};
16
}
17
18
suite('ContextKeyExpr', () => {
19
20
ensureNoDisposablesAreLeakedInTestSuite();
21
22
test('ContextKeyExpr.equals', () => {
23
const a = ContextKeyExpr.and(
24
ContextKeyExpr.has('a1'),
25
ContextKeyExpr.and(ContextKeyExpr.has('and.a')),
26
ContextKeyExpr.has('a2'),
27
ContextKeyExpr.regex('d3', /d.*/),
28
ContextKeyExpr.regex('d4', /\*\*3*/),
29
ContextKeyExpr.equals('b1', 'bb1'),
30
ContextKeyExpr.equals('b2', 'bb2'),
31
ContextKeyExpr.notEquals('c1', 'cc1'),
32
ContextKeyExpr.notEquals('c2', 'cc2'),
33
ContextKeyExpr.not('d1'),
34
ContextKeyExpr.not('d2')
35
)!;
36
const b = ContextKeyExpr.and(
37
ContextKeyExpr.equals('b2', 'bb2'),
38
ContextKeyExpr.notEquals('c1', 'cc1'),
39
ContextKeyExpr.not('d1'),
40
ContextKeyExpr.regex('d4', /\*\*3*/),
41
ContextKeyExpr.notEquals('c2', 'cc2'),
42
ContextKeyExpr.has('a2'),
43
ContextKeyExpr.equals('b1', 'bb1'),
44
ContextKeyExpr.regex('d3', /d.*/),
45
ContextKeyExpr.has('a1'),
46
ContextKeyExpr.and(ContextKeyExpr.equals('and.a', true)),
47
ContextKeyExpr.not('d2')
48
)!;
49
assert(a.equals(b), 'expressions should be equal');
50
});
51
52
test('issue #134942: Equals in comparator expressions', () => {
53
function testEquals(expr: ContextKeyExpression | undefined, str: string): void {
54
const deserialized = ContextKeyExpr.deserialize(str);
55
assert.ok(expr);
56
assert.ok(deserialized);
57
assert.strictEqual(expr.equals(deserialized), true, str);
58
}
59
testEquals(ContextKeyExpr.greater('value', 0), 'value > 0');
60
testEquals(ContextKeyExpr.greaterEquals('value', 0), 'value >= 0');
61
testEquals(ContextKeyExpr.smaller('value', 0), 'value < 0');
62
testEquals(ContextKeyExpr.smallerEquals('value', 0), 'value <= 0');
63
});
64
65
test('normalize', () => {
66
const key1IsTrue = ContextKeyExpr.equals('key1', true);
67
const key1IsNotFalse = ContextKeyExpr.notEquals('key1', false);
68
const key1IsFalse = ContextKeyExpr.equals('key1', false);
69
const key1IsNotTrue = ContextKeyExpr.notEquals('key1', true);
70
71
assert.ok(key1IsTrue.equals(ContextKeyExpr.has('key1')));
72
assert.ok(key1IsNotFalse.equals(ContextKeyExpr.has('key1')));
73
assert.ok(key1IsFalse.equals(ContextKeyExpr.not('key1')));
74
assert.ok(key1IsNotTrue.equals(ContextKeyExpr.not('key1')));
75
});
76
77
test('evaluate', () => {
78
const context = createContext({
79
'a': true,
80
'b': false,
81
'c': '5',
82
'd': 'd'
83
});
84
function testExpression(expr: string, expected: boolean): void {
85
// console.log(expr + ' ' + expected);
86
const rules = ContextKeyExpr.deserialize(expr);
87
assert.strictEqual(rules!.evaluate(context), expected, expr);
88
}
89
function testBatch(expr: string, value: any): void {
90
/* eslint-disable eqeqeq */
91
testExpression(expr, !!value);
92
testExpression(expr + ' == true', !!value);
93
testExpression(expr + ' != true', !value);
94
testExpression(expr + ' == false', !value);
95
testExpression(expr + ' != false', !!value);
96
testExpression(expr + ' == 5', value == <any>'5');
97
testExpression(expr + ' != 5', value != <any>'5');
98
testExpression('!' + expr, !value);
99
testExpression(expr + ' =~ /d.*/', /d.*/.test(value));
100
testExpression(expr + ' =~ /D/i', /D/i.test(value));
101
/* eslint-enable eqeqeq */
102
}
103
104
testBatch('a', true);
105
testBatch('b', false);
106
testBatch('c', '5');
107
testBatch('d', 'd');
108
testBatch('z', undefined);
109
110
testExpression('true', true);
111
testExpression('false', false);
112
testExpression('a && !b', true && !false);
113
testExpression('a && b', true && false);
114
testExpression('a && !b && c == 5', true && !false && '5' === '5');
115
testExpression('d =~ /e.*/', false);
116
117
// precedence test: false && true || true === true because && is evaluated first
118
testExpression('b && a || a', true);
119
120
testExpression('a || b', true);
121
testExpression('b || b', false);
122
testExpression('b && a || a && b', false);
123
});
124
125
test('negate', () => {
126
function testNegate(expr: string, expected: string): void {
127
const actual = ContextKeyExpr.deserialize(expr)!.negate().serialize();
128
assert.strictEqual(actual, expected);
129
}
130
testNegate('true', 'false');
131
testNegate('false', 'true');
132
testNegate('a', '!a');
133
testNegate('a && b || c', '!a && !c || !b && !c');
134
testNegate('a && b || c || d', '!a && !c && !d || !b && !c && !d');
135
testNegate('!a && !b || !c && !d', 'a && c || a && d || b && c || b && d');
136
testNegate('!a && !b || !c && !d || !e && !f', 'a && c && e || a && c && f || a && d && e || a && d && f || b && c && e || b && c && f || b && d && e || b && d && f');
137
});
138
139
test('false, true', () => {
140
function testNormalize(expr: string, expected: string): void {
141
const actual = ContextKeyExpr.deserialize(expr)!.serialize();
142
assert.strictEqual(actual, expected);
143
}
144
testNormalize('true', 'true');
145
testNormalize('!true', 'false');
146
testNormalize('false', 'false');
147
testNormalize('!false', 'true');
148
testNormalize('a && true', 'a');
149
testNormalize('a && false', 'false');
150
testNormalize('a || true', 'true');
151
testNormalize('a || false', 'a');
152
testNormalize('isMac', isMacintosh ? 'true' : 'false');
153
testNormalize('isLinux', isLinux ? 'true' : 'false');
154
testNormalize('isWindows', isWindows ? 'true' : 'false');
155
});
156
157
test('issue #101015: distribute OR', () => {
158
function t(expr1: string, expr2: string, expected: string | undefined): void {
159
const e1 = ContextKeyExpr.deserialize(expr1);
160
const e2 = ContextKeyExpr.deserialize(expr2);
161
const actual = ContextKeyExpr.and(e1, e2)?.serialize();
162
assert.strictEqual(actual, expected);
163
}
164
t('a', 'b', 'a && b');
165
t('a || b', 'c', 'a && c || b && c');
166
t('a || b', 'c || d', 'a && c || a && d || b && c || b && d');
167
t('a || b', 'c && d', 'a && c && d || b && c && d');
168
t('a || b', 'c && d || e', 'a && e || b && e || a && c && d || b && c && d');
169
});
170
171
test('ContextKeyInExpr', () => {
172
const ainb = ContextKeyExpr.deserialize('a in b')!;
173
assert.strictEqual(ainb.evaluate(createContext({ 'a': 3, 'b': [3, 2, 1] })), true);
174
assert.strictEqual(ainb.evaluate(createContext({ 'a': 3, 'b': [1, 2, 3] })), true);
175
assert.strictEqual(ainb.evaluate(createContext({ 'a': 3, 'b': [1, 2] })), false);
176
assert.strictEqual(ainb.evaluate(createContext({ 'a': 3 })), false);
177
assert.strictEqual(ainb.evaluate(createContext({ 'a': 3, 'b': null })), false);
178
assert.strictEqual(ainb.evaluate(createContext({ 'a': 'x', 'b': ['x'] })), true);
179
assert.strictEqual(ainb.evaluate(createContext({ 'a': 'x', 'b': ['y'] })), false);
180
assert.strictEqual(ainb.evaluate(createContext({ 'a': 'x', 'b': {} })), false);
181
assert.strictEqual(ainb.evaluate(createContext({ 'a': 'x', 'b': { 'x': false } })), true);
182
assert.strictEqual(ainb.evaluate(createContext({ 'a': 'x', 'b': { 'x': true } })), true);
183
assert.strictEqual(ainb.evaluate(createContext({ 'a': 'prototype', 'b': {} })), false);
184
});
185
186
test('ContextKeyNotInExpr', () => {
187
const aNotInB = ContextKeyExpr.deserialize('a not in b')!;
188
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 3, 'b': [3, 2, 1] })), false);
189
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 3, 'b': [1, 2, 3] })), false);
190
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 3, 'b': [1, 2] })), true);
191
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 3 })), true);
192
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 3, 'b': null })), true);
193
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 'x', 'b': ['x'] })), false);
194
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 'x', 'b': ['y'] })), true);
195
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 'x', 'b': {} })), true);
196
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 'x', 'b': { 'x': false } })), false);
197
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 'x', 'b': { 'x': true } })), false);
198
assert.strictEqual(aNotInB.evaluate(createContext({ 'a': 'prototype', 'b': {} })), true);
199
});
200
201
test('issue #106524: distributing AND should normalize', () => {
202
const actual = ContextKeyExpr.and(
203
ContextKeyExpr.or(
204
ContextKeyExpr.has('a'),
205
ContextKeyExpr.has('b')
206
),
207
ContextKeyExpr.has('c')
208
);
209
const expected = ContextKeyExpr.or(
210
ContextKeyExpr.and(
211
ContextKeyExpr.has('a'),
212
ContextKeyExpr.has('c')
213
),
214
ContextKeyExpr.and(
215
ContextKeyExpr.has('b'),
216
ContextKeyExpr.has('c')
217
)
218
);
219
assert.strictEqual(actual!.equals(expected!), true);
220
});
221
222
test('issue #129625: Removes duplicated terms in OR expressions', () => {
223
const expr = ContextKeyExpr.or(
224
ContextKeyExpr.has('A'),
225
ContextKeyExpr.has('B'),
226
ContextKeyExpr.has('A')
227
)!;
228
assert.strictEqual(expr.serialize(), 'A || B');
229
});
230
231
test('Resolves true constant OR expressions', () => {
232
const expr = ContextKeyExpr.or(
233
ContextKeyExpr.has('A'),
234
ContextKeyExpr.not('A')
235
)!;
236
assert.strictEqual(expr.serialize(), 'true');
237
});
238
239
test('Resolves false constant AND expressions', () => {
240
const expr = ContextKeyExpr.and(
241
ContextKeyExpr.has('A'),
242
ContextKeyExpr.not('A')
243
)!;
244
assert.strictEqual(expr.serialize(), 'false');
245
});
246
247
test('issue #129625: Removes duplicated terms in AND expressions', () => {
248
const expr = ContextKeyExpr.and(
249
ContextKeyExpr.has('A'),
250
ContextKeyExpr.has('B'),
251
ContextKeyExpr.has('A')
252
)!;
253
assert.strictEqual(expr.serialize(), 'A && B');
254
});
255
256
test('issue #129625: Remove duplicated terms when negating', () => {
257
const expr = ContextKeyExpr.and(
258
ContextKeyExpr.has('A'),
259
ContextKeyExpr.or(
260
ContextKeyExpr.has('B1'),
261
ContextKeyExpr.has('B2'),
262
)
263
)!;
264
assert.strictEqual(expr.serialize(), 'A && B1 || A && B2');
265
assert.strictEqual(expr.negate()!.serialize(), '!A || !A && !B1 || !A && !B2 || !B1 && !B2');
266
assert.strictEqual(expr.negate()!.negate()!.serialize(), 'A && B1 || A && B2');
267
assert.strictEqual(expr.negate()!.negate()!.negate()!.serialize(), '!A || !A && !B1 || !A && !B2 || !B1 && !B2');
268
});
269
270
test('issue #129625: remove redundant terms in OR expressions', () => {
271
function strImplies(p0: string, q0: string): boolean {
272
const p = ContextKeyExpr.deserialize(p0)!;
273
const q = ContextKeyExpr.deserialize(q0)!;
274
return implies(p, q);
275
}
276
assert.strictEqual(strImplies('a && b', 'a'), true);
277
assert.strictEqual(strImplies('a', 'a && b'), false);
278
});
279
280
test('implies', () => {
281
function strImplies(p0: string, q0: string): boolean {
282
const p = ContextKeyExpr.deserialize(p0)!;
283
const q = ContextKeyExpr.deserialize(q0)!;
284
return implies(p, q);
285
}
286
assert.strictEqual(strImplies('a', 'a'), true);
287
assert.strictEqual(strImplies('a', 'a || b'), true);
288
assert.strictEqual(strImplies('a', 'a && b'), false);
289
assert.strictEqual(strImplies('a', 'a && b || a && c'), false);
290
assert.strictEqual(strImplies('a && b', 'a'), true);
291
assert.strictEqual(strImplies('a && b', 'b'), true);
292
assert.strictEqual(strImplies('a && b', 'a && b || c'), true);
293
assert.strictEqual(strImplies('a || b', 'a || c'), false);
294
assert.strictEqual(strImplies('a || b', 'a || b'), true);
295
assert.strictEqual(strImplies('a && b', 'a && b'), true);
296
assert.strictEqual(strImplies('a || b', 'a || b || c'), true);
297
assert.strictEqual(strImplies('c && a && b', 'c && a'), true);
298
});
299
300
test('Greater, GreaterEquals, Smaller, SmallerEquals evaluate', () => {
301
function checkEvaluate(expr: string, ctx: any, expected: any): void {
302
const _expr = ContextKeyExpr.deserialize(expr)!;
303
assert.strictEqual(_expr.evaluate(createContext(ctx)), expected);
304
}
305
306
checkEvaluate('a > 1', {}, false);
307
checkEvaluate('a > 1', { a: 0 }, false);
308
checkEvaluate('a > 1', { a: 1 }, false);
309
checkEvaluate('a > 1', { a: 2 }, true);
310
checkEvaluate('a > 1', { a: '0' }, false);
311
checkEvaluate('a > 1', { a: '1' }, false);
312
checkEvaluate('a > 1', { a: '2' }, true);
313
checkEvaluate('a > 1', { a: 'a' }, false);
314
315
checkEvaluate('a > 10', { a: 2 }, false);
316
checkEvaluate('a > 10', { a: 11 }, true);
317
checkEvaluate('a > 10', { a: '11' }, true);
318
checkEvaluate('a > 10', { a: '2' }, false);
319
checkEvaluate('a > 10', { a: '11' }, true);
320
321
checkEvaluate('a > 1.1', { a: 1 }, false);
322
checkEvaluate('a > 1.1', { a: 2 }, true);
323
checkEvaluate('a > 1.1', { a: 11 }, true);
324
checkEvaluate('a > 1.1', { a: '1.1' }, false);
325
checkEvaluate('a > 1.1', { a: '2' }, true);
326
checkEvaluate('a > 1.1', { a: '11' }, true);
327
328
checkEvaluate('a > b', { a: 'b' }, false);
329
checkEvaluate('a > b', { a: 'c' }, false);
330
checkEvaluate('a > b', { a: 1000 }, false);
331
332
checkEvaluate('a >= 2', { a: '1' }, false);
333
checkEvaluate('a >= 2', { a: '2' }, true);
334
checkEvaluate('a >= 2', { a: '3' }, true);
335
336
checkEvaluate('a < 2', { a: '1' }, true);
337
checkEvaluate('a < 2', { a: '2' }, false);
338
checkEvaluate('a < 2', { a: '3' }, false);
339
340
checkEvaluate('a <= 2', { a: '1' }, true);
341
checkEvaluate('a <= 2', { a: '2' }, true);
342
checkEvaluate('a <= 2', { a: '3' }, false);
343
});
344
345
test('Greater, GreaterEquals, Smaller, SmallerEquals negate', () => {
346
function checkNegate(expr: string, expected: string): void {
347
const a = ContextKeyExpr.deserialize(expr)!;
348
const b = a.negate();
349
assert.strictEqual(b.serialize(), expected);
350
}
351
352
checkNegate('a > 1', 'a <= 1');
353
checkNegate('a > 1.1', 'a <= 1.1');
354
checkNegate('a > b', 'a <= b');
355
356
checkNegate('a >= 1', 'a < 1');
357
checkNegate('a >= 1.1', 'a < 1.1');
358
checkNegate('a >= b', 'a < b');
359
360
checkNegate('a < 1', 'a >= 1');
361
checkNegate('a < 1.1', 'a >= 1.1');
362
checkNegate('a < b', 'a >= b');
363
364
checkNegate('a <= 1', 'a > 1');
365
checkNegate('a <= 1.1', 'a > 1.1');
366
checkNegate('a <= b', 'a > b');
367
});
368
369
test('issue #111899: context keys can use `<` or `>` ', () => {
370
const actual = ContextKeyExpr.deserialize('editorTextFocus && vim.active && vim.use<C-r>')!;
371
assert.ok(actual.equals(
372
ContextKeyExpr.and(
373
ContextKeyExpr.has('editorTextFocus'),
374
ContextKeyExpr.has('vim.active'),
375
ContextKeyExpr.has('vim.use<C-r>'),
376
)!
377
));
378
});
379
});
380
381