Merge branch 'array_buffer' of https://github.com/Finesse/jasmine into main

* Adds support for ArrayBuffers to matchersUtil.equals
* Merges #1891 from @Finesse
* Merges #1689 from @dankurka
* Fixes #1687
This commit is contained in:
Steve Gravrock
2021-03-22 13:06:40 -07:00
5 changed files with 57 additions and 0 deletions

View File

@@ -885,6 +885,25 @@ describe('matchersUtil', function() {
);
});
it('passes for ArrayBuffers with same length and content', function() {
jasmine.getEnv().requireFunctioningArrayBuffers();
var buffer1 = new ArrayBuffer(4); // eslint-disable-line compat/compat
var buffer2 = new ArrayBuffer(4); // eslint-disable-line compat/compat
expect(jasmineUnderTest.matchersUtil.equals(buffer1, buffer2)).toBe(true);
});
it('fails for ArrayBuffers with same length but different content', function() {
jasmine.getEnv().requireFunctioningTypedArrays();
jasmine.getEnv().requireFunctioningArrayBuffers();
var buffer1 = new ArrayBuffer(4); // eslint-disable-line compat/compat
var buffer2 = new ArrayBuffer(4); // eslint-disable-line compat/compat
var array1 = new Uint8Array(buffer1); // eslint-disable-line compat/compat
array1[0] = 1;
expect(jasmineUnderTest.matchersUtil.equals(buffer1, buffer2)).toBe(
false
);
});
describe('when running in an environment with array polyfills', function() {
var findIndexDescriptor = Object.getOwnPropertyDescriptor(
Array.prototype,

View File

@@ -0,0 +1,25 @@
/* eslint-disable compat/compat */
(function(env) {
function hasFunctioningArrayBuffers() {
if (typeof ArrayBuffer === 'undefined') {
return false;
}
try {
var buffer = new ArrayBuffer(2);
var view8bit = new Uint8Array(buffer);
var view16bit = new Uint16Array(buffer);
view16bit[0] = 0xabcd;
return view8bit[0] === 0xcd && view8bit[1] === 0xab;
} catch (e) {
return false;
}
}
env.requireFunctioningArrayBuffers = function() {
env.requireFunctioningTypedArrays();
if (!hasFunctioningArrayBuffers()) {
env.pending('Browser has incomplete or missing support for ArrayBuffer');
}
};
})(jasmine.getEnv());

View File

@@ -20,6 +20,7 @@ module.exports = {
'helpers/asyncAwait.js',
'helpers/generator.js',
'helpers/BrowserFlags.js',
'helpers/checkForArrayBuffer.js',
'helpers/checkForMap.js',
'helpers/checkForSet.js',
'helpers/checkForSymbol.js',

View File

@@ -7,6 +7,7 @@
"helpers": [
"helpers/asyncAwait.js",
"helpers/generator.js",
"helpers/checkForArrayBuffer.js",
"helpers/checkForMap.js",
"helpers/checkForSet.js",
"helpers/checkForSymbol.js",

View File

@@ -269,6 +269,17 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
diffBuilder.recordMismatch();
}
return result;
case '[object ArrayBuffer]':
// If we have an instance of ArrayBuffer the Uint8Array ctor
// will be defined as well
return self.eq_(
new Uint8Array(a), // eslint-disable-line compat/compat
new Uint8Array(b), // eslint-disable-line compat/compat
aStack,
bStack,
customTesters,
diffBuilder
);
// RegExps are compared by their source patterns and flags.
case '[object RegExp]':
return (