From 97b24965238089037590d8e9ba28df881e9ab98f Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Fri, 2 Mar 2018 12:09:37 -0500 Subject: [PATCH] Update integration test suite implementation --- test/ajax_stubs.js | 106 +++++++++++++++++++++++++++++++++++ test/expression.test.js | 4 +- test/query.test.js | 4 +- test/render.test.js | 4 +- test/stub_loader.js | 13 +++++ test/suite_implementation.js | 101 +++------------------------------ 6 files changed, 137 insertions(+), 95 deletions(-) create mode 100644 test/ajax_stubs.js create mode 100644 test/stub_loader.js diff --git a/test/ajax_stubs.js b/test/ajax_stubs.js new file mode 100644 index 00000000000..6d051859de5 --- /dev/null +++ b/test/ajax_stubs.js @@ -0,0 +1,106 @@ + +import {PNG} from 'pngjs'; +import request from 'request'; +// we're using a require hook to load this file instead of src/util/ajax.js, +// so we import browser module as if it were in an adjacent file +import browser from './browser'; +const cache = {}; + +/** + * The type of a resource. + * @private + * @readonly + * @enum {string} + */ +const ResourceType = { + Unknown: 'Unknown', + Style: 'Style', + Source: 'Source', + Tile: 'Tile', + Glyphs: 'Glyphs', + SpriteImage: 'SpriteImage', + SpriteJSON: 'SpriteJSON', + Image: 'Image' +}; +export { ResourceType }; + +if (typeof Object.freeze == 'function') { + Object.freeze(ResourceType); +} + +function cached(data, callback) { + setImmediate(() => { + callback(null, data); + }); +} + +export const getJSON = function({ url }, callback) { + if (cache[url]) return cached(cache[url], callback); + return request(url, (error, response, body) => { + if (!error && response.statusCode >= 200 && response.statusCode < 300) { + let data; + try { + data = JSON.parse(body); + } catch (err) { + return callback(err); + } + cache[url] = data; + callback(null, data); + } else { + callback(error || new Error(response.statusCode)); + } + }); +}; + +export const getArrayBuffer = function({ url }, callback) { + if (cache[url]) return cached(cache[url], callback); + return request({ url, encoding: null }, (error, response, body) => { + if (!error && response.statusCode >= 200 && response.statusCode < 300) { + cache[url] = {data: body}; + callback(null, {data: body}); + } else { + callback(error || new Error(response.statusCode)); + } + }); +}; + +export const getImage = function({ url }, callback) { + if (cache[url]) return cached(cache[url], callback); + return request({ url, encoding: null }, (error, response, body) => { + if (!error && response.statusCode >= 200 && response.statusCode < 300) { + new PNG().parse(body, (err, png) => { + if (err) return callback(err); + cache[url] = png; + callback(null, png); + }); + } else { + callback(error || {status: response.statusCode}); + } + }); +}; + +browser.getImageData = function({width, height, data}) { + return {width, height, data: new Uint8Array(data)}; +}; + +// Hack: since node doesn't have any good video codec modules, just grab a png with +// the first frame and fake the video API. +export const getVideo = function(urls, callback) { + return request({ url: urls[0], encoding: null }, (error, response, body) => { + if (!error && response.statusCode >= 200 && response.statusCode < 300) { + new PNG().parse(body, (err, png) => { + if (err) return callback(err); + callback(null, { + readyState: 4, // HAVE_ENOUGH_DATA + addEventListener: function() {}, + play: function() {}, + width: png.width, + height: png.height, + data: png.data + }); + }); + } else { + callback(error || new Error(response.statusCode)); + } + }); +}; diff --git a/test/expression.test.js b/test/expression.test.js index 50ed7527ffe..4aae3738ea0 100644 --- a/test/expression.test.js +++ b/test/expression.test.js @@ -1,6 +1,8 @@ 'use strict'; -require('flow-remove-types/register'); +require('../build/flow-remove-types.js'); +require = require("@std/esm")(module, true); + const expressionSuite = require('./integration').expression; const { createPropertyExpression } = require('../src/style-spec/expression'); const { toString } = require('../src/style-spec/expression/types'); diff --git a/test/query.test.js b/test/query.test.js index 6cbde8857ea..6a9154be136 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -1,6 +1,8 @@ 'use strict'; -require('flow-remove-types/register'); +require('./stub_loader'); +require('../build/flow-remove-types.js'); +require = require("@std/esm")(module, true); const querySuite = require('./integration').query; const suiteImplementation = require('./suite_implementation'); diff --git a/test/render.test.js b/test/render.test.js index 10c72cbf8b2..aaaf5d9f396 100644 --- a/test/render.test.js +++ b/test/render.test.js @@ -1,6 +1,8 @@ 'use strict'; -require('flow-remove-types/register'); +require('./stub_loader'); +require('../build/flow-remove-types.js'); +require = require("@std/esm")(module, true); const suite = require('./integration').render; const suiteImplementation = require('./suite_implementation'); diff --git a/test/stub_loader.js b/test/stub_loader.js new file mode 100644 index 00000000000..c5034ddfc0f --- /dev/null +++ b/test/stub_loader.js @@ -0,0 +1,13 @@ +// Load our stubbed ajax module for the integration suite implementation +const fs = require('fs'); +const assert = require('assert'); +const pirates = require('pirates'); + +pirates.addHook((code, filename) => { + assert(filename.endsWith('/ajax.js')); + return fs.readFileSync(`${__dirname}/ajax_stubs.js`, 'utf-8'); +}, { + exts: ['.js'], + matcher: filename => filename.endsWith('/ajax.js') +}); + diff --git a/test/suite_implementation.js b/test/suite_implementation.js index 41935e71e1c..703001590be 100644 --- a/test/suite_implementation.js +++ b/test/suite_implementation.js @@ -1,16 +1,12 @@ -'use strict'; - -const ajax = require('../src/util/ajax'); -const request = require('request'); -const PNG = require('pngjs').PNG; -const Map = require('../src/ui/map'); -const config = require('../src/util/config'); -const window = require('../src/util/window'); -const browser = require('../src/util/browser'); -const rtlTextPlugin = require('../src/source/rtl_text_plugin'); -const rtlText = require('@mapbox/mapbox-gl-rtl-text'); -const fs = require('fs'); -const path = require('path'); +import {PNG} from 'pngjs'; +import Map from '../src/ui/map'; +import config from '../src/util/config'; +import window from '../src/util/window'; +import browser from '../src/util/browser'; +import {plugin as rtlTextPlugin} from '../src/source/rtl_text_plugin'; +import rtlText from '@mapbox/mapbox-gl-rtl-text'; +import fs from 'fs'; +import path from 'path'; rtlTextPlugin['applyArabicShaping'] = rtlText.applyArabicShaping; rtlTextPlugin['processBidirectionalText'] = rtlText.processBidirectionalText; @@ -150,82 +146,3 @@ module.exports = function(style, options, _callback) { } } }; - -const cache = {}; - -function cached(data, callback) { - setImmediate(() => { - callback(null, data); - }); -} - -ajax.getJSON = function({ url }, callback) { - if (cache[url]) return cached(cache[url], callback); - return request(url, (error, response, body) => { - if (!error && response.statusCode >= 200 && response.statusCode < 300) { - let data; - try { - data = JSON.parse(body); - } catch (err) { - return callback(err); - } - cache[url] = data; - callback(null, data); - } else { - callback(error || new Error(response.statusCode)); - } - }); -}; - -ajax.getArrayBuffer = function({ url }, callback) { - if (cache[url]) return cached(cache[url], callback); - return request({ url, encoding: null }, (error, response, body) => { - if (!error && response.statusCode >= 200 && response.statusCode < 300) { - cache[url] = {data: body}; - callback(null, {data: body}); - } else { - callback(error || new Error(response.statusCode)); - } - }); -}; - -ajax.getImage = function({ url }, callback) { - if (cache[url]) return cached(cache[url], callback); - return request({ url, encoding: null }, (error, response, body) => { - if (!error && response.statusCode >= 200 && response.statusCode < 300) { - new PNG().parse(body, (err, png) => { - if (err) return callback(err); - cache[url] = png; - callback(null, png); - }); - } else { - callback(error || {status: response.statusCode}); - } - }); -}; - -browser.getImageData = function({width, height, data}) { - return {width, height, data: new Uint8Array(data)}; -}; - -// Hack: since node doesn't have any good video codec modules, just grab a png with -// the first frame and fake the video API. -ajax.getVideo = function(urls, callback) { - return request({ url: urls[0], encoding: null }, (error, response, body) => { - if (!error && response.statusCode >= 200 && response.statusCode < 300) { - new PNG().parse(body, (err, png) => { - if (err) return callback(err); - callback(null, { - readyState: 4, // HAVE_ENOUGH_DATA - addEventListener: function() {}, - play: function() {}, - width: png.width, - height: png.height, - data: png.data - }); - }); - } else { - callback(error || new Error(response.statusCode)); - } - }); -};