contextkey.test.ts 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import * as assert from 'assert';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';

10 11 12 13 14 15 16
function createContext(ctx: any) {
	return {
		getValue: (key: string) => {
			return ctx[key];
		}
	};
}
J
Joao Moreno 已提交
17

18 19 20 21 22 23
suite('ContextKeyExpr', () => {
	test('ContextKeyExpr.equals', function () {
		let a = ContextKeyExpr.and(
			ContextKeyExpr.has('a1'),
			ContextKeyExpr.and(ContextKeyExpr.has('and.a')),
			ContextKeyExpr.has('a2'),
24 25
			ContextKeyExpr.regex('d3', 'd.*'),
			ContextKeyExpr.regex('d4', '\\*\\*/3*'),
26 27 28 29 30 31 32 33 34 35 36
			ContextKeyExpr.equals('b1', 'bb1'),
			ContextKeyExpr.equals('b2', 'bb2'),
			ContextKeyExpr.notEquals('c1', 'cc1'),
			ContextKeyExpr.notEquals('c2', 'cc2'),
			ContextKeyExpr.not('d1'),
			ContextKeyExpr.not('d2')
		);
		let b = ContextKeyExpr.and(
			ContextKeyExpr.equals('b2', 'bb2'),
			ContextKeyExpr.notEquals('c1', 'cc1'),
			ContextKeyExpr.not('d1'),
37
			ContextKeyExpr.regex('d4', '\\*\\*/3*'),
38 39 40
			ContextKeyExpr.notEquals('c2', 'cc2'),
			ContextKeyExpr.has('a2'),
			ContextKeyExpr.equals('b1', 'bb1'),
41
			ContextKeyExpr.regex('d3', 'd.*'),
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
			ContextKeyExpr.has('a1'),
			ContextKeyExpr.and(ContextKeyExpr.equals('and.a', true)),
			ContextKeyExpr.not('d2')
		);
		assert(a.equals(b), 'expressions should be equal');
	});

	test('normalize', function () {
		let key1IsTrue = ContextKeyExpr.equals('key1', true);
		let key1IsNotFalse = ContextKeyExpr.notEquals('key1', false);
		let key1IsFalse = ContextKeyExpr.equals('key1', false);
		let key1IsNotTrue = ContextKeyExpr.notEquals('key1', true);

		assert.ok(key1IsTrue.normalize().equals(ContextKeyExpr.has('key1')));
		assert.ok(key1IsNotFalse.normalize().equals(ContextKeyExpr.has('key1')));
		assert.ok(key1IsFalse.normalize().equals(ContextKeyExpr.not('key1')));
		assert.ok(key1IsNotTrue.normalize().equals(ContextKeyExpr.not('key1')));
	});

	test('evaluate', function () {
		/* tslint:disable:triple-equals */
J
Joao Moreno 已提交
63
		let context = createContext({
64 65
			'a': true,
			'b': false,
C
chrisdias 已提交
66 67
			'c': '5',
			'd': 'd'
J
Joao Moreno 已提交
68
		});
69
		function testExpression(expr: string, expected: boolean): void {
70
			// console.log(expr + ' ' + expected);
71 72 73 74 75 76 77 78 79 80 81 82
			let rules = ContextKeyExpr.deserialize(expr);
			assert.equal(rules.evaluate(context), expected, expr);
		}
		function testBatch(expr: string, value: any): void {
			testExpression(expr, !!value);
			testExpression(expr + ' == true', !!value);
			testExpression(expr + ' != true', !value);
			testExpression(expr + ' == false', !value);
			testExpression(expr + ' != false', !!value);
			testExpression(expr + ' == 5', value == <any>'5');
			testExpression(expr + ' != 5', value != <any>'5');
			testExpression('!' + expr, !value);
83
			testExpression(expr + ' =~ d.*', /d.*/.test(value));
84 85 86 87 88
		}

		testBatch('a', true);
		testBatch('b', false);
		testBatch('c', '5');
C
chrisdias 已提交
89
		testBatch('d', 'd');
90 91 92 93 94 95 96 97
		testBatch('z', undefined);

		testExpression('a && !b', true && !false);
		testExpression('a && b', true && false);
		testExpression('a && !b && c == 5', true && !false && '5' == '5');
		/* tslint:enable:triple-equals */
	});
});