More tests for binary operators
This commit is contained in:
@@ -310,8 +310,7 @@ ol.expr.Parser.prototype.parseBinaryExpression_ = function(lexer) {
|
|||||||
var right = this.parseUnaryExpression_(lexer);
|
var right = this.parseUnaryExpression_(lexer);
|
||||||
var stack = [left, operator, right];
|
var stack = [left, operator, right];
|
||||||
|
|
||||||
operator = lexer.peek();
|
precedence = this.binaryPrecedence_(lexer.peek());
|
||||||
precedence = this.binaryPrecedence_(operator);
|
|
||||||
while (precedence > 0) {
|
while (precedence > 0) {
|
||||||
// TODO: cache operator precedence in stack
|
// TODO: cache operator precedence in stack
|
||||||
while (stack.length > 2 &&
|
while (stack.length > 2 &&
|
||||||
@@ -321,11 +320,9 @@ ol.expr.Parser.prototype.parseBinaryExpression_ = function(lexer) {
|
|||||||
left = stack.pop();
|
left = stack.pop();
|
||||||
stack.push(this.createBinaryExpression_(operator.value, left, right));
|
stack.push(this.createBinaryExpression_(operator.value, left, right));
|
||||||
}
|
}
|
||||||
lexer.skip();
|
stack.push(lexer.next());
|
||||||
operator = lexer.peek();
|
|
||||||
precedence = this.binaryPrecedence_(operator);
|
|
||||||
stack.push(operator);
|
|
||||||
stack.push(this.parseUnaryExpression_(lexer));
|
stack.push(this.parseUnaryExpression_(lexer));
|
||||||
|
precedence = this.binaryPrecedence_(lexer.peek());
|
||||||
}
|
}
|
||||||
|
|
||||||
var i = stack.length - 1;
|
var i = stack.length - 1;
|
||||||
|
|||||||
@@ -225,6 +225,32 @@ describe('ol.expr.parse()', function() {
|
|||||||
expect(expr.evaluate({bar: 3})).to.be(1);
|
expect(expr.evaluate({bar: 3})).to.be(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('parses * in call argument', function() {
|
||||||
|
var lib = {
|
||||||
|
foo: function(arg) {
|
||||||
|
return arg;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var expr = ol.expr.parse('foo(2 * bar)');
|
||||||
|
expect(expr).to.be.a(ol.expr.Call);
|
||||||
|
expect(expr.evaluate({bar: 3}, lib)).to.be(6);
|
||||||
|
expect(expr.evaluate({bar: 4}, lib)).to.be(8);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses * in left side of comparison expression', function() {
|
||||||
|
var expr = ol.expr.parse('foo * 2 >bar');
|
||||||
|
expect(expr).to.be.a(ol.expr.Comparison);
|
||||||
|
expect(expr.evaluate({foo: 4, bar: 7})).to.be(true);
|
||||||
|
expect(expr.evaluate({foo: 4, bar: 8})).to.be(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses * in right side of comparison expression', function() {
|
||||||
|
var expr = ol.expr.parse('foo > 2 * bar');
|
||||||
|
expect(expr).to.be.a(ol.expr.Comparison);
|
||||||
|
expect(expr.evaluate({foo: 4, bar: 1})).to.be(true);
|
||||||
|
expect(expr.evaluate({foo: 4, bar: 2})).to.be(false);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('11.6 - additive operators', function() {
|
describe('11.6 - additive operators', function() {
|
||||||
@@ -254,6 +280,32 @@ describe('ol.expr.parse()', function() {
|
|||||||
expect(expr.evaluate({foo: 15})).to.be(5);
|
expect(expr.evaluate({foo: 15})).to.be(5);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('parses + in call argument', function() {
|
||||||
|
var lib = {
|
||||||
|
foo: function(arg) {
|
||||||
|
return arg;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var expr = ol.expr.parse('foo(2 + bar)');
|
||||||
|
expect(expr).to.be.a(ol.expr.Call);
|
||||||
|
expect(expr.evaluate({bar: 3}, lib)).to.be(5);
|
||||||
|
expect(expr.evaluate({bar: 4}, lib)).to.be(6);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses + in left side of comparison expression', function() {
|
||||||
|
var expr = ol.expr.parse('foo+2>bar');
|
||||||
|
expect(expr).to.be.a(ol.expr.Comparison);
|
||||||
|
expect(expr.evaluate({foo: 4, bar: 5})).to.be(true);
|
||||||
|
expect(expr.evaluate({foo: 4, bar: 6})).to.be(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses + in right side of comparison expression', function() {
|
||||||
|
var expr = ol.expr.parse('foo >2 +bar');
|
||||||
|
expect(expr).to.be.a(ol.expr.Comparison);
|
||||||
|
expect(expr.evaluate({foo: 4, bar: 1})).to.be(true);
|
||||||
|
expect(expr.evaluate({foo: 4, bar: 2})).to.be(false);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('11.7 - bitwise shift operators', function() {
|
describe('11.7 - bitwise shift operators', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user