From 62f769767ac7a2df7ee86f53adc043d4567950e4 Mon Sep 17 00:00:00 2001 From: Zaven Muradyan Date: Mon, 23 Oct 2017 19:31:40 -0700 Subject: [PATCH] Add support for jasmine.any(Symbol). 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 #1431. --- spec/core/asymmetric_equality/AnySpec.js | 32 ++++++++++++++++++++++++ spec/helpers/checkForSymbol.js | 28 +++++++++++++++++++++ spec/support/jasmine.json | 3 ++- spec/support/jasmine.yml | 3 ++- src/core/asymmetric_equality/Any.js | 6 +++++ 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 spec/helpers/checkForSymbol.js diff --git a/spec/core/asymmetric_equality/AnySpec.js b/spec/core/asymmetric_equality/AnySpec.js index 28c433bf..44ffe6b8 100644 --- a/spec/core/asymmetric_equality/AnySpec.js +++ b/spec/core/asymmetric_equality/AnySpec.js @@ -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); diff --git a/spec/helpers/checkForSymbol.js b/spec/helpers/checkForSymbol.js new file mode 100644 index 00000000..93e414ee --- /dev/null +++ b/spec/helpers/checkForSymbol.js @@ -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()); diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json index 604af59c..a6e286df 100644 --- a/spec/support/jasmine.json +++ b/spec/support/jasmine.json @@ -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" ], diff --git a/spec/support/jasmine.yml b/spec/support/jasmine.yml index 637b00eb..eb6a784d 100644 --- a/spec/support/jasmine.yml +++ b/spec/support/jasmine.yml @@ -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: diff --git a/src/core/asymmetric_equality/Any.js b/src/core/asymmetric_equality/Any.js index c401159a..3f447529 100644 --- a/src/core/asymmetric_equality/Any.js +++ b/src/core/asymmetric_equality/Any.js @@ -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; };