From 927cc011d6054b2f3e6db0df8130f35c27add721 Mon Sep 17 00:00:00 2001 From: Erik Welander Date: Fri, 4 Mar 2016 01:59:10 -0800 Subject: [PATCH] Added support for ES6 sets to toContain and toEqual. --- spec/core/PrettyPrintSpec.js | 19 ++++++++++++++ spec/core/matchers/matchersUtilSpec.js | 36 ++++++++++++++++++++++++++ src/core/PrettyPrinter.js | 23 ++++++++++++++++ src/core/base.js | 1 + src/core/matchers/matchersUtil.js | 17 ++++++++++++ 5 files changed, 96 insertions(+) diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index 7290db01..19e5965f 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -14,6 +14,25 @@ describe("jasmineUnderTest.pp", function () { expect(jasmineUnderTest.pp(-0)).toEqual("-0"); }); + describe('stringify sets', function() { + if (typeof Set === 'undefined') { return; } + + it("should stringify sets properly", function() { + expect(jasmineUnderTest.pp(new Set([1, 2]))).toEqual("Set( 1, 2 )"); + }); + + it("should truncate sets with more elments than jasmineUnderTest.MAX_PRETTY_PRINT_SET_SIZE", function() { + var originalMaxSize = jasmineUnderTest.MAX_PRETTY_PRINT_SET_SIZE; + + try { + jasmineUnderTest.MAX_PRETTY_PRINT_SET_SIZE = 2; + expect(jasmineUnderTest.pp(new Set(["a", "b", "c"]))).toEqual("Set( 'a', 'b', ... )"); + } finally { + jasmineUnderTest.MAX_PRETTY_PRINT_SET_SIZE = originalMaxSize; + } + }) + }); + describe('stringify arrays', function() { it("should stringify arrays properly", function() { expect(jasmineUnderTest.pp([1, 2])).toEqual("[ 1, 2 ]"); diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index 3f0deac1..ffab7b7d 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -314,6 +314,42 @@ describe("matchersUtil", function() { expect(jasmineUnderTest.matchersUtil.equals(objA, objB)).toBe(false); }); + + it("passes when comparing two empty sets", function() { + if (typeof Set === 'undefined') { return; } + expect(jasmineUnderTest.matchersUtil.equals(new Set(), new Set())).toBe(true); + }); + + it("passes when comparing identical sets", function() { + if (typeof Set === 'undefined') { return; } + var setA = new Set([6, 5]); + var setB = new Set(); + setB.add(6); + setB.add(5); + expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); + }); + + it("fails for sets with different elements", function() { + if (typeof Set === 'undefined') { return; } + var setA = new Set([6, 3, 5]); + var setB = new Set([6, 4, 5]); + expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false); + }); + + it("fails for sets of different size", function() { + if (typeof Set === 'undefined') { return; } + var setA = new Set([6, 3]); + var setB = new Set([6, 4, 5]); + expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false); + }); + + it("fails for sets with different insertion order", function() { + if (typeof Set === 'undefined') { return; } + var setA = new Set([3, 6]); + var setB = new Set([6, 3]); + expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false); + }); + }); describe("contains", function() { diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index e9452961..436eeba5 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -30,6 +30,8 @@ getJasmineRequireObj().pp = function(j$) { this.emitScalar('HTMLNode'); } else if (value instanceof Date) { this.emitScalar('Date(' + value + ')'); + } else if (value.toString && value.toString() == '[object Set]') { + this.emitSet(value); } else if (value.toString && typeof value === 'object' && !(value instanceof Array) && value.toString !== Object.prototype.toString) { this.emitScalar(value.toString()); } else if (j$.util.arrayContains(this.seen, value)) { @@ -59,6 +61,7 @@ getJasmineRequireObj().pp = function(j$) { }; PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_; + PrettyPrinter.prototype.emitSet = j$.unimplementedMethod_; PrettyPrinter.prototype.emitObject = j$.unimplementedMethod_; PrettyPrinter.prototype.emitScalar = j$.unimplementedMethod_; PrettyPrinter.prototype.emitString = j$.unimplementedMethod_; @@ -115,6 +118,26 @@ getJasmineRequireObj().pp = function(j$) { this.append(' ]'); }; + StringPrettyPrinter.prototype.emitSet = function(set) { + if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) { + this.append('Set'); + return; + } + this.append('Set( '); + var size = Math.min(set.size, j$.MAX_PRETTY_PRINT_SET_SIZE); + var iter = set.values(); + for (var i = 0; i < size; i++) { + if (i > 0) { + this.append(', '); + } + this.format(iter.next().value); + } + if (set.size > size){ + this.append(', ...'); + } + this.append(' )'); + }; + StringPrettyPrinter.prototype.emitObject = function(obj) { var constructorName = obj.constructor ? j$.fnNameFor(obj.constructor) : 'null'; this.append(constructorName); diff --git a/src/core/base.js b/src/core/base.js index 418a690f..1488838f 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -5,6 +5,7 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { j$.MAX_PRETTY_PRINT_DEPTH = 40; j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 100; + j$.MAX_PRETTY_PRINT_SET_SIZE = 100; j$.DEFAULT_TIMEOUT_INTERVAL = 5000; j$.getGlobal = function() { diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index 50b376c8..0ffdbffe 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -11,6 +11,10 @@ getJasmineRequireObj().matchersUtil = function(j$) { contains: function(haystack, needle, customTesters) { customTesters = customTesters || []; + if ((Object.prototype.toString.apply(haystack) === '[object Set]')) { + return haystack.has(needle); + } + if ((Object.prototype.toString.apply(haystack) === '[object Array]') || (!!haystack && !haystack.indexOf)) { @@ -173,6 +177,19 @@ getJasmineRequireObj().matchersUtil = function(j$) { return false; } } + } else if (className == '[object Set]') { + if (a.size != b.size) { + return false; + } + var iterA = a.values(), iterB = b.values(); + var valA, valB; + do { + valA = iterA.next(); + valB = iterB.next(); + if (!eq(valA.value, valB.value, aStack, bStack, customTesters)) { + return false; + } + } while (!valA.done && !valB.done); } else { // Objects with different constructors are not equivalent, but `Object`s