Cast to numbers for literals

There are times when we parse from XML without a schema (e.g. KML).  In these cases, features attributes will always be strings.  We can cast to number when creating literals from symbolizers and then assert `!isNaN` instead of asserting that they are numbers before.
This commit is contained in:
Tim Schaub
2013-08-23 17:30:24 -04:00
parent 43953c8efa
commit 3c993168c4
9 changed files with 233 additions and 37 deletions

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() {