Skip to content

Commit

Permalink
Add support for jasmine.any(Symbol).
Browse files Browse the repository at this point in the history
The original asymmetric matcher code for Any did not work with symbols
since `symbolInstance instanceof Symbol` is actually `false` (but,
`symbolInstance.constructor` is `Symbol). This simply adds an extra
clause that explicitly checks for symbol (if available) like the other
primitive types.

Also added some missing specs for other types, like Map, Set, etc.

Fixes jasmine#1431.
  • Loading branch information
voithos committed Oct 24, 2017
1 parent 03f7f76 commit 62f7697
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
32 changes: 32 additions & 0 deletions spec/core/asymmetric_equality/AnySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@ describe("Any", function() {
expect(any.asymmetricMatch(true)).toBe(true);
});

it("matches a Map", function() {
jasmine.getEnv().requireFunctioningMaps();

var any = new jasmineUnderTest.Any(Map);

expect(any.asymmetricMatch(new Map())).toBe(true);
});

it("matches a Set", function() {
jasmine.getEnv().requireFunctioningSets();

var any = new jasmineUnderTest.Any(Set);

expect(any.asymmetricMatch(new Set())).toBe(true);
});

it("matches a TypedArray", function() {
jasmine.getEnv().requireFunctioningTypedArrays();

var any = new jasmineUnderTest.Any(Uint32Array);

expect(any.asymmetricMatch(new Uint32Array([]))).toBe(true);
});

it("matches a Symbol", function() {
jasmine.getEnv().requireFunctioningSymbols();

var any = new jasmineUnderTest.Any(Symbol);

expect(any.asymmetricMatch(Symbol())).toBe(true);
});

it("matches another constructed object", function() {
var Thing = function() {},
any = new jasmineUnderTest.Any(Thing);
Expand Down
28 changes: 28 additions & 0 deletions spec/helpers/checkForSymbol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(function(env) {
function hasFunctioningSymbols() {
if (typeof Symbol === 'undefined') {
return false;
}

try {
var s1 = Symbol();
var s2 = Symbol();
if (typeof s1 !== 'symbol') {
return false;
}
if (s1 === s2) {
return false;
}
return true;
} catch (e) {
return false;
}
}

env.requireFunctioningSymbols = function() {
if (!hasFunctioningSymbols()) {
env.pending("Browser has incomplete or missing support for Symbols");
}
};

})(jasmine.getEnv());
3 changes: 2 additions & 1 deletion spec/support/jasmine.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
],
"helpers": [
"helpers/asyncAwait.js",
"helpers/checkForSet.js",
"helpers/checkForMap.js",
"helpers/checkForSet.js",
"helpers/checkForSymbol.js",
"helpers/checkForTypedArrays.js",
"helpers/nodeDefineJasmineUnderTest.js"
],
Expand Down
3 changes: 2 additions & 1 deletion spec/support/jasmine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ stylesheets:
helpers:
- 'helpers/asyncAwait.js'
- 'helpers/BrowserFlags.js'
- 'helpers/checkForSet.js'
- 'helpers/checkForMap.js'
- 'helpers/checkForSet.js'
- 'helpers/checkForSymbol.js'
- 'helpers/checkForTypedArrays.js'
- 'helpers/defineJasmineUnderTest.js'
spec_files:
Expand Down
6 changes: 6 additions & 0 deletions src/core/asymmetric_equality/Any.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ getJasmineRequireObj().Any = function(j$) {
return typeof other == 'boolean';
}

/* jshint -W122 */
if (typeof Symbol != 'undefined' && this.expectedObject == Symbol) {
return typeof other == 'symbol';
}
/* jshint +W122 */

return other instanceof this.expectedObject;
};

Expand Down

0 comments on commit 62f7697

Please sign in to comment.