Merge pull request #232 from tschaub/projection-tests

Projection tests
This commit is contained in:
Tim Schaub
2013-02-27 21:06:54 -08:00
2 changed files with 100 additions and 0 deletions

View File

@@ -266,6 +266,31 @@ ol.Projection.addTransform = function(source, destination, transformFn) {
};
/**
* Unregisters the conversion function to convert coordinates from the source
* projection to the destination projection. This method is used to clean up
* cached transforms during testing.
*
* @param {ol.Projection} source Source projection.
* @param {ol.Projection} destination Destination projection.
* @return {ol.TransformFunction} transformFn The unregistered transform.
*/
ol.Projection.removeTransform = function(source, destination) {
var sourceCode = source.getCode();
var destinationCode = destination.getCode();
var transforms = ol.Projection.transforms_;
goog.asserts.assert(sourceCode in transforms);
goog.asserts.assert(destinationCode in transforms[sourceCode]);
var transform = transforms[sourceCode][destinationCode];
delete transforms[sourceCode][destinationCode];
var keys = goog.object.getKeys(transforms[sourceCode]);
if (keys.length == 0) {
delete transforms[sourceCode];
}
return transform;
};
/**
* @param {string} code Code which is a combination of authority and identifier
* such as “EPSG:4326”.

View File

@@ -2,6 +2,27 @@ goog.provide('ol.test.Projection');
describe('ol.Projection', function() {
beforeEach(function() {
spyOn(ol.Projection, 'addTransform').andCallThrough();
});
afterEach(function() {
var argsForCall = ol.Projection.addTransform.argsForCall;
for (var i = 0, ii = argsForCall.length; i < ii; ++i) {
try {
ol.Projection.removeTransform.apply(
ol.Projection, argsForCall[i].splice(0, 2));
} catch (error) {
if (error instanceof goog.asserts.AssertionError) {
// The removeTransform function may have been called explicitly by the
// tests, so we pass.
} else {
throw error;
}
}
}
});
describe('projection equivalence', function() {
function _testAllEquivalent(codes) {
@@ -125,12 +146,64 @@ describe('ol.Projection', function() {
});
describe('ol.Projection.getTransform()', function() {
var sm = ol.Projection.getFromCode('GOOGLE');
var gg = ol.Projection.getFromCode('EPSG:4326');
it('returns a transform function', function() {
var transform = ol.Projection.getTransform(sm, gg);
expect(typeof transform).toBe('function');
var coordinate = transform(new ol.Coordinate(-12000000, 5000000));
expect(coordinate.x).toRoughlyEqual(-107.79783409434258, 1e-9);
expect(coordinate.y).toRoughlyEqual(40.91627447067577, 1e-9);
});
});
describe('ol.Projection.getTransformFromCodes()', function() {
it('returns a function', function() {
var transform = ol.Projection.getTransformFromCodes(
'GOOGLE', 'EPSG:4326');
expect(typeof transform).toBe('function');
});
it('returns a transform function', function() {
var transform = ol.Projection.getTransformFromCodes(
'GOOGLE', 'EPSG:4326');
expect(typeof transform).toBe('function');
var coordinate = transform(
new ol.Coordinate(-626172.13571216376, 6887893.4928337997));
expect(coordinate.x).toRoughlyEqual(-5.625, 1e-9);
expect(coordinate.y).toRoughlyEqual(52.4827802220782, 1e-9);
});
});
describe('ol.Projection.removeTransform()', function() {
var extent = new ol.Extent(-180, -90, 180, 90);
var units = ol.ProjectionUnits.DEGREES;
it('removes functions cached by addTransform', function() {
var foo = new ol.Projection('foo', units, extent);
var bar = new ol.Projection('bar', units, extent);
var transform = function() {};
ol.Projection.addTransform(foo, bar, transform);
expect(ol.Projection.transforms_).not.toBeUndefined();
expect(ol.Projection.transforms_.foo).not.toBeUndefined();
expect(ol.Projection.transforms_.foo.bar).toBe(transform);
var removed = ol.Projection.removeTransform(foo, bar);
expect(removed).toBe(transform);
expect(ol.Projection.transforms_.foo).toBeUndefined();
});
});
@@ -139,4 +212,6 @@ describe('ol.Projection', function() {
goog.require('goog.array');
goog.require('ol.Coordinate');
goog.require('ol.Extent');
goog.require('ol.Projection');
goog.require('ol.ProjectionUnits');