From c4a7f899dbb731be468cb4c1908fc84c76af34b0 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 26 Aug 2014 21:36:04 -0700 Subject: [PATCH] Fix tests for `rhino -require`. --- test/asset/set.js | 191 +++++++++++++++------------- test/asset/weakmap.js | 107 +++++++++------- test/test.js | 54 ++++---- vendor/qunit-extras/qunit-extras.js | 4 - 4 files changed, 191 insertions(+), 165 deletions(-) diff --git a/test/asset/set.js b/test/asset/set.js index 893291aa1..256ee684a 100644 --- a/test/asset/set.js +++ b/test/asset/set.js @@ -12,6 +12,9 @@ /** Used as a reference to the global object */ var root = (objectTypes[typeof window] && window) || this; + /** Detect free variable `exports` */ + var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports; + /** Detect free variable `global` from Node.js or Browserified code and use it as `root` */ var freeGlobal = objectTypes[typeof global] && global; if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal || freeGlobal.self === freeGlobal)) { @@ -21,113 +24,123 @@ /*--------------------------------------------------------------------------*/ /** - * Creates a `Set` object. - */ - function Set() { - this.__cache__ = {}; - } - - /*--------------------------------------------------------------------------*/ - - /** - * Gets the index at which the first occurrence of `value` is found using - * strict equality for comparisons, i.e. `===`. + * Installs `Set` on the given `context` object. * - * @private - * @param {Array} array The array to search. - * @param {*} value The value to search for. - * @returns {number} Returns the index of the matched value or `-1`. + * @memberOf exports + * @param {Object} context The context object. */ - function indexOf(array, value) { - var index = -1, - length = array.length; + function runInContext(context) { - while (++index < length) { - if (array[index] === value) { - return index; + /** + * Creates a `Set` object. + */ + function Set() { + this.__cache__ = {}; + } + + /** + * Gets the index at which the first occurrence of `value` is found using + * strict equality for comparisons, i.e. `===`. + * + * @private + * @param {Array} array The array to search. + * @param {*} value The value to search for. + * @returns {number} Returns the index of the matched value or `-1`. + */ + function indexOf(array, value) { + var index = -1, + length = array.length; + + while (++index < length) { + if (array[index] === value) { + return index; + } } + return -1; } - return -1; - } - /*--------------------------------------------------------------------------*/ + /** + * Checks if `value` is in the set. + * + * @memberOf Set + * @param {*} value The value to search for. + * @returns {boolean} Returns `true` if `value` is found, else `false`. + */ + function has(value) { + var type = typeof value, + cache = this.__cache__; - /** - * Checks if `value` is in the set. - * - * @memberOf Set - * @param {*} value The value to search for. - * @returns {boolean} Returns `true` if `value` is found, else `false`. - */ - function has(value) { - var type = typeof value, - cache = this.__cache__; - - if (type == 'boolean' || value == null) { - return cache[value] || false; - } - if (type != 'number' && type != 'string') { - type = 'object'; - } - var key = type == 'number' ? value : '_' + value; - cache = (cache = cache[type]) && cache[key]; - - return type == 'object' - ? (cache && indexOf(cache, value) > -1 ? true : false) - : (cache || false); - } - - /** - * Adds `value` to the set. - * - * @memberOf Set - * @param {*} value The value to add. - */ - function add(value) { - var cache = this.__cache__, - type = typeof value; - - if (type == 'boolean' || value == null) { - cache[value] = true; - } else { + if (type == 'boolean' || value == null) { + return cache[value] || false; + } if (type != 'number' && type != 'string') { type = 'object'; } - var key = type == 'number' ? value : '_' + value, - typeCache = cache[type] || (cache[type] = {}); + var key = type == 'number' ? value : '_' + value; + cache = (cache = cache[type]) && cache[key]; - if (type == 'object') { - var array = typeCache[key]; - if (array) { - array.push(value); - } else { - typeCache[key] = [value]; - } + return type == 'object' + ? (cache && indexOf(cache, value) > -1 ? true : false) + : (cache || false); + } + + /** + * Adds `value` to the set. + * + * @memberOf Set + * @param {*} value The value to add. + */ + function add(value) { + var cache = this.__cache__, + type = typeof value; + + if (type == 'boolean' || value == null) { + cache[value] = true; } else { - typeCache[key] = true; + if (type != 'number' && type != 'string') { + type = 'object'; + } + var key = type == 'number' ? value : '_' + value, + typeCache = cache[type] || (cache[type] = {}); + + if (type == 'object') { + var array = typeCache[key]; + if (array) { + array.push(value); + } else { + typeCache[key] = [value]; + } + } else { + typeCache[key] = true; + } } } - } - /** - * Produces the `toString` result of `Set`. - * - * @static - * @memberOf Set - * @returns {string} Returns the string result. - */ - function toString() { - return nativeString; + /** + * Produces the `toString` result of `Set`. + * + * @static + * @memberOf Set + * @returns {string} Returns the string result. + */ + function toString() { + return nativeString; + } + + Set.toString = toString; + Set.prototype.add = add; + Set.prototype.has = has; + + if (!root.Set) { + context.Set = Set; + } } /*--------------------------------------------------------------------------*/ - Set.toString = toString; - Set.prototype.add = add; - Set.prototype.has = has; - - // expose `Set` - if (!root.Set) { - root.Set = Set; + if (freeExports) { + freeExports.runInContext = runInContext; + } else { + runInContext(root); } }.call(this)); diff --git a/test/asset/weakmap.js b/test/asset/weakmap.js index 01d02be00..06040e664 100644 --- a/test/asset/weakmap.js +++ b/test/asset/weakmap.js @@ -12,6 +12,9 @@ /** Used as a reference to the global object */ var root = (objectTypes[typeof window] && window) || this; + /** Detect free variable `exports` */ + var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports; + /** Detect free variable `global` from Node.js or Browserified code and use it as `root` */ var freeGlobal = objectTypes[typeof global] && global; if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal || freeGlobal.self === freeGlobal)) { @@ -21,56 +24,68 @@ /*--------------------------------------------------------------------------*/ /** - * Creates a `WeakMap` object. + * Installs `WeakMap` on the given `context` object. + * + * @memberOf exports + * @param {Object} context The context object. */ - function WeakMap() { - // no-op + function runInContext(context) { + + /** + * Creates a `WeakMap` object. + */ + function WeakMap() { + // no-op + } + + /** + * Gets the value associated with the given key. + * + * @memberOf WeakMap + * @param {Object} key The key object. + * @returns {*} Returns the associated value, else `undefined`. + */ + function get(key) { + return key.__weakmap__; + } + + /** + * Sets a value for the given key. + * + * @memberOf WeakMap + * @param {Object} key The key object. + * @param {*} value The value to set. + */ + function set(key, value) { + key.__weakmap__ = value; + return this; + } + + /** + * Produces the `toString` result of `WeakMap`. + * + * @static + * @memberOf WeakMap + * @returns {string} Returns the string result. + */ + function toString() { + return nativeString; + } + + WeakMap.toString = toString; + WeakMap.prototype.get = get; + WeakMap.prototype.set = set; + + if (!root.WeakMap) { + context.WeakMap = WeakMap; + } } /*--------------------------------------------------------------------------*/ - /** - * Gets the value associated with the given key. - * - * @memberOf WeakMap - * @param {Object} key The key object. - * @returns {*} Returns the associated value, else `undefined`. - */ - function get(key) { - return key.__weakmap__; - } - - /** - * Sets a value for the given key. - * - * @memberOf WeakMap - * @param {Object} key The key object. - * @param {*} value The value to set. - */ - function set(key, value) { - key.__weakmap__ = value; - return this; - } - - /** - * Produces the `toString` result of `WeakMap`. - * - * @static - * @memberOf WeakMap - * @returns {string} Returns the string result. - */ - function toString() { - return nativeString; - } - - /*--------------------------------------------------------------------------*/ - - WeakMap.toString = toString; - WeakMap.prototype.get = get; - WeakMap.prototype.set = set; - - // expose `WeakMap` - if (!root.WeakMap) { - root.WeakMap = WeakMap; + if (freeExports) { + freeExports.runInContext = runInContext; + } else { + runInContext(root); } }.call(this)); diff --git a/test/test.js b/test/test.js index 4e05f9b35..73cf30e3d 100644 --- a/test/test.js +++ b/test/test.js @@ -117,10 +117,6 @@ ? require : (isJava && root.load) || noop; - /** Load ES6 Set and WeakMap shims */ - load('./asset/set.js'); - load('./asset/weakmap.js'); - /** The unit testing framework */ var QUnit = (function() { return root.QUnit || ( @@ -132,11 +128,24 @@ ); }()); - /** Load and install QUnit Extras */ - var qe = load('../vendor/qunit-extras/qunit-extras.js'); - if (qe) { - qe.runInContext(root); - } + /** Load and install QUnit Extras and ES6 Set/WeakMap shims */ + (function() { + var paths = [ + './asset/set.js', + './asset/weakmap.js', + '../vendor/qunit-extras/qunit-extras.js' + ]; + + var index = -1, + length = paths.length; + + while (++index < length) { + var object = load(paths[index]); + if (object) { + object.runInContext(root); + } + } + }()); /*--------------------------------------------------------------------------*/ @@ -613,7 +622,7 @@ } }); - test('should avoid overwritten native methods', 15, function() { + test('should avoid overwritten native methods', 14, function() { function Foo() {} function message(lodashMethod, nativeMethod) { @@ -647,13 +656,6 @@ ok(actual[0] instanceof Foo, message('_.create', 'Object.create')); deepEqual(actual[1], {}, message('_.create', 'Object.create')); - try { - actual = lodashBizarro.curry(function(a, b) { return [a, b]; })(1)(2); - } catch(e) { - actual = null; - } - deepEqual(actual, [1, 2], message('_.curry', 'Object.defineProperty')); - try { actual = [lodashBizarro.isPlainObject({}), lodashBizarro.isPlainObject([])]; } catch(e) { @@ -722,7 +724,7 @@ } } else { - skipTest(15); + skipTest(14); } }); }()); @@ -2210,19 +2212,19 @@ var object = {}; - if ((defineProperty && !WeakMap) && _.support.funcDecomp) { - _.callback(a, object); + if (lodashBizarro && lodashBizarro.support.funcDecomp) { + lodashBizarro.callback(a, object); ok(EXPANDO in a); - _.callback(b, object); + lodashBizarro.callback(b, object); ok(!(EXPANDO in b)); - if (_.support.funcNames) { - _.support.funcNames = false; - _.callback(c, object); + if (lodashBizarro.support.funcNames) { + lodashBizarro.support.funcNames = false; + lodashBizarro.callback(c, object); ok(EXPANDO in c); - _.support.funcNames = true; + lodashBizarro.support.funcNames = true; } else { skipTest(); @@ -2236,7 +2238,7 @@ test('should not write metadata when `_.support.funcDecomp` is `false`', 1, function() { function a() {}; - if ((defineProperty && !WeakMap) && lodashBizarro) { + if (lodashBizarro) { lodashBizarro.callback(a, {}); ok(!(EXPANDO in a)); } diff --git a/vendor/qunit-extras/qunit-extras.js b/vendor/qunit-extras/qunit-extras.js index e8ad1062e..fda9a4e1c 100644 --- a/vendor/qunit-extras/qunit-extras.js +++ b/vendor/qunit-extras/qunit-extras.js @@ -177,10 +177,6 @@ * @param {Object} context The context object. */ function runInContext(context) { - // exit early if no `context` is provided or if `QUnit` does not exist - if (!context || !context.QUnit) { - return; - } /** Used to report the test module for failing tests */ var moduleName,