Skip to content

Commit 345ef20

Browse files
committed
add tests
1 parent 10928c0 commit 345ef20

File tree

8 files changed

+239
-36
lines changed

8 files changed

+239
-36
lines changed

.babelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": [
3+
"es2015",
4+
"stage-0"
5+
],
6+
"plugins": [
7+
"transform-flow-strip-types",
8+
"add-module-exports"
9+
]
10+
}

dist/bundle.js

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
exports["TileCacheUrlFunction"] = factory(require("openlayers"));
1717
else
1818
root["ol"] = root["ol"] || {}, root["ol"]["TileCacheUrlFunction"] = factory(root["ol"]);
19-
})(this, function(__WEBPACK_EXTERNAL_MODULE_3__) {
19+
})(this, function(__WEBPACK_EXTERNAL_MODULE_4__) {
2020
return /******/ (function(modules) { // webpackBootstrap
2121
/******/ // The module cache
2222
/******/ var installedModules = {};
@@ -69,7 +69,7 @@ return /******/ (function(modules) { // webpackBootstrap
6969
value: true
7070
});
7171

72-
var _tileUrlFunction = __webpack_require__(1);
72+
var _tileUrlFunction = __webpack_require__(2);
7373

7474
var tileUrlFunction = _interopRequireWildcard(_tileUrlFunction);
7575

@@ -89,6 +89,87 @@ return /******/ (function(modules) { // webpackBootstrap
8989

9090
/***/ },
9191
/* 1 */
92+
/***/ function(module, exports) {
93+
94+
"use strict";
95+
96+
Object.defineProperty(exports, "__esModule", {
97+
value: true
98+
});
99+
100+
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
101+
102+
exports.calculateTileRangeForZ = calculateTileRangeForZ;
103+
exports.getTileCoordForXYAndResolution = getTileCoordForXYAndResolution;
104+
exports.getTileRangeHeight = getTileRangeHeight;
105+
/**
106+
* @param {ol.tilegrid.TileGrid} tileGrid
107+
* @param {ol.Extent} extent
108+
* @param {number} z
109+
* @return {{minX, minY, maxX, maxY}}
110+
*/
111+
function calculateTileRangeForZ(tileGrid, extent, z) {
112+
var resolution = tileGrid.getResolution(z);
113+
114+
var _getTileCoordForXYAnd = getTileCoordForXYAndResolution(tileGrid, extent[0], extent[1], resolution, false),
115+
_getTileCoordForXYAnd2 = _slicedToArray(_getTileCoordForXYAnd, 2),
116+
minX = _getTileCoordForXYAnd2[0],
117+
minY = _getTileCoordForXYAnd2[1];
118+
119+
var _getTileCoordForXYAnd3 = getTileCoordForXYAndResolution(tileGrid, extent[2], extent[3], resolution, true),
120+
_getTileCoordForXYAnd4 = _slicedToArray(_getTileCoordForXYAnd3, 2),
121+
maxX = _getTileCoordForXYAnd4[0],
122+
maxY = _getTileCoordForXYAnd4[1];
123+
124+
return { minX: minX, minY: minY, maxX: maxX, maxY: maxY };
125+
}
126+
127+
/**
128+
* @param {ol.tilegrid.TileGrid} tileGrid
129+
* @param {number} x
130+
* @param {number} y
131+
* @param {number} resolution
132+
* @param {boolean} reverseIntersectionPolicy
133+
* @return {number[]}
134+
*/
135+
function getTileCoordForXYAndResolution(tileGrid, x, y, resolution, reverseIntersectionPolicy) {
136+
var z = tileGrid.getZForResolution(resolution);
137+
var scale = resolution / tileGrid.getResolution(z);
138+
var origin = tileGrid.getOrigin(z);
139+
var tileSize = tileGrid.getTileSize(z);
140+
141+
if (!Array.isArray(tileSize)) {
142+
tileSize = [tileSize, tileSize];
143+
}
144+
145+
var adjustX = reverseIntersectionPolicy ? 0.5 : 0;
146+
var adjustY = reverseIntersectionPolicy ? 0 : 0.5;
147+
var xFromOrigin = Math.floor((x - origin[0]) / resolution + adjustX);
148+
var yFromOrigin = Math.floor((y - origin[1]) / resolution + adjustY);
149+
var tileCoordX = scale * xFromOrigin / tileSize[0];
150+
var tileCoordY = scale * yFromOrigin / tileSize[1];
151+
152+
if (reverseIntersectionPolicy) {
153+
tileCoordX = Math.ceil(tileCoordX) - 1;
154+
tileCoordY = Math.ceil(tileCoordY) - 1;
155+
} else {
156+
tileCoordX = Math.floor(tileCoordX);
157+
tileCoordY = Math.floor(tileCoordY);
158+
}
159+
160+
return [tileCoordX, tileCoordY];
161+
}
162+
163+
/**
164+
* @param {{minX, minY, maxX, maxY}} tileRange
165+
* @return {number}
166+
*/
167+
function getTileRangeHeight(tileRange) {
168+
return tileRange.maxY - tileRange.minY + 1;
169+
}
170+
171+
/***/ },
172+
/* 2 */
92173
/***/ function(module, exports, __webpack_require__) {
93174

94175
'use strict';
@@ -100,31 +181,38 @@ return /******/ (function(modules) { // webpackBootstrap
100181
exports.createTileUrlFunctionFromTemplate = createTileUrlFunctionFromTemplate;
101182
exports.createTileUrlFunctionFromTemplates = createTileUrlFunctionFromTemplates;
102183

103-
var _openlayers = __webpack_require__(3);
184+
var _openlayers = __webpack_require__(4);
104185

105186
var _openlayers2 = _interopRequireDefault(_openlayers);
106187

107-
var _util = __webpack_require__(2);
188+
var _util = __webpack_require__(3);
189+
190+
var _tileRange = __webpack_require__(1);
108191

109192
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
110193

111194
var zRegEx = /\{z\}/g;
195+
112196
var zPadRegEx = /\{0z\}/g;
113197
var xRegEx = /\{x\d?\}/g;
114198
var yRegEx = /\{y\d?\}/g;
115199
var dashYRegEx = /\{-y\d?\}/g;
116200

201+
var EPSG3857_EXTENT = [-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244];
202+
117203
/**
118204
* Basic create factory.
119205
*
120206
* @param {string} url Url template
121207
* @param {ol.tilegrid.TileGrid} [tileGrid] Tile grid.
208+
* @param {ol.Extent | number[]} [extent] Tile grid extent.
122209
* @returns {ol.TileUrlFunctionType}
123210
* @static
124211
* @public
125212
*/
126213
function createTileUrlFunction(url) {
127214
var tileGrid = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _openlayers2.default.tilegrid.createXYZ();
215+
var extent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : EPSG3857_EXTENT;
128216

129217
return createTileUrlFunctionFromTemplates(expandUrl(url), tileGrid);
130218
}
@@ -134,13 +222,15 @@ return /******/ (function(modules) { // webpackBootstrap
134222
*
135223
* @param {string} template Source url
136224
* @param {ol.tilegrid.TileGrid} [tileGrid] Tile grid.
225+
* @param {ol.Extent | number[]} [extent] Tile grid extent.
137226
* @returns {ol.TileUrlFunctionType}
138227
* @private
139228
*/
140229
function createTileUrlFunctionFromTemplate(template) {
141230
var tileGrid = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _openlayers2.default.tilegrid.createXYZ();
231+
var extent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : EPSG3857_EXTENT;
142232

143-
return(
233+
return (
144234
/**
145235
* @param {ol.TileCoord} tileCoord Tile Coordinate.
146236
* @return {string | undefined} Tile URL.
@@ -153,11 +243,9 @@ return /******/ (function(modules) { // webpackBootstrap
153243
return coordReplacer(y)(part);
154244
}).replace(dashYRegEx, function (part) {
155245
var z = tileCoord[0];
156-
var range = tileGrid.getFullTileRange(z);
157246
// The {-y} placeholder requires a tile grid with extent
158-
(0, _util.assert)(range, 'Tile grid with defined extent');
159-
160-
var y = range.getHeight() + tileCoord[2];
247+
var range = (0, _tileRange.calculateTileRangeForZ)(tileGrid, extent, z);
248+
var y = (0, _tileRange.getTileRangeHeight)(range) + tileCoord[2];
161249

162250
return coordReplacer(y)(part);
163251
});
@@ -171,11 +259,13 @@ return /******/ (function(modules) { // webpackBootstrap
171259
*
172260
* @param {string[]} templates Url templates
173261
* @param {ol.tilegrid.TileGrid} [tileGrid] Tile grid.
262+
* @param {ol.Extent | number[]} [extent] Tile grid extent.
174263
* @returns {ol.TileUrlFunctionType}
175264
* @private
176265
*/
177266
function createTileUrlFunctionFromTemplates(templates) {
178267
var tileGrid = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _openlayers2.default.tilegrid.createXYZ();
268+
var extent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : EPSG3857_EXTENT;
179269

180270
return createTileUrlFunctionFromTileUrlFunctions(templates.map(function (tileUrlFunction) {
181271
return createTileUrlFunctionFromTemplate(tileUrlFunction, tileGrid);
@@ -244,7 +334,7 @@ return /******/ (function(modules) { // webpackBootstrap
244334
return tileUrlFunctions[0];
245335
}
246336

247-
return(
337+
return (
248338
/**
249339
* @param {ol.TileCoord} tileCoord Tile Coordinate.
250340
* @param {number} pixelRatio Pixel ratio.
@@ -263,7 +353,7 @@ return /******/ (function(modules) { // webpackBootstrap
263353
}
264354

265355
/***/ },
266-
/* 2 */
356+
/* 3 */
267357
/***/ function(module, exports) {
268358

269359
"use strict";
@@ -321,10 +411,10 @@ return /******/ (function(modules) { // webpackBootstrap
321411
}
322412

323413
/***/ },
324-
/* 3 */
414+
/* 4 */
325415
/***/ function(module, exports) {
326416

327-
module.exports = __WEBPACK_EXTERNAL_MODULE_3__;
417+
module.exports = __WEBPACK_EXTERNAL_MODULE_4__;
328418

329419
/***/ }
330420
/******/ ])

dist/bundle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
zoom: 5
2929
}),
3030
layers: [
31+
new ol.layer.Tile({
32+
source: new ol.source.OSM(),
33+
}),
3134
new ol.layer.Tile({
3235
source: new ol.source.XYZ({
33-
tileUrlFunction: ol.TileCacheUrlFunction.createTileUrlFunction('http://tilecache_server/{0z}/{x1}/{x2}/{x3}/{-y1}/{-y2}/{-y3}.png')
36+
tileUrlFunction: ol.TileCacheUrlFunction.createTileUrlFunction('http://karta.tatar.ru/tilecache/satellite/{0z}/{x1}/{x2}/{x3}/{-y1}/{-y2}/{-y3}.png')
3437
})
3538
})
3639
]

package.json

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"release": "webpack --release --verbose",
88
"build": "webpack --build --verbose",
99
"serv": "webpack-dev-server",
10-
"test": "mocha 'tests/**/*.js'"
10+
"test": "mocha-phantomjs tests/index.html"
1111
},
1212
"homepage": "https://github.com/ghettovoice/ol3-tilecache",
1313
"repository": {
@@ -29,6 +29,7 @@
2929
"openlayers": ">= 3.14"
3030
},
3131
"devDependencies": {
32+
"babel-core": "^6.18.0",
3233
"babel-loader": "^6.2.7",
3334
"babel-plugin-add-module-exports": "^0.2.1",
3435
"babel-plugin-transform-flow-strip-types": "^6.18.0",
@@ -40,6 +41,7 @@
4041
"istanbul": "^0.4.5",
4142
"lodash": "^4.16.4",
4243
"mocha": "^3.1.2",
44+
"mocha-phantomjs": "^4.1.0",
4345
"openlayers": "^3.19.1",
4446
"progress-bar-webpack-plugin": "^1.9.0",
4547
"webpack": "^1.13.3",
@@ -51,15 +53,5 @@
5153
"dist",
5254
"src",
5355
"index.html"
54-
],
55-
"babel": {
56-
"presets": [
57-
"es2015",
58-
"stage-0"
59-
],
60-
"plugins": [
61-
"transform-flow-strip-types",
62-
"add-module-exports"
63-
]
64-
}
56+
]
6557
}

tests/index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<title>Mocha Tests</title>
5+
<link href="../node_modules/mocha/mocha.css" rel="stylesheet"/>
6+
<link href="../node_modules/openlayers/dist/ol.css" rel="stylesheet"/>
7+
</head>
8+
<body>
9+
<div id="map" style="display: none"></div>
10+
<div id="mocha"></div>
11+
12+
<script src="./polyfills.js"></script>
13+
<script src="../node_modules/mocha/mocha.js"></script>
14+
<script src="../node_modules/chai/chai.js"></script>
15+
<script src="../node_modules/openlayers/dist/ol.js"></script>
16+
<script src="../dist/bundle.js"></script>
17+
18+
<script>
19+
window.__coverage__ = true;
20+
21+
if (typeof initMochaPhantomJS === 'function') {
22+
initMochaPhantomJS()
23+
}
24+
25+
mocha.setup({
26+
ui: 'tdd',
27+
ignoreLeaks: false,
28+
globals: [ 'ol' ]
29+
});
30+
</script>
31+
<script src="./test.js"></script>
32+
<script>
33+
mocha.run();
34+
</script>
35+
</body>
36+
</html>

0 commit comments

Comments
 (0)