Adding unit tests for rule based styling

This commit is contained in:
ahocevar
2013-03-03 15:25:20 +01:00
parent 63c048edb2
commit 5535a26d4a
6 changed files with 227 additions and 0 deletions

View File

@@ -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();
});
});
});

View File

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

View File

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

View File

@@ -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');

View File

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

View File

@@ -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');