提交 d233bf34 编写于 作者: A Alex Dima 提交者: Eric Amodio

- Add tests

- RHS of `in` expression is not a value, so don't deserialize it like a value
- avoid unnecessary undefined check
- use `hasOwnProperty` for object case
- keys() should include the RHS of `in` expression
上级 2057d8ab
...@@ -17,6 +17,8 @@ STATIC_VALUES.set('isWindows', isWindows); ...@@ -17,6 +17,8 @@ STATIC_VALUES.set('isWindows', isWindows);
STATIC_VALUES.set('isWeb', isWeb); STATIC_VALUES.set('isWeb', isWeb);
STATIC_VALUES.set('isMacNative', isMacintosh && !isWeb); STATIC_VALUES.set('isMacNative', isMacintosh && !isWeb);
const hasOwnProperty = Object.prototype.hasOwnProperty;
export const enum ContextKeyExprType { export const enum ContextKeyExprType {
False = 0, False = 0,
True = 1, True = 1,
...@@ -138,7 +140,7 @@ export abstract class ContextKeyExpr { ...@@ -138,7 +140,7 @@ export abstract class ContextKeyExpr {
if (serializedOne.indexOf(' in ') >= 0) { if (serializedOne.indexOf(' in ') >= 0) {
let pieces = serializedOne.split(' in '); let pieces = serializedOne.split(' in ');
return ContextKeyInExpr.create(pieces[0].trim(), this._deserializeValue(pieces[1], strict)); return ContextKeyInExpr.create(pieces[0].trim(), pieces[1].trim());
} }
if (/^\!\s*/.test(serializedOne)) { if (/^\!\s*/.test(serializedOne)) {
...@@ -451,8 +453,8 @@ export class ContextKeyInExpr implements IContextKeyExpression { ...@@ -451,8 +453,8 @@ export class ContextKeyInExpr implements IContextKeyExpression {
return (source.indexOf(item) >= 0); return (source.indexOf(item) >= 0);
} }
if (typeof item === 'string' && typeof source === 'object' && source !== undefined && source !== null) { if (typeof item === 'string' && typeof source === 'object' && source !== null) {
return item in source; return hasOwnProperty.call(source, item);
} }
return false; return false;
} }
...@@ -462,7 +464,7 @@ export class ContextKeyInExpr implements IContextKeyExpression { ...@@ -462,7 +464,7 @@ export class ContextKeyInExpr implements IContextKeyExpression {
} }
public keys(): string[] { public keys(): string[] {
return [this.key]; return [this.key, this.valueKey];
} }
public map(mapFnc: IContextKeyExprMapper): ContextKeyInExpr { public map(mapFnc: IContextKeyExprMapper): ContextKeyInExpr {
......
...@@ -150,4 +150,19 @@ suite('ContextKeyExpr', () => { ...@@ -150,4 +150,19 @@ suite('ContextKeyExpr', () => {
t('a || b', 'c && d', 'a && c && d || b && c && d'); t('a || b', 'c && d', 'a && c && d || b && c && d');
t('a || b', 'c && d || e', 'a && e || b && e || a && c && d || b && c && d'); t('a || b', 'c && d || e', 'a && e || b && e || a && c && d || b && c && d');
}); });
test('ContextKeyInExpr', () => {
const ainb = ContextKeyExpr.deserialize('a in b')!;
assert.equal(ainb.evaluate(createContext({ 'a': 3, 'b': [3, 2, 1] })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 3, 'b': [1, 2, 3] })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 3, 'b': [1, 2] })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 3 })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 3, 'b': null })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': ['x'] })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': ['y'] })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': {} })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': { 'x': false } })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': { 'x': true } })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 'prototype', 'b': {} })), false);
});
}); });
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册