Provide ol.expression.parse method
This is the only method needed in the API.
This commit is contained in:
15
src/ol/expression/expression.js
Normal file
15
src/ol/expression/expression.js
Normal file
@@ -0,0 +1,15 @@
|
||||
goog.provide('ol.expression');
|
||||
|
||||
goog.require('ol.expression.Parser');
|
||||
|
||||
|
||||
/**
|
||||
* Parse an expression
|
||||
* @param {string} source The expression source (e.g. `'foo + 2'`).
|
||||
* @return {ol.expression.Expression} An expression instance that can be
|
||||
* evaluated within some scope to provide a value.
|
||||
*/
|
||||
ol.expression.parse = function(source) {
|
||||
var parser = new ol.expression.Parser();
|
||||
return parser.parse(source);
|
||||
};
|
||||
@@ -453,15 +453,3 @@ ol.expression.Parser.prototype.parseUnaryExpression_ = function(lexer) {
|
||||
ol.expression.Parser.prototype.parseExpression_ = function(lexer) {
|
||||
return this.parseBinaryExpression_(lexer);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Parse an expression
|
||||
* @param {string} source The expression source (e.g. `'foo + 2'`).
|
||||
* @return {ol.expression.Expression} An expression instance that can be
|
||||
* evaluated within some scope to provide a value.
|
||||
*/
|
||||
ol.expression.parse = function(source) {
|
||||
var parser = new ol.expression.Parser();
|
||||
return parser.parse(source);
|
||||
};
|
||||
|
||||
70
test/spec/ol/expression/expression.test.js
Normal file
70
test/spec/ol/expression/expression.test.js
Normal file
@@ -0,0 +1,70 @@
|
||||
goog.provide('ol.test.expression');
|
||||
|
||||
|
||||
describe('ol.expression.parse', function() {
|
||||
|
||||
it('parses a string and returns an expression', function() {
|
||||
var expr = ol.expression.parse('foo');
|
||||
expect(expr).to.be.a(ol.expression.Expression);
|
||||
});
|
||||
|
||||
describe('primary expressions', function() {
|
||||
// http://www.ecma-international.org/ecma-262/5.1/#sec-11.1
|
||||
|
||||
it('parses identifier expressions', function() {
|
||||
var expr = ol.expression.parse('foo');
|
||||
expect(expr).to.be.a(ol.expression.Identifier);
|
||||
expect(expr.evaluate({foo: 'bar'})).to.be('bar');
|
||||
});
|
||||
|
||||
it('throws on invalid identifier expressions', function() {
|
||||
expect(function() {
|
||||
ol.expression.parse('3foo');
|
||||
}).throwException();
|
||||
});
|
||||
|
||||
it('parses string literal expressions', function() {
|
||||
var expr = ol.expression.parse('"foo"');
|
||||
expect(expr).to.be.a(ol.expression.Literal);
|
||||
expect(expr.evaluate({})).to.be('foo');
|
||||
});
|
||||
|
||||
it('throws on unterminated string', function() {
|
||||
expect(function() {
|
||||
ol.expression.parse('"foo');
|
||||
}).throwException();
|
||||
});
|
||||
|
||||
it('parses numeric literal expressions', function() {
|
||||
var expr = ol.expression.parse('.42e+2');
|
||||
expect(expr).to.be.a(ol.expression.Literal);
|
||||
expect(expr.evaluate({})).to.be(42);
|
||||
});
|
||||
|
||||
it('throws on invalid number', function() {
|
||||
expect(function() {
|
||||
ol.expression.parse('.42eX');
|
||||
}).throwException();
|
||||
});
|
||||
|
||||
it('parses boolean literal expressions', function() {
|
||||
var expr = ol.expression.parse('false');
|
||||
expect(expr).to.be.a(ol.expression.Literal);
|
||||
expect(expr.evaluate({})).to.be(false);
|
||||
});
|
||||
|
||||
it('parses null literal expressions', function() {
|
||||
var expr = ol.expression.parse('null');
|
||||
expect(expr).to.be.a(ol.expression.Literal);
|
||||
expect(expr.evaluate({})).to.be(null);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
goog.require('ol.expression');
|
||||
goog.require('ol.expression.Expression');
|
||||
goog.require('ol.expression.Identifier');
|
||||
goog.require('ol.expression.Literal');
|
||||
Reference in New Issue
Block a user