Integration tests for equality operators

This commit is contained in:
Tim Schaub
2013-06-11 18:22:50 -06:00
parent d5e133b7d8
commit 973606e67a
2 changed files with 99 additions and 1 deletions

View File

@@ -219,7 +219,13 @@ ol.expression.Parser.prototype.createUnaryExpression_ = function(op, expr) {
*/
ol.expression.Parser.prototype.parse = function(source) {
var lexer = new ol.expression.Lexer(source);
return this.parseExpression_(lexer);
var expr = this.parseExpression_(lexer);
var token = lexer.peek();
if (token.type !== ol.expression.TokenType.EOF) {
// TODO: token.index
throw new Error('Unexpected token: ' + token.value);
}
return expr;
};

View File

@@ -279,6 +279,98 @@ describe('ol.expression.parse', function() {
});
describe('equality operators', function() {
// http://www.ecma-international.org/ecma-262/5.1/#sec-11.9
it('parses == operator', function() {
var expr = ol.expression.parse('foo==42');
expect(expr).to.be.a(ol.expression.Comparison);
expect(expr.evaluate({foo: 42})).to.be(true);
expect(expr.evaluate({foo: 41})).to.be(false);
expect(expr.evaluate({foo: '42'})).to.be(true);
});
it('consumes whitespace as expected with ==', function() {
var expr = ol.expression.parse(' 42 ==foo ');
expect(expr).to.be.a(ol.expression.Comparison);
expect(expr.evaluate({foo: 42})).to.be(true);
expect(expr.evaluate({foo: 41})).to.be(false);
expect(expr.evaluate({foo: '42'})).to.be(true);
});
it('throws for invalid spacing with ==', function() {
expect(function() {
ol.expression.parse(' 10 = =foo ');
}).throwException();
});
it('parses != operator', function() {
var expr = ol.expression.parse('foo!=42');
expect(expr).to.be.a(ol.expression.Comparison);
expect(expr.evaluate({foo: 42})).to.be(false);
expect(expr.evaluate({foo: 41})).to.be(true);
expect(expr.evaluate({foo: '42'})).to.be(false);
});
it('consumes whitespace as expected with !=', function() {
var expr = ol.expression.parse(' 42 !=foo ');
expect(expr).to.be.a(ol.expression.Comparison);
expect(expr.evaluate({foo: 42})).to.be(false);
expect(expr.evaluate({foo: 41})).to.be(true);
expect(expr.evaluate({foo: '42'})).to.be(false);
});
it('throws for invalid spacing with !=', function() {
expect(function() {
ol.expression.parse(' 10! =foo ');
}).throwException();
});
it('parses === operator', function() {
var expr = ol.expression.parse('42===foo');
expect(expr).to.be.a(ol.expression.Comparison);
expect(expr.evaluate({foo: 42})).to.be(true);
expect(expr.evaluate({foo: 41})).to.be(false);
expect(expr.evaluate({foo: '42'})).to.be(false);
});
it('consumes whitespace as expected with ===', function() {
var expr = ol.expression.parse(' foo ===42 ');
expect(expr).to.be.a(ol.expression.Comparison);
expect(expr.evaluate({foo: 42})).to.be(true);
expect(expr.evaluate({foo: 41})).to.be(false);
expect(expr.evaluate({foo: '42'})).to.be(false);
});
it('throws for invalid spacing with ===', function() {
expect(function() {
ol.expression.parse(' 10 = == foo ');
}).throwException();
});
it('parses !== operator', function() {
var expr = ol.expression.parse('foo!==42');
expect(expr).to.be.a(ol.expression.Comparison);
expect(expr.evaluate({foo: 42})).to.be(false);
expect(expr.evaluate({foo: 41})).to.be(true);
expect(expr.evaluate({foo: '42'})).to.be(true);
});
it('consumes whitespace as expected with !==', function() {
var expr = ol.expression.parse(' 42 !== foo ');
expect(expr).to.be.a(ol.expression.Comparison);
expect(expr.evaluate({foo: 42})).to.be(false);
expect(expr.evaluate({foo: 41})).to.be(true);
expect(expr.evaluate({foo: '42'})).to.be(true);
});
it('throws for invalid spacing with !==', function() {
expect(function() {
ol.expression.parse(' 10 != = foo ');
}).throwException();
});
});
});