Merge pull request #920 from tschaub/point-examples

Cast to number when creating literals from symbolizers where literal properties must be numeric.
This commit is contained in:
Tim Schaub
2013-08-26 11:57:09 -07:00
17 changed files with 11226 additions and 53 deletions

View File

@@ -628,6 +628,52 @@ describe('ol.expr.lib', function() {
var parse = ol.expr.parse;
var evaluate = ol.expr.evaluateFeature;
describe('concat()', function() {
var feature = new ol.Feature({
str: 'bar',
num: 42,
bool: false,
nul: null
});
it('concatenates strings', function() {
expect(evaluate(parse('concat(str, "after")'), feature))
.to.be('barafter');
expect(evaluate(parse('concat("before", str)'), feature))
.to.be('beforebar');
expect(evaluate(parse('concat("a", str, "b")'), feature))
.to.be('abarb');
});
it('concatenates numbers as strings', function() {
expect(evaluate(parse('concat(num, 0)'), feature))
.to.be('420');
expect(evaluate(parse('concat(0, num)'), feature))
.to.be('042');
expect(evaluate(parse('concat(42, 42)'), feature))
.to.be('4242');
expect(evaluate(parse('concat(str, num)'), feature))
.to.be('bar42');
});
it('concatenates booleans as strings', function() {
expect(evaluate(parse('concat(bool, "foo")'), feature))
.to.be('falsefoo');
expect(evaluate(parse('concat(true, str)'), feature))
.to.be('truebar');
expect(evaluate(parse('concat(true, false)'), feature))
.to.be('truefalse');
});
it('concatenates nulls as strings', function() {
expect(evaluate(parse('concat(nul, "foo")'), feature))
.to.be('nullfoo');
expect(evaluate(parse('concat(str, null)'), feature))
.to.be('barnull');
});
});
describe('extent()', function() {
var nw = new ol.Feature({

View File

@@ -235,6 +235,8 @@ describe('ol.parser.ogc.Filter_v1_0_0', function() {
});
describe('_expression reader', function() {
var evaluate = ol.expr.evaluateFeature;
it('handles combined propertyname and text', function() {
var xml = '<ogc:UpperBoundary xmlns:ogc="' +
'http://www.opengis.net/ogc">10</ogc:UpperBoundary>';
@@ -247,7 +249,7 @@ describe('ol.parser.ogc.Filter_v1_0_0', function() {
xml = '<ogc:UpperBoundary xmlns:ogc="http://www.opengis.net/ogc">' +
'foo<ogc:PropertyName>x</ogc:PropertyName>bar</ogc:UpperBoundary>';
expr = reader.call(parser, goog.dom.xml.loadXml(xml).documentElement);
expect(expr.evaluate({x: 4})).to.eql('foo4bar');
expect(evaluate(expr, new ol.Feature({x: 4}))).to.eql('foo4bar');
});
it('handles combined propertyname and literal', function() {
@@ -258,7 +260,7 @@ describe('ol.parser.ogc.Filter_v1_0_0', function() {
'<ogc:PropertyName>x</ogc:PropertyName>' +
'<ogc:Literal>foo</ogc:Literal></ogc:UpperBoundary>';
var expr = reader.call(parser, goog.dom.xml.loadXml(xml).documentElement);
expect(expr.evaluate({x: 42})).to.eql('bar42foo');
expect(evaluate(expr, new ol.Feature({x: 42}))).to.eql('bar42foo');
});
});
@@ -266,6 +268,7 @@ describe('ol.parser.ogc.Filter_v1_0_0', function() {
});
goog.require('goog.dom.xml');
goog.require('ol.Feature');
goog.require('ol.expr');
goog.require('ol.expr.Call');
goog.require('ol.expr.Comparison');

View File

@@ -64,6 +64,23 @@ describe('ol.style.Fill', function() {
expect(literal.fillOpacity).to.be(0.8);
});
it('casts opacity to number', function() {
var symbolizer = new ol.style.Fill({
opacity: ol.expr.parse('opacity'),
color: '#ff00ff'
});
var feature = new ol.Feature({
opacity: '0.55',
geometry: new ol.geom.Polygon(
[[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]])
});
var literal = symbolizer.createLiteral(feature);
expect(literal).to.be.a(ol.style.PolygonLiteral);
expect(literal.fillOpacity).to.be(0.55);
});
});
describe('#getColor()', function() {

View File

@@ -75,19 +75,82 @@ describe('ol.style.Icon', function() {
expect(literal.url).to.be('http://example.com/1.png');
});
it('applies default type if none provided', function() {
it('applies default opacity if none provided', function() {
var symbolizer = new ol.style.Icon({
height: ol.expr.parse('10'),
width: ol.expr.parse('20'),
url: ol.expr.parse('"http://example.com/1.png"')
height: 10,
width: 20,
url: 'http://example.com/1.png'
});
var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT);
expect(literal).to.be.a(ol.style.IconLiteral);
expect(literal.opacity).to.be(1);
});
it('applies default rotation if none provided', function() {
var symbolizer = new ol.style.Icon({
height: 10,
width: 20,
url: 'http://example.com/1.png'
});
var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT);
expect(literal).to.be.a(ol.style.IconLiteral);
expect(literal.rotation).to.be(0);
});
it('casts opacity to number', function() {
var symbolizer = new ol.style.Icon({
opacity: ol.expr.parse('opacity'),
height: 10,
width: 20,
url: 'http://example.com/1.png'
});
var feature = new ol.Feature({
opacity: '0.53',
geometry: new ol.geom.Point([1, 2])
});
var literal = symbolizer.createLiteral(feature);
expect(literal).to.be.a(ol.style.IconLiteral);
expect(literal.opacity).to.be(0.53);
});
it('casts width to number', function() {
var symbolizer = new ol.style.Icon({
width: ol.expr.parse('width'),
height: 10,
url: 'http://example.com/1.png'
});
var feature = new ol.Feature({
width: '42',
geometry: new ol.geom.Point([1, 2])
});
var literal = symbolizer.createLiteral(feature);
expect(literal).to.be.a(ol.style.IconLiteral);
expect(literal.width).to.be(42);
});
it('casts height to number', function() {
var symbolizer = new ol.style.Icon({
height: ol.expr.parse('height'),
width: 10,
url: 'http://example.com/1.png'
});
var feature = new ol.Feature({
height: '42',
geometry: new ol.geom.Point([1, 2])
});
var literal = symbolizer.createLiteral(feature);
expect(literal).to.be.a(ol.style.IconLiteral);
expect(literal.height).to.be(42);
});
});
describe('#getHeight()', function() {

View File

@@ -71,6 +71,95 @@ describe('ol.style.Shape', function() {
expect(literal.strokeWidth).to.be(2);
});
it('casts size to number', function() {
var symbolizer = new ol.style.Shape({
size: ol.expr.parse('size'),
fill: new ol.style.Fill({
color: '#BADA55'
}),
stroke: new ol.style.Stroke({
color: '#013',
opacity: 1,
width: 2
})
});
var feature = new ol.Feature({
size: '42',
geometry: new ol.geom.Point([1, 2])
});
var literal = symbolizer.createLiteral(feature);
expect(literal).to.be.a(ol.style.ShapeLiteral);
expect(literal.size).to.be(42);
});
it('casts stroke width to number', function() {
var symbolizer = new ol.style.Shape({
fill: new ol.style.Fill({
color: '#BADA55'
}),
stroke: new ol.style.Stroke({
color: '#013',
opacity: 1,
width: ol.expr.parse('strokeWidth')
})
});
var feature = new ol.Feature({
strokeWidth: '4.2',
geometry: new ol.geom.Point([1, 2])
});
var literal = symbolizer.createLiteral(feature);
expect(literal).to.be.a(ol.style.ShapeLiteral);
expect(literal.strokeWidth).to.be(4.2);
});
it('casts stroke opacity to number', function() {
var symbolizer = new ol.style.Shape({
fill: new ol.style.Fill({
color: '#BADA55'
}),
stroke: new ol.style.Stroke({
color: '#013',
opacity: ol.expr.parse('strokeOpacity'),
width: 3
})
});
var feature = new ol.Feature({
strokeOpacity: '.2',
geometry: new ol.geom.Point([1, 2])
});
var literal = symbolizer.createLiteral(feature);
expect(literal).to.be.a(ol.style.ShapeLiteral);
expect(literal.strokeOpacity).to.be(0.2);
});
it('casts fill opacity to number', function() {
var symbolizer = new ol.style.Shape({
fill: new ol.style.Fill({
opacity: ol.expr.parse('fillOpacity'),
color: '#BADA55'
}),
stroke: new ol.style.Stroke({
color: '#013',
width: 3
})
});
var feature = new ol.Feature({
fillOpacity: '.42',
geometry: new ol.geom.Point([1, 2])
});
var literal = symbolizer.createLiteral(feature);
expect(literal).to.be.a(ol.style.ShapeLiteral);
expect(literal.fillOpacity).to.be(0.42);
});
});
describe('#getFill()', function() {

View File

@@ -74,7 +74,7 @@ describe('ol.style.Text', function() {
expect(literal.opacity).to.be(0.6);
});
it('applies default type if none provided', function() {
it('applies defaults if none provided', function() {
var symbolizer = new ol.style.Text({
text: 'Test'
});
@@ -88,6 +88,34 @@ describe('ol.style.Text', function() {
expect(literal.opacity).to.be(1);
});
it('casts size to number', function() {
var symbolizer = new ol.style.Text({
text: 'test',
fontSize: ol.expr.parse('size')
});
var feature = new ol.Feature({
size: '42'
});
var literal = symbolizer.createLiteral(feature);
expect(literal.fontSize).to.be(42);
});
it('casts opacity to number', function() {
var symbolizer = new ol.style.Text({
text: 'test',
opacity: ol.expr.parse('opacity')
});
var feature = new ol.Feature({
opacity: '0.42'
});
var literal = symbolizer.createLiteral(feature);
expect(literal.opacity).to.be(0.42);
});
});
describe('#getColor()', function() {