From 5535a26d4a0bf03f3d0b5617e4a1d861f035a08a Mon Sep 17 00:00:00 2001 From: ahocevar Date: Sun, 3 Mar 2013 15:25:20 +0100 Subject: [PATCH] Adding unit tests for rule based styling --- test/spec/ol/layer/vectorlayer.test.js | 53 +++++++++++++++++++++ test/spec/ol/style/line.test.js | 25 ++++++++++ test/spec/ol/style/polygon.test.js | 25 ++++++++++ test/spec/ol/style/rule.test.js | 33 +++++++++++++ test/spec/ol/style/shape.test.js | 25 ++++++++++ test/spec/ol/style/style.test.js | 66 ++++++++++++++++++++++++++ 6 files changed, 227 insertions(+) create mode 100644 test/spec/ol/layer/vectorlayer.test.js create mode 100644 test/spec/ol/style/rule.test.js create mode 100644 test/spec/ol/style/style.test.js diff --git a/test/spec/ol/layer/vectorlayer.test.js b/test/spec/ol/layer/vectorlayer.test.js new file mode 100644 index 0000000000..1c5ccd4f79 --- /dev/null +++ b/test/spec/ol/layer/vectorlayer.test.js @@ -0,0 +1,53 @@ +goog.provide('ol.test.layer.Vector'); + +describe('ol.layer.Vector', function() { + + describe('#groupFeaturesBySymbolizerLiteral()', function() { + + it('groups equal symbolizers', function() { + var layer = new ol.layer.Vector({ + source: new ol.source.Vector({ + projection: ol.Projection.getFromCode('EPSG:4326') + }), + style: new ol.style.Style({ + rules: [ + new ol.style.Rule({ + symbolizers: [ + new ol.style.Line({ + strokeWidth: 2, + strokeStyle: new ol.Expression('colorProperty'), + opacity: 1 + }) + ] + }) + ] + }) + }); + var features = [ + new ol.Feature({ + g: new ol.geom.LineString([[-10, -10], [10, 10]]), + colorProperty: '#BADA55' + }), + new ol.Feature({ + g: new ol.geom.LineString([[-10, 10], [10, -10]]), + colorProperty: '#013' + }), + new ol.Feature({ + g: new ol.geom.LineString([[10, -10], [-10, -10]]), + colorProperty: '#013' + }) + ]; + + var groups = layer.groupFeaturesBySymbolizerLiteral(features); + expect(groups.length).toBe(2); + expect(groups[0][0].length).toBe(1); + expect(groups[0][1].strokeStyle).toBe('#BADA55'); + expect(groups[1][0].length).toBe(2); + expect(groups[1][1].strokeStyle).toBe('#013'); + + layer.dispose(); + }); + + }); + +}); diff --git a/test/spec/ol/style/line.test.js b/test/spec/ol/style/line.test.js index aac6cebe4a..e9957e036e 100644 --- a/test/spec/ol/style/line.test.js +++ b/test/spec/ol/style/line.test.js @@ -1,5 +1,30 @@ goog.provide('ol.test.style.Line'); +describe('ol.style.LineLiteral', function() { + + describe('#equals()', function() { + + it('identifies equal literals', function() { + var literal = new ol.style.LineLiteral({ + strokeWidth: 3, + strokeStyle: '#BADA55' + }); + var equalLiteral = new ol.style.LineLiteral({ + strokeStyle: '#BADA55', + strokeWidth: 3 + }); + var differentLiteral = new ol.style.LineLiteral({ + strokeStyle: '#013', + strokeWidth: 3 + }); + expect(literal.equals(equalLiteral)).toBe(true); + expect(literal.equals(differentLiteral)).toBe(false); + }); + + }); + +}); + describe('ol.style.Line', function() { describe('constructor', function() { diff --git a/test/spec/ol/style/polygon.test.js b/test/spec/ol/style/polygon.test.js index b4c4722a9f..3e7693bc85 100644 --- a/test/spec/ol/style/polygon.test.js +++ b/test/spec/ol/style/polygon.test.js @@ -1,5 +1,30 @@ goog.provide('ol.test.style.Polygon'); +describe('ol.style.PolygonLiteral', function() { + + describe('#equals()', function() { + + it('identifies equal literals', function() { + var literal = new ol.style.PolygonLiteral({ + strokeWidth: 3, + fillStyle: '#BADA55' + }); + var equalLiteral = new ol.style.PolygonLiteral({ + fillStyle: '#BADA55', + strokeWidth: 3 + }); + var differentLiteral = new ol.style.PolygonLiteral({ + fillStyle: '#013', + strokeWidth: 3 + }); + expect(literal.equals(equalLiteral)).toBe(true); + expect(literal.equals(differentLiteral)).toBe(false); + }); + + }); + +}); + describe('ol.style.Polygon', function() { describe('constructor', function() { diff --git a/test/spec/ol/style/rule.test.js b/test/spec/ol/style/rule.test.js new file mode 100644 index 0000000000..fe5fcbcca7 --- /dev/null +++ b/test/spec/ol/style/rule.test.js @@ -0,0 +1,33 @@ +goog.provide('ol.test.style.Rule'); + +describe('ol.style.Rule', function() { + + describe('#applies()', function() { + var feature = new ol.Feature(), + rule; + + it('returns true for a rule without filter', function() { + rule = new ol.style.Rule({}); + expect(rule.applies(feature)).toBe(true); + }); + + it('returns false when the rule does not apply', function() { + rule = new ol.style.Rule({ + filter: new ol.filter.Filter(function() { return false; }) + }); + expect(rule.applies(feature)).toBe(false); + }); + + it('returns true when the rule applies', function() { + rule = new ol.style.Rule({ + filter: new ol.filter.Filter(function() { return true; }) + }); + expect(rule.applies(feature)).toBe(true); + }); + }); + +}); + +goog.require('ol.Feature'); +goog.require('ol.filter.Filter'); +goog.require('ol.style.Rule'); diff --git a/test/spec/ol/style/shape.test.js b/test/spec/ol/style/shape.test.js index 26405624e6..fec06b87d0 100644 --- a/test/spec/ol/style/shape.test.js +++ b/test/spec/ol/style/shape.test.js @@ -1,5 +1,30 @@ goog.provide('ol.test.style.Shape'); +describe('ol.style.ShapeLiteral', function() { + + describe('#equals()', function() { + + it('identifies equal literals', function() { + var literal = new ol.style.ShapeLiteral({ + size: 4, + fillStyle: '#BADA55' + }); + var equalLiteral = new ol.style.ShapeLiteral({ + fillStyle: '#BADA55', + size: 4 + }); + var differentLiteral = new ol.style.ShapeLiteral({ + fillStyle: '#013', + size: 4 + }); + expect(literal.equals(equalLiteral)).toBe(true); + expect(literal.equals(differentLiteral)).toBe(false); + }); + + }); + +}); + describe('ol.style.Shape', function() { describe('constructor', function() { diff --git a/test/spec/ol/style/style.test.js b/test/spec/ol/style/style.test.js new file mode 100644 index 0000000000..3e93398b64 --- /dev/null +++ b/test/spec/ol/style/style.test.js @@ -0,0 +1,66 @@ +goog.provide('ol.test.style.Style'); + +describe('ol.style.Style', function() { + + describe('#apply()', function() { + + it('applies a style to a feature', function() { + var style = new ol.style.Style({ + rules: [ + new ol.style.Rule({ + filter: new ol.filter.Filter(function(feature) { + return feature.get('foo') == 'bar'; + }), + symbolizers: [ + new ol.style.Shape({ + size: 4, + fillStyle: '#BADA55' + }) + ] + }) + ] + }); + var feature = new ol.Feature(); + feature.set('foo', 'bar'); + expect(style.apply(feature).length).toBe(1); + expect(style.apply(feature)[0].fillStyle).toBe('#BADA55'); + feature.set('foo', 'baz'); + expect(style.apply(feature).length).toBe(0); + }); + + }); + + describe('ol.style.Style.applyDefaultStyle()', function() { + var feature = new ol.Feature(); + + it('returns an empty array for features without geometry', function() { + expect(ol.style.Style.applyDefaultStyle(feature).length).toBe(0); + }); + + it('returns an array with the Shape default for points', function() { + feature.setGeometry(new ol.geom.Point([0, 0])); + expect(ol.style.Style.applyDefaultStyle(feature)[0] + .equals(ol.style.ShapeDefaults)).toBe(true); + }); + + it('returns an array with the Line default for lines', function() { + feature.setGeometry(new ol.geom.LineString([[0, 0], [1, 1]])); + expect(ol.style.Style.applyDefaultStyle(feature)[0] + .equals(ol.style.LineDefaults)).toBe(true); + }); + + it('returns an array with the Polygon default for polygons', function() { + feature.setGeometry(new ol.geom.Polygon([[[0, 0], [1, 1], [0, 0]]])); + expect(ol.style.Style.applyDefaultStyle(feature)[0] + .equals(ol.style.PolygonDefaults)).toBe(true); + }); + + }); + +}); + +goog.require('ol.geom.LineString'); +goog.require('ol.geom.Point'); +goog.require('ol.geom.Polygon'); +goog.require('ol.filter.Filter'); +goog.require('ol.style.Style');