Added complete support for Map also for IE11.

Fixes 1472
This commit is contained in:
Volonterio Riccardo
2018-01-11 18:13:24 +01:00
parent cb6de64e58
commit 085a1f8a16
7 changed files with 173 additions and 84 deletions

View File

@@ -35,8 +35,10 @@ describe("jasmineUnderTest.pp", function () {
describe('stringify maps', function() {
it("should stringify maps properly", function() {
jasmine.getEnv().requireFunctioningMaps();
expect(jasmineUnderTest.pp(new Map([[1, 2]]))).toEqual("Map( [ 1, 2 ] )");
jasmine.getEnv().requireFunctioningMaps();
var map = new Map();
map.set(1,2);
expect(jasmineUnderTest.pp(map)).toEqual("Map( [ 1, 2 ] )");
});
it("should truncate maps with more elments than jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH", function() {
@@ -44,8 +46,12 @@ describe("jasmineUnderTest.pp", function () {
var originalMaxSize = jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH;
try {
jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
expect(jasmineUnderTest.pp(new Map([["a", 1], ["b", 2], ["c", 3]]))).toEqual("Map( [ 'a', 1 ], [ 'b', 2 ], ... )");
jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
var map = new Map();
map.set("a",1);
map.set("b",2);
map.set("c",3);
expect(jasmineUnderTest.pp(map)).toEqual("Map( [ 'a', 1 ], [ 'b', 2 ], ... )");
} finally {
jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxSize;
}

View File

@@ -445,7 +445,8 @@ describe("matchersUtil", function() {
it("passes when comparing identical maps", function() {
jasmine.getEnv().requireFunctioningMaps();
var mapA = new Map([[6, 5]]);
var mapA = new Map();
mapA.set(6, 5);
var mapB = new Map();
mapB.set(6, 5);
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(true);
@@ -453,22 +454,34 @@ describe("matchersUtil", function() {
it("passes when comparing identical maps with different insertion order", function() {
jasmine.getEnv().requireFunctioningMaps();
var mapA = new Map([['a', 3], [6, 1]]);
var mapB = new Map([[6, 1], ['a', 3]]);
var mapA = new Map();
mapA.set("a", 3);
mapA.set(6, 1);
var mapB = new Map();
mapB.set(6, 1);
mapB.set("a", 3);
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(true);
});
it("fails for maps with different elements", function() {
jasmine.getEnv().requireFunctioningMaps();
var mapA = new Map([[6, 3], [5, 1]]);
var mapB = new Map([[6, 4], [5, 1]]);
var mapA = new Map();
mapA.set(6, 3);
mapA.set(5, 1);
var mapB = new Map();
mapB.set(6, 4);
mapB.set(5, 1);
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(false);
});
it("fails for maps of different size", function() {
jasmine.getEnv().requireFunctioningMaps();
var mapA = new Map([[6, 3]]);
var mapB = new Map([[6, 4], [5, 1]]);
var mapA = new Map();
mapA.set(6, 3);
var mapB = new Map();
mapB.set(6, 4);
mapB.set(5, 1);
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(false);
});

View File

@@ -436,11 +436,13 @@ describe("toEqual", function() {
// == Maps ==
it("does not report mismatches between deep equal Maps", function() {
jasmine.getEnv().requireFunctioningSets();
jasmine.getEnv().requireFunctioningMaps();
// values are the same but with different object identity
var actual = new Map([['a', {x: 1}]]),
expected = new Map([['a', {x: 1}]]);
var actual = new Map();
actual.set('a',{x:1});
var expected = new Map();
expected.set('a',{x:1});
expect(compareEquals(actual, expected).pass).toBe(true);
});
@@ -448,9 +450,11 @@ describe("toEqual", function() {
it("reports deep mismatches within Maps", function() {
jasmine.getEnv().requireFunctioningMaps();
var actual = new Map([['a', {x: 1}]]),
expected = new Map([['a', {x: 2}]]),
message = "Expected Map( [ 'a', Object({ x: 1 }) ] ) to equal Map( [ 'a', Object({ x: 2 }) ] ).";
var actual = new Map();
actual.set('a',{x:1});
var expected = new Map();
expected.set('a',{x:2});
var message = "Expected Map( [ 'a', Object({ x: 1 }) ] ) to equal Map( [ 'a', Object({ x: 2 }) ] ).";
expect(compareEquals(actual, expected).message).toEqual(message);
});
@@ -458,9 +462,14 @@ describe("toEqual", function() {
it("reports mismatches between Maps nested in objects", function() {
jasmine.getEnv().requireFunctioningMaps();
var actual = {Maps: [new Map([['a', 1]])]},
expected = {Maps: [new Map([['a', 2]])]},
message = "Expected $.Maps[0] = Map( [ 'a', 1 ] ) to equal Map( [ 'a', 2 ] ).";
var actual = {Maps:[new Map()]};
actual.Maps[0].set('a',1);
var expected = {Maps:[new Map()]};
expected.Maps[0].set('a',2);
// var actual = {Maps: [new Map([['a', 1]])]},
// expected = {Maps: [new Map([['a', 2]])]},
var message = "Expected $.Maps[0] = Map( [ 'a', 1 ] ) to equal Map( [ 'a', 2 ] ).";
expect(compareEquals(actual, expected).message).toEqual(message);
});
@@ -468,9 +477,12 @@ describe("toEqual", function() {
it("reports mismatches between Maps of different lengths", function() {
jasmine.getEnv().requireFunctioningMaps();
var actual = new Map([['a', 1]]),
expected = new Map([['a', 2], ['b', 1]]),
message = "Expected Map( [ 'a', 1 ] ) to equal Map( [ 'a', 2 ], [ 'b', 1 ] ).";
var actual = new Map();
actual.set('a',1);
var expected = new Map();
expected.set('a',2);
expected.set('b',1);
var message = "Expected Map( [ 'a', 1 ] ) to equal Map( [ 'a', 2 ], [ 'b', 1 ] ).";
expect(compareEquals(actual, expected).message).toEqual(message);
});
@@ -478,18 +490,22 @@ describe("toEqual", function() {
it("reports mismatches between Maps with equal values but differing keys", function() {
jasmine.getEnv().requireFunctioningMaps();
var actual = new Map([['a', 1]]),
expected = new Map([['b', 1]]),
message = "Expected Map( [ 'a', 1 ] ) to equal Map( [ 'b', 1 ] ).";
var actual = new Map();
actual.set('a',1);
var expected = new Map();
expected.set('b',1);
var message = "Expected Map( [ 'a', 1 ] ) to equal Map( [ 'b', 1 ] ).";
expect(compareEquals(actual, expected).message).toEqual(message);
});
it("does not report mismatches between Maps with keys with same object identity", function() {
jasmine.getEnv().requireFunctioningMaps();
var key = {x: 1},
actual = new Map([[key, 2]]),
expected = new Map([[key, 2]]);
var key = {x: 1};
var actual = new Map();
actual.set(key,2);
var expected = new Map();
expected.set(key,2);
expect(compareEquals(actual, expected).pass).toBe(true);
});
@@ -497,9 +513,11 @@ describe("toEqual", function() {
it("reports mismatches between Maps with identical keys with different object identity", function() {
jasmine.getEnv().requireFunctioningMaps();
var actual = new Map([[{x: 1}, 2]]),
expected = new Map([[{x: 1}, 2]]),
message = "Expected Map( [ Object({ x: 1 }), 2 ] ) to equal Map( [ Object({ x: 1 }), 2 ] ).";
var actual = new Map();
actual.set({x:1},2);
var expected = new Map();
expected.set({x:1},2);
var message = "Expected Map( [ Object({ x: 1 }), 2 ] ) to equal Map( [ Object({ x: 1 }), 2 ] ).";
expect(compareEquals(actual, expected).message).toEqual(message);
});
@@ -507,8 +525,11 @@ describe("toEqual", function() {
it("does not report mismatches when comparing Map key to jasmine.anything()", function() {
jasmine.getEnv().requireFunctioningMaps();
var actual = new Map([['a', 1]]),
expected = new Map([[jasmineUnderTest.anything(), 1]]);
var actual = new Map();
actual.set('a',1);
var expected = new Map();
expected.set(jasmineUnderTest.anything(),1);
expect(compareEquals(actual, expected).pass).toBe(true);
});
@@ -516,9 +537,12 @@ describe("toEqual", function() {
jasmine.getEnv().requireFunctioningMaps();
jasmine.getEnv().requireFunctioningSymbols();
var key = Symbol(),
actual = new Map([[key, 1]]),
expected = new Map([[key, 1]]);
var key = Symbol();
var actual = new Map();
actual.set(key,1);
var expected = new Map();
expected.set(key,1);
expect(compareEquals(actual, expected).pass).toBe(true);
});
@@ -526,9 +550,11 @@ describe("toEqual", function() {
jasmine.getEnv().requireFunctioningMaps();
jasmine.getEnv().requireFunctioningSymbols();
var actual = new Map([[Symbol(), 1]]),
expected = new Map([[Symbol(), 1]]),
message = "Expected Map( [ Symbol(), 1 ] ) to equal Map( [ Symbol(), 1 ] ).";
var actual = new Map();
actual.set(Symbol(),1);
var expected = new Map();
expected.set(Symbol(),1);
var message = "Expected Map( [ Symbol(), 1 ] ) to equal Map( [ Symbol(), 1 ] ).";
expect(compareEquals(actual, expected).message).toBe(message);
});
@@ -537,8 +563,11 @@ describe("toEqual", function() {
jasmine.getEnv().requireFunctioningMaps();
jasmine.getEnv().requireFunctioningSymbols();
var actual = new Map([[Symbol(), 1]]),
expected = new Map([[jasmineUnderTest.anything(), 1]]);
var actual = new Map();
actual.set(Symbol(),1);
var expected = new Map();
expected.set(jasmineUnderTest.anything(),1);
expect(compareEquals(actual, expected).pass).toBe(true);
});

View File

@@ -3,10 +3,36 @@
if (typeof Map === 'undefined') { return false; }
try {
var s = new Map([['a', 4]]);
if (s.size !== 1) { return false; }
if (s.keys().next().value !== 'a') { return false; }
if (s.values().next().value !== 4) { return false; }
var s = new Map();
s.set('a',1);
s.set('b',2);
// Check for `size`
if (s.size !== 2) { return false; }
// Check for `has`
if (s.has('a') !== true) { return false; }
// Check for `delete`
if (s.delete('b') !== true) { return false; }
// Check for `forEach`
var iterations = 0;
var ifForEachWorking = true;
s.forEach( function( value, key, map ) {
ifForEachWorking = ifForEachWorking && map === s;
if( key==='a') {
ifForEachWorking = ifForEachWorking && value===1;
}
iterations++;
} );
if (iterations !== 1) { return false; }
if (ifForEachWorking !== true) { return false; }
// Check for `clear`
s.clear()
if (s.size !== 0) { return false; }
return true;
} catch(e) {
return false;

View File

@@ -40,7 +40,7 @@ getJasmineRequireObj().pp = function(j$) {
this.emitScalar('Date(' + value + ')');
} else if (j$.getType_(value) == '[object Set]') {
this.emitSet(value);
} else if (j$.getType_(value) == '[object Map]') {
} else if (j$.isMap(value)) {
this.emitMap(value);
} else if (j$.isTypedArray_(value)) {
this.emitTypedArray(value);
@@ -157,13 +157,18 @@ getJasmineRequireObj().pp = function(j$) {
}
this.append('Map( ');
var size = Math.min(map.size, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
var iter = map.entries();
for (var i = 0; i < size; i++) {
if (i > 0) {
var i = 0;
map.forEach( function( value, key ) {
if (i >= size) {
return;
}
if (i > 0) {
this.append(', ');
}
this.format(iter.next().value);
}
}
this.format([key,value]);
i++;
}, this );
if (map.size > size){
this.append(', ...');
}

View File

@@ -93,6 +93,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return obj.nodeType > 0;
};
j$.isMap = function(obj) {
return typeof jasmineGlobal.Map !== 'undefined' && obj.constructor === jasmineGlobal.Map;
};
j$.isPromise = function(obj) {
return typeof jasmineGlobal.Promise !== 'undefined' && obj.constructor === jasmineGlobal.Promise;
};

View File

@@ -253,42 +253,48 @@ getJasmineRequireObj().matchersUtil = function(j$) {
if (!result) {
return false;
}
} else if (className == '[object Map]') {
} else if (j$.isMap(a) && j$.isMap(b)) {
if (a.size != b.size) {
diffBuilder.record(a, b);
return false;
}
}
var keysA = [];
var keysB = [];
a.forEach( function( valueA, keyA ) {
keysA.push( keyA );
});
b.forEach( function( valueB, keyB ) {
keysB.push( keyB );
});
// For both sets of keys, check they map to equal values in both maps.
// Keep track of corresponding keys (in insertion order) in order to handle asymmetric obj keys.
var mapKeys = [a.keys(), b.keys()];
var cmpKeys = [b.keys(), a.keys()];
var mapIter, mapKeyIt, mapKey, mapValueA, mapValueB;
var cmpIter, cmpKeyIt, cmpKey;
for (i = 0; result && i < mapKeys.length; i++) {
mapIter = mapKeys[i];
cmpIter = cmpKeys[i];
mapKeyIt = mapIter.next();
cmpKeyIt = cmpIter.next();
while (result && !mapKeyIt.done) {
mapKey = mapKeyIt.value;
cmpKey = cmpKeyIt.value;
mapValueA = a.get(mapKey);
// For both sets of keys, check they map to equal values in both maps.
// Keep track of corresponding keys (in insertion order) in order to handle asymmetric obj keys.
var mapKeys = [keysA, keysB];
var cmpKeys = [keysB, keysA];
var mapIter, mapKey, mapValueA, mapValueB;
var cmpIter, cmpKey;
for (i = 0; result && i < mapKeys.length; i++) {
mapIter = mapKeys[i];
cmpIter = cmpKeys[i];
for (var j = 0; result && j < mapIter.length; j++) {
mapKey = mapIter[j];
cmpKey = cmpIter[j];
mapValueA = a.get(mapKey);
// Only use the cmpKey when one of the keys is asymmetric and the corresponding key matches,
// otherwise explicitly look up the mapKey in the other Map since we want keys with unique
// obj identity (that are otherwise equal) to not match.
if (isAsymmetric(mapKey) || isAsymmetric(cmpKey) &&
eq(mapKey, cmpKey, aStack, bStack, customTesters, j$.NullDiffBuilder())) {
mapValueB = b.get(cmpKey);
} else {
mapValueB = b.get(mapKey);
}
result = eq(mapValueA, mapValueB, aStack, bStack, customTesters, j$.NullDiffBuilder());
mapKeyIt = mapIter.next();
cmpKeyIt = cmpIter.next();
}
}
// Only use the cmpKey when one of the keys is asymmetric and the corresponding key matches,
// otherwise explicitly look up the mapKey in the other Map since we want keys with unique
// obj identity (that are otherwise equal) to not match.
if (isAsymmetric(mapKey) || isAsymmetric(cmpKey) &&
eq(mapKey, cmpKey, aStack, bStack, customTesters, j$.NullDiffBuilder())) {
mapValueB = b.get(cmpKey);
} else {
mapValueB = b.get(mapKey);
}
result = eq(mapValueA, mapValueB, aStack, bStack, customTesters, j$.NullDiffBuilder());
}
}
if (!result) {
diffBuilder.record(a, b);