Added complete support for Set also for IE11.

Fixes 1355
This commit is contained in:
Volonterio Riccardo
2018-01-12 12:18:35 +01:00
parent 1dfdc89ba9
commit 486a64658f
7 changed files with 169 additions and 63 deletions

View File

@@ -16,8 +16,11 @@ describe("jasmineUnderTest.pp", function () {
describe('stringify sets', function() { describe('stringify sets', function() {
it("should stringify sets properly", function() { it("should stringify sets properly", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
expect(jasmineUnderTest.pp(new Set([1, 2]))).toEqual("Set( 1, 2 )"); var set = new Set();
set.add(1);
set.add(2);
expect(jasmineUnderTest.pp(set)).toEqual("Set( 1, 2 )");
}); });
it("should truncate sets with more elments than jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH", function() { it("should truncate sets with more elments than jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH", function() {
@@ -25,8 +28,12 @@ describe("jasmineUnderTest.pp", function () {
var originalMaxSize = jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH; var originalMaxSize = jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH;
try { try {
jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2; jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
expect(jasmineUnderTest.pp(new Set(["a", "b", "c"]))).toEqual("Set( 'a', 'b', ... )"); var set = new Set();
set.add('a');
set.add('b');
set.add('c');
expect(jasmineUnderTest.pp(set)).toEqual("Set( 'a', 'b', ... )");
} finally { } finally {
jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxSize; jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxSize;
} }

View File

@@ -396,45 +396,94 @@ describe("matchersUtil", function() {
it("passes when comparing identical sets", function() { it("passes when comparing identical sets", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
var setA = new Set([6, 5]);
var setA = new Set();
setA.add(6);
setA.add(5);
var setB = new Set(); var setB = new Set();
setB.add(6); setB.add(6);
setB.add(5); setB.add(5);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
}); });
it("passes when comparing identical sets with different insertion order and simple elements", function() { it("passes when comparing identical sets with different insertion order and simple elements", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
var setA = new Set([3, 6]);
var setB = new Set([6, 3]); var setA = new Set();
setA.add(3);
setA.add(6);
var setB = new Set();
setB.add(6);
setB.add(3);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
}); });
it("passes when comparing identical sets with different insertion order and complex elements 1", function() { it("passes when comparing identical sets with different insertion order and complex elements 1", function() {
jasmine.getEnv().requireFunctioningMaps(); jasmine.getEnv().requireFunctioningSets();
var setA = new Set([new Set([['a', 3], [6, 1]]), new Set([['y', 3], [6, 1]])]);
var setB = new Set([new Set([[6, 1], ['a', 3]]), new Set([[6, 1], ['y', 3]])]); var setA1 = new Set();
setA1.add(['a',3]);
setA1.add([6,1]);
var setA2 = new Set();
setA1.add(['y',3]);
setA1.add([6,1]);
var setA = new Set();
setA.add(setA1);
setA.add(setA2);
var setB1 = new Set();
setB1.add([6,1]);
setB1.add(['a',3]);
var setB2 = new Set();
setB1.add([6,1]);
setB1.add(['y',3]);
var setB = new Set();
setB.add(setB1);
setB.add(setB2);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
}); });
it("passes when comparing identical sets with different insertion order and complex elements 2", function() { it("passes when comparing identical sets with different insertion order and complex elements 2", function() {
jasmine.getEnv().requireFunctioningMaps(); jasmine.getEnv().requireFunctioningSets();
var setA = new Set([[[1,2], [3,4]], [[5,6], [7,8]]]);
var setB = new Set([[[5,6], [7,8]], [[1,2], [3,4]]]); var setA = new Set();
setA.add([[1,2], [3,4]]);
setA.add([[5,6], [7,8]]);
var setB = new Set();
setB.add([[5,6], [7,8]]);
setB.add([[1,2], [3,4]]);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true);
}); });
it("fails for sets with different elements", function() { it("fails for sets with different elements", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
var setA = new Set([6, 3, 5]); var setA = new Set();
var setB = new Set([6, 4, 5]); setA.add(6);
setA.add(3);
setA.add(5);
var setB = new Set();
setB.add(6);
setB.add(4);
setB.add(5);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false); expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false);
}); });
it("fails for sets of different size", function() { it("fails for sets of different size", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
var setA = new Set([6, 3]); var setA = new Set();
var setB = new Set([6, 4, 5]); setA.add(6);
setA.add(3);
var setB = new Set();
setB.add(6);
setB.add(4);
setB.add(5);
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false); expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false);
}); });

View File

@@ -374,9 +374,11 @@ describe("toEqual", function() {
it("reports mismatches between Sets", function() { it("reports mismatches between Sets", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
var actual = new Set([1]), var actual = new Set();
expected = new Set([2]), actual.add(1);
message = 'Expected Set( 1 ) to equal Set( 2 ).'; var expected = new Set();
expected.add(2);
var message = 'Expected Set( 1 ) to equal Set( 2 ).';
expect(compareEquals(actual, expected).message).toEqual(message); expect(compareEquals(actual, expected).message).toEqual(message);
}); });
@@ -384,9 +386,11 @@ describe("toEqual", function() {
it("reports deep mismatches within Sets", function() { it("reports deep mismatches within Sets", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
var actual = new Set([{x: 1}]), var actual = new Set();
expected = new Set([{x: 2}]), actual.add({x: 1});
message = 'Expected Set( Object({ x: 1 }) ) to equal Set( Object({ x: 2 }) ).'; var expected = new Set();
expected.add({x: 2});
var message = 'Expected Set( Object({ x: 1 }) ) to equal Set( Object({ x: 2 }) ).';
expect(compareEquals(actual, expected).message).toEqual(message); expect(compareEquals(actual, expected).message).toEqual(message);
}); });
@@ -394,9 +398,14 @@ describe("toEqual", function() {
it("reports mismatches between Sets nested in objects", function() { it("reports mismatches between Sets nested in objects", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
var actual = {sets: [new Set([1])]}, var actualSet = new Set();
expected = {sets: [new Set([2])]}, actualSet.add(1);
message = "Expected $.sets[0] = Set( 1 ) to equal Set( 2 )."; var expectedSet = new Set();
expectedSet.add(2);
var actual = { sets: [actualSet] };
var expected = { sets: [expectedSet] };
var message = "Expected $.sets[0] = Set( 1 ) to equal Set( 2 ).";
expect(compareEquals(actual, expected).message).toEqual(message); expect(compareEquals(actual, expected).message).toEqual(message);
}); });
@@ -404,9 +413,12 @@ describe("toEqual", function() {
it("reports mismatches between Sets of different lengths", function() { it("reports mismatches between Sets of different lengths", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
var actual = new Set([1, 2]), var actual = new Set();
expected = new Set([2]), actual.add(1);
message = 'Expected Set( 1, 2 ) to equal Set( 2 ).'; actual.add(2);
var expected = new Set();
expected.add(2);
var message = 'Expected Set( 1, 2 ) to equal Set( 2 ).';
expect(compareEquals(actual, expected).message).toEqual(message); expect(compareEquals(actual, expected).message).toEqual(message);
}); });
@@ -415,9 +427,13 @@ describe("toEqual", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
// Use 'duplicate' object in actual so sizes match // Use 'duplicate' object in actual so sizes match
var actual = new Set([{x: 1}, {x: 1}]), var actual = new Set();
expected = new Set([{x: 1}, {x: 2}]), actual.add({x: 1});
message = 'Expected Set( Object({ x: 1 }), Object({ x: 1 }) ) to equal Set( Object({ x: 1 }), Object({ x: 2 }) ).'; actual.add({x: 1});
var expected = new Set();
expected.add({x: 1});
expected.add({x: 2});
var message = 'Expected Set( Object({ x: 1 }), Object({ x: 1 }) ) to equal Set( Object({ x: 1 }), Object({ x: 2 }) ).';
expect(compareEquals(actual, expected).message).toEqual(message); expect(compareEquals(actual, expected).message).toEqual(message);
}); });
@@ -426,9 +442,13 @@ describe("toEqual", function() {
jasmine.getEnv().requireFunctioningSets(); jasmine.getEnv().requireFunctioningSets();
// Use 'duplicate' object in expected so sizes match // Use 'duplicate' object in expected so sizes match
var actual = new Set([{x: 1}, {x: 2}]), var actual = new Set();
expected = new Set([{x: 1}, {x: 1}]), actual.add({x: 1});
message = 'Expected Set( Object({ x: 1 }), Object({ x: 2 }) ) to equal Set( Object({ x: 1 }), Object({ x: 1 }) ).'; actual.add({x: 2});
var expected = new Set();
expected.add({x: 1});
expected.add({x: 1});
var message = 'Expected Set( Object({ x: 1 }), Object({ x: 2 }) ) to equal Set( Object({ x: 1 }), Object({ x: 1 }) ).';
expect(compareEquals(actual, expected).message).toEqual(message); expect(compareEquals(actual, expected).message).toEqual(message);
}); });

View File

@@ -3,9 +3,29 @@
if (typeof Set === 'undefined') { return false; } if (typeof Set === 'undefined') { return false; }
try { try {
var s = new Set([4]); var s = new Set();
if (s.size !== 1) { return false; } s.add(1);
if (s.values().next().value !== 4) { return false; } s.add(2);
if (s.size !== 2) { return false; }
if (s.has(1) !== true) { return false; }
var iterations = 0;
var isForEachWorking = true;
s.forEach(function(value, key, set) {
isForEachWorking = isForEachWorking && set === s;
if (iterations===0) {
isForEachWorking = isForEachWorking && (key===value) && value===1;
} else if (iterations===1) {
isForEachWorking = isForEachWorking && (key===value) && value===2;
}
iterations++;
});
if (iterations !== 2) { return false; }
if (isForEachWorking !== true) { return false; }
return true; return true;
} catch(e) { } catch(e) {
return false; return false;

View File

@@ -38,7 +38,7 @@ getJasmineRequireObj().pp = function(j$) {
this.emitScalar('HTMLNode'); this.emitScalar('HTMLNode');
} else if (value instanceof Date) { } else if (value instanceof Date) {
this.emitScalar('Date(' + value + ')'); this.emitScalar('Date(' + value + ')');
} else if (j$.getType_(value) == '[object Set]') { } else if (j$.isSet(value)) {
this.emitSet(value); this.emitSet(value);
} else if (j$.isMap(value)) { } else if (j$.isMap(value)) {
this.emitMap(value); this.emitMap(value);
@@ -137,13 +137,18 @@ getJasmineRequireObj().pp = function(j$) {
} }
this.append('Set( '); this.append('Set( ');
var size = Math.min(set.size, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH); var size = Math.min(set.size, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
var iter = set.values(); var i = 0;
for (var i = 0; i < size; i++) { set.forEach( function( value, key ) {
if (i >= size) {
return;
}
if (i > 0) { if (i > 0) {
this.append(', '); this.append(', ');
} }
this.format(iter.next().value); this.format(value);
}
i++;
}, this );
if (set.size > size){ if (set.size > size){
this.append(', ...'); this.append(', ...');
} }

View File

@@ -95,6 +95,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
j$.isMap = function(obj) { j$.isMap = function(obj) {
return typeof jasmineGlobal.Map !== 'undefined' && obj.constructor === jasmineGlobal.Map; return typeof jasmineGlobal.Map !== 'undefined' && obj.constructor === jasmineGlobal.Map;
};
j$.isSet = function(obj) {
return typeof jasmineGlobal.Set !== 'undefined' && obj.constructor === jasmineGlobal.Set;
}; };
j$.isPromise = function(obj) { j$.isPromise = function(obj) {

View File

@@ -300,48 +300,49 @@ getJasmineRequireObj().matchersUtil = function(j$) {
diffBuilder.record(a, b); diffBuilder.record(a, b);
return false; return false;
} }
} else if (className == '[object Set]') { } else if (j$.isSet(a) && j$.isSet(b)) {
if (a.size != b.size) { if (a.size != b.size) {
diffBuilder.record(a, b); diffBuilder.record(a, b);
return false; return false;
} }
var valuesA = [];
a.forEach( function( valueA ) {
valuesA.push( valueA );
});
var valuesB = [];
b.forEach( function( valueB ) {
valuesB.push( valueB );
});
// For both sets, check they are all contained in the other set // For both sets, check they are all contained in the other set
var setPairs = [[a, b], [b, a]]; var setPairs = [[valuesA, valuesB], [valuesB, valuesA]];
var stackPairs = [[aStack, bStack], [bStack, aStack]]; var stackPairs = [[aStack, bStack], [bStack, aStack]];
var baseIter, baseValueIt, baseValue, baseStack; var baseIter, baseValue, baseStack;
var otherSet, otherIter, otherValueIt, otherValue, otherStack; var otherSet, otherIter, otherValue, otherStack;
var found; var found;
var prevStackSize; var prevStackSize;
for (i = 0; result && i < setPairs.length; i++) { for (i = 0; result && i < setPairs.length; i++) {
baseIter = setPairs[i][0].values(); baseIter = setPairs[i][0];
otherSet = setPairs[i][1]; otherIter = setPairs[i][1];
baseStack = stackPairs[i][0]; baseStack = stackPairs[i][0];
otherStack = stackPairs[i][1]; otherStack = stackPairs[i][1];
// For each value in the base set... // For each value in the base set...
baseValueIt = baseIter.next(); for (var k = 0; result && k < baseIter.length; k++) {
while (result && !baseValueIt.done) { baseValue = baseIter[k];
baseValue = baseValueIt.value; found = false;
// ... test that it is present in the other set // ... test that it is present in the other set
// Optimisation: start looking for value by object identity for (var l = 0; !found && l < otherIter.length; l++) {
found = otherSet.has(baseValue); otherValue = otherIter[l];
if (!found) {
otherIter = otherSet.values();
otherValueIt = otherIter.next();
}
// If not found, compare by value equality
while (!found && !otherValueIt.done) {
otherValue = otherValueIt.value;
prevStackSize = baseStack.length; prevStackSize = baseStack.length;
// compare by value equality
found = eq(baseValue, otherValue, baseStack, otherStack, customTesters, j$.NullDiffBuilder()); found = eq(baseValue, otherValue, baseStack, otherStack, customTesters, j$.NullDiffBuilder());
if (!found && prevStackSize !== baseStack.length) { if (!found && prevStackSize !== baseStack.length) {
baseStack.splice(prevStackSize); baseStack.splice(prevStackSize);
otherStack.splice(prevStackSize); otherStack.splice(prevStackSize);
} }
otherValueIt = otherIter.next();
} }
result = result && found; result = result && found;
baseValueIt = baseIter.next();
} }
} }