From 3234d475cd52c53ff7fb4b252d33bd76f3483b99 Mon Sep 17 00:00:00 2001 From: leoni laetitia Date: Sun, 31 Jan 2021 22:32:58 +0100 Subject: [PATCH] expect...toEqual check for Symbols --- lib/jasmine-core/boot.js | 2 +- lib/jasmine-core/jasmine-html.js | 2 +- lib/jasmine-core/jasmine.js | 9 ++- lib/jasmine-core/node_boot.js | 2 +- spec/core/matchers/toEqualSpec.js | 91 +++++++++++++++++++++++++++++++ src/core/matchers/matchersUtil.js | 21 ++++--- 6 files changed, 108 insertions(+), 19 deletions(-) diff --git a/lib/jasmine-core/boot.js b/lib/jasmine-core/boot.js index acd4c5a8..9c9aee42 100644 --- a/lib/jasmine-core/boot.js +++ b/lib/jasmine-core/boot.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2020 Pivotal Labs +Copyright (c) 2008-2021 Pivotal Labs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/lib/jasmine-core/jasmine-html.js b/lib/jasmine-core/jasmine-html.js index 5e436b3e..8539c76c 100644 --- a/lib/jasmine-core/jasmine-html.js +++ b/lib/jasmine-core/jasmine-html.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2020 Pivotal Labs +Copyright (c) 2008-2021 Pivotal Labs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 51c2d0ad..434697d4 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2020 Pivotal Labs +Copyright (c) 2008-2021 Pivotal Labs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -5172,16 +5172,15 @@ getJasmineRequireObj().MatchersUtil = function(j$) { }; function keys(obj, isArray) { - var allKeys = Object.keys - ? Object.keys(obj) - : (function(o) { + var allKeys = (function(o) { var keys = []; for (var key in o) { if (j$.util.has(o, key)) { keys.push(key); } } - return keys; + // eslint-disable-next-line compat/compat + return keys.concat(Object.getOwnPropertySymbols(o)); })(obj); if (!isArray) { diff --git a/lib/jasmine-core/node_boot.js b/lib/jasmine-core/node_boot.js index d62c7d36..4016e3b8 100644 --- a/lib/jasmine-core/node_boot.js +++ b/lib/jasmine-core/node_boot.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2020 Pivotal Labs +Copyright (c) 2008-2021 Pivotal Labs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/spec/core/matchers/toEqualSpec.js b/spec/core/matchers/toEqualSpec.js index b2fb9f9c..ec08ed63 100644 --- a/spec/core/matchers/toEqualSpec.js +++ b/spec/core/matchers/toEqualSpec.js @@ -1,3 +1,4 @@ +/* eslint-disable compat/compat */ describe('toEqual', function() { 'use strict'; @@ -1077,4 +1078,94 @@ describe('toEqual', function() { expect(compareEquals(actual, expected).message).toEqual(message); }); }); + + // == Symbols == + + describe('Symbols', function() { + it('Fails if Symbol compared to Object', function() { + var sym = Symbol('foo'); + var obj = {}; + + expect(sym).not.toEqual(obj); + }); + + it('Passes Symbol with itself', function() { + var sym = Symbol('foo'); + + expect(sym).toEqual(sym); + }); + + it('Fails if two Symbols with same value are compared', function() { + var symA = Symbol('foo'); + var symB = Symbol('foo'); + + expect(symA).not.toEqual(symB); + }); + + it('Fails if two Symbols with different value are compared', function() { + var symA = Symbol('foo'); + var symB = Symbol('bar'); + + expect(symA).not.toEqual(symB); + }); + + it('Fails if Symbol compared to NaN', function() { + var sym = Symbol('foo'); + + expect(sym).not.toEqual(NaN); + }); + + it('Fails if Symbol compared to Infinity', function() { + var sym = Symbol('foo'); + + expect(sym).not.toEqual(Infinity); + }); + + it('Fails if Symbol compared to String', function() { + var sym = Symbol('foo'); + var str = 'foo'; + + expect(sym).not.toEqual(str); + }); + + it('Fails if Symbol compared to Number', function() { + var sym = Symbol('foo'); + var num = Math.random(); + + expect(sym).not.toEqual(num); + }); + + it('Fails if Symbol compared to Boolean', function() { + var sym = Symbol('foo'); + + expect(sym).not.toEqual(true); + expect(sym).not.toEqual(false); + }); + + it('Fails if Symbol compared to Undefined', function() { + var sym = Symbol('foo'); + + expect(sym).not.toEqual(undefined); + }); + + it('Fails if Symbol compared to null', function() { + var sym = Symbol('foo'); + + expect(sym).not.toEqual(null); + }); + + it('Fails if Symbol compared to []', function() { + var sym = Symbol('foo'); + var arr = ['foo']; + + expect(sym).not.toEqual(arr); + }); + + it('Fails if Symbol compared to Function', function() { + var sym = Symbol('foo'); + var f = function func() {}; + + expect(sym).not.toEqual(f); + }); + }); }); diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index 5f75a4ed..74ac0f11 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -544,17 +544,16 @@ getJasmineRequireObj().MatchersUtil = function(j$) { }; function keys(obj, isArray) { - var allKeys = Object.keys - ? Object.keys(obj) - : (function(o) { - var keys = []; - for (var key in o) { - if (j$.util.has(o, key)) { - keys.push(key); - } - } - return keys; - })(obj); + var allKeys = (function(o) { + var keys = []; + for (var key in o) { + if (j$.util.has(o, key)) { + keys.push(key); + } + } + // eslint-disable-next-line compat/compat + return keys.concat(Object.getOwnPropertySymbols(o)); + })(obj); if (!isArray) { return allKeys;