From 653aab235841f09cf3665c8f1de3d4c5c2331244 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 21 Jul 2014 23:10:40 -0700 Subject: [PATCH] Fix typo in the ArrayBuffer#slice mock for node. --- test/index.html | 33 +++++++++++++------------- test/test.js | 63 ++++++++++++++++++++++++++----------------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/test/index.html b/test/index.html index 746662e37..d7904135f 100644 --- a/test/index.html +++ b/test/index.html @@ -113,38 +113,39 @@ document.createDocumentFragment = function() {}; setProperty(window, '_ArrayBuffer', window.ArrayBuffer); - if (window.ArrayBuffer && window.Uint8Array && !new ArrayBuffer(0).slice) { + if (window.ArrayBuffer && window.Uint8Array) { ArrayBuffer = (function(_ArrayBuffer) { + function ArrayBuffer(byteLength) { + var buffer = new _ArrayBuffer(byteLength); + if (!byteLength) { + setProperty(buffer, 'slice', buffer.slice ? null : bufferSlice); + } + return buffer; + } + function bufferSlice() { + var newBuffer = new _ArrayBuffer(this.byteLength), + view = new Uint8Array(newBuffer); + + view.set(new Uint8Array(this)); + return newBuffer; + } function constant(value) { return function() { return value; }; } - function ArrayBuffer(byteLength) { - var buffer = new _ArrayBuffer(byteLength); - buffer.slice = function() { - var newBuffer = new _ArrayBuffer(this.byteLength), - view = new Uint8Array(newBuffer); - - view.set(new Uint8Array(this)); - return newBuffer; - }; - setProperty(buffer.slice, 'toString', sliceToString); - return buffer; - } var reToString = /toString/g, nativeString = Function.prototype.toString.call(toString), bufferToString = constant(nativeString.replace(reToString, 'ArrayBuffer')), sliceToString = constant(nativeString.replace(reToString, 'slice')); setProperty(ArrayBuffer, 'toString', bufferToString); + setProperty(bufferSlice, 'toString', sliceToString); return ArrayBuffer; }(_ArrayBuffer)); } setProperty(window, '_Float64Array', window.Float64Array); - if (window._ArrayBuffer == window.ArrayBuffer && window._Float64Array) { - Float64Array = function() {}; - } else { + if (!window._Float64Array) { Float64Array = window.Uint8Array; } setProperty(window, 'WinRTError', Error); diff --git a/test/test.js b/test/test.js index 691753d4a..f99a620f9 100644 --- a/test/test.js +++ b/test/test.js @@ -37,7 +37,8 @@ push = arrayProto.push, slice = arrayProto.slice, system = root.system, - toString = objectProto.toString; + toString = objectProto.toString, + Uint8Array = root.Uint8Array; /** The file path of the Lo-Dash file to test */ var filePath = (function() { @@ -383,34 +384,34 @@ setProperty(stringProto, 'contains', _contains ? _.noop : Boolean); var _ArrayBuffer = ArrayBuffer; - if (Uint8Array && new ArrayBuffer(0).slice) { - setProperty(root, 'ArrayBuffer', (function() { - function ArrayBuffer(byteLength) { - var buffer = new _ArrayBuffer(byteLength); - buffer.slice = function() { - var newBuffer = new _ArrayBuffer(this.byteLength), - view = new Uint8Array(newBuffer); - - view.set(new Uint8Array(this)); - return newBuffer; - }; - setProperty(buffer.slice, 'toString', sliceToString); - return buffer; + setProperty(root, 'ArrayBuffer', (function() { + function ArrayBuffer(byteLength) { + var buffer = new _ArrayBuffer(byteLength); + if (!byteLength) { + setProperty(buffer, 'slice', buffer.slice ? null : bufferSlice); } - var reToString = /toString/g, - nativeString = _fnToString.call(toString), - bufferToString = _.constant(nativeString.replace(reToString, 'ArrayBuffer')), - sliceToString = _.constant(nativeString.replace(reToString, 'slice')); + return buffer; + } + function bufferSlice() { + var newBuffer = new _ArrayBuffer(this.byteLength), + view = new Uint8Array(newBuffer); + + view.set(new Uint8Array(this)); + return newBuffer; + } + var reToString = /toString/g, + nativeString = _fnToString.call(toString), + bufferToString = _.constant(nativeString.replace(reToString, 'ArrayBuffer')), + sliceToString = _.constant(nativeString.replace(reToString, 'slice')); + + setProperty(ArrayBuffer, 'toString', bufferToString); + setProperty(bufferSlice, 'toString', sliceToString); + return ArrayBuffer; + }())); - setProperty(ArrayBuffer, 'toString', bufferToString); - return ArrayBuffer; - }())); - } var _Float64Array = root.Float64Array; - if (_ArrayBuffer == root.ArrayBuffer && _Float64Array) { - setProperty(root, 'Float64Array', _.noop); - } else { - setProperty(root, 'Float64Array', root.Uint8Array); + if (!_Float64Array) { + setProperty(root, 'Float64Array', Uint8Array); } // fake `WinRTError` setProperty(root, 'WinRTError', Error); @@ -451,6 +452,8 @@ } if (_ArrayBuffer) { setProperty(root, 'ArrayBuffer', _ArrayBuffer); + } else { + delete root.ArrayBuffer; } if (_Float64Array) { setProperty(root, 'Float64Array', _Float64Array); @@ -651,9 +654,9 @@ } strictEqual(actual, true, message('_.contains', 'String#contains')); - if (root.ArrayBuffer) { + if (ArrayBuffer) { try { - var buffer = new ArrayBuffer(8); + var buffer = new ArrayBuffer(12); actual = lodashBizarro.clone(buffer); } catch(e) { actual = null; @@ -664,9 +667,9 @@ else { skipTest(2); } - if (root.ArrayBuffer && root.Uint8Array) { + if (ArrayBuffer && Uint8Array) { try { - var array = new Uint8Array(new ArrayBuffer(8)); + var array = new Uint8Array(new ArrayBuffer(12)); actual = lodashBizarro.cloneDeep(array); } catch(e) { actual = null;