|
| 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 | +'use strict'; |
| 6 | + |
| 7 | +import * as assert from 'assert'; |
| 8 | +import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; |
| 9 | + |
| 10 | +suite('ContextKeyExpr', () => { |
| 11 | + test('ContextKeyExpr.equals', function () { |
| 12 | + let a = ContextKeyExpr.and( |
| 13 | + ContextKeyExpr.has('a1'), |
| 14 | + ContextKeyExpr.and(ContextKeyExpr.has('and.a')), |
| 15 | + ContextKeyExpr.has('a2'), |
| 16 | + ContextKeyExpr.equals('b1', 'bb1'), |
| 17 | + ContextKeyExpr.equals('b2', 'bb2'), |
| 18 | + ContextKeyExpr.notEquals('c1', 'cc1'), |
| 19 | + ContextKeyExpr.notEquals('c2', 'cc2'), |
| 20 | + ContextKeyExpr.not('d1'), |
| 21 | + ContextKeyExpr.not('d2') |
| 22 | + ); |
| 23 | + let b = ContextKeyExpr.and( |
| 24 | + ContextKeyExpr.equals('b2', 'bb2'), |
| 25 | + ContextKeyExpr.notEquals('c1', 'cc1'), |
| 26 | + ContextKeyExpr.not('d1'), |
| 27 | + ContextKeyExpr.notEquals('c2', 'cc2'), |
| 28 | + ContextKeyExpr.has('a2'), |
| 29 | + ContextKeyExpr.equals('b1', 'bb1'), |
| 30 | + ContextKeyExpr.has('a1'), |
| 31 | + ContextKeyExpr.and(ContextKeyExpr.equals('and.a', true)), |
| 32 | + ContextKeyExpr.not('d2') |
| 33 | + ); |
| 34 | + assert(a.equals(b), 'expressions should be equal'); |
| 35 | + }); |
| 36 | + |
| 37 | + test('normalize', function () { |
| 38 | + let key1IsTrue = ContextKeyExpr.equals('key1', true); |
| 39 | + let key1IsNotFalse = ContextKeyExpr.notEquals('key1', false); |
| 40 | + let key1IsFalse = ContextKeyExpr.equals('key1', false); |
| 41 | + let key1IsNotTrue = ContextKeyExpr.notEquals('key1', true); |
| 42 | + |
| 43 | + assert.ok(key1IsTrue.normalize().equals(ContextKeyExpr.has('key1'))); |
| 44 | + assert.ok(key1IsNotFalse.normalize().equals(ContextKeyExpr.has('key1'))); |
| 45 | + assert.ok(key1IsFalse.normalize().equals(ContextKeyExpr.not('key1'))); |
| 46 | + assert.ok(key1IsNotTrue.normalize().equals(ContextKeyExpr.not('key1'))); |
| 47 | + }); |
| 48 | + |
| 49 | + test('evaluate', function () { |
| 50 | + /* tslint:disable:triple-equals */ |
| 51 | + let context = { |
| 52 | + 'a': true, |
| 53 | + 'b': false, |
| 54 | + 'c': '5' |
| 55 | + }; |
| 56 | + function testExpression(expr: string, expected: boolean): void { |
| 57 | + let rules = ContextKeyExpr.deserialize(expr); |
| 58 | + assert.equal(rules.evaluate(context), expected, expr); |
| 59 | + } |
| 60 | + function testBatch(expr: string, value: any): void { |
| 61 | + testExpression(expr, !!value); |
| 62 | + testExpression(expr + ' == true', !!value); |
| 63 | + testExpression(expr + ' != true', !value); |
| 64 | + testExpression(expr + ' == false', !value); |
| 65 | + testExpression(expr + ' != false', !!value); |
| 66 | + testExpression(expr + ' == 5', value == <any>'5'); |
| 67 | + testExpression(expr + ' != 5', value != <any>'5'); |
| 68 | + testExpression('!' + expr, !value); |
| 69 | + } |
| 70 | + |
| 71 | + testBatch('a', true); |
| 72 | + testBatch('b', false); |
| 73 | + testBatch('c', '5'); |
| 74 | + testBatch('z', undefined); |
| 75 | + |
| 76 | + testExpression('a && !b', true && !false); |
| 77 | + testExpression('a && b', true && false); |
| 78 | + testExpression('a && !b && c == 5', true && !false && '5' == '5'); |
| 79 | + /* tslint:enable:triple-equals */ |
| 80 | + }); |
| 81 | +}); |
0 commit comments