Merge branch 'map-support' of https://github.com/rmehlinger/jasmine into rmehlinger-map-support
- Merges #1340 from @rmehlinger - Fixes #1257
This commit is contained in:
@@ -2512,12 +2512,12 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
} else if (className == '[object Set]') {
|
||||
} else if (className == '[object Set]' || className == '[object Map]') {
|
||||
if (a.size != b.size) {
|
||||
diffBuilder.record(a, b);
|
||||
return false;
|
||||
}
|
||||
var iterA = a.values(), iterB = b.values();
|
||||
var iterA = a.entries(), iterB = b.entries();
|
||||
var valA, valB;
|
||||
do {
|
||||
valA = iterA.next();
|
||||
@@ -3669,6 +3669,8 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.emitScalar('Date(' + value + ')');
|
||||
} else if (value.toString && value.toString() == '[object Set]') {
|
||||
this.emitSet(value);
|
||||
} else if (value.toString && value.toString() == '[object Map]') {
|
||||
this.emitMap(value);
|
||||
} else if (value.toString && typeof value === 'object' && !j$.isArray_(value) && hasCustomToString(value)) {
|
||||
this.emitScalar(value.toString());
|
||||
} else if (j$.util.arrayContains(this.seen, value)) {
|
||||
@@ -3699,6 +3701,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
|
||||
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitSet = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitMap = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitObject = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitScalar = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitString = j$.unimplementedMethod_;
|
||||
@@ -3775,6 +3778,26 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.append(' )');
|
||||
};
|
||||
|
||||
StringPrettyPrinter.prototype.emitMap = function(map) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Map');
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
this.append(', ');
|
||||
}
|
||||
this.format(iter.next().value);
|
||||
}
|
||||
if (map.size > size){
|
||||
this.append(', ...');
|
||||
}
|
||||
this.append(' )');
|
||||
};
|
||||
|
||||
StringPrettyPrinter.prototype.emitObject = function(obj) {
|
||||
var ctor = obj.constructor,
|
||||
constructorName;
|
||||
|
||||
@@ -33,6 +33,26 @@ 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 ] )");
|
||||
});
|
||||
|
||||
it("should truncate maps with more elments than jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH", function() {
|
||||
jasmine.getEnv().requireFunctioningMaps();
|
||||
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 ], ... )");
|
||||
} finally {
|
||||
jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxSize;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
describe('stringify arrays', function() {
|
||||
it("should stringify arrays properly", function() {
|
||||
expect(jasmineUnderTest.pp([1, 2])).toEqual("[ 1, 2 ]");
|
||||
|
||||
@@ -414,6 +414,40 @@ describe("matchersUtil", function() {
|
||||
expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(false);
|
||||
});
|
||||
|
||||
it("passes when comparing two empty maps", function() {
|
||||
jasmine.getEnv().requireFunctioningMaps();
|
||||
expect(jasmineUnderTest.matchersUtil.equals(new Map(), new Map())).toBe(true);
|
||||
});
|
||||
|
||||
it("passes when comparing identical maps", function() {
|
||||
jasmine.getEnv().requireFunctioningMaps();
|
||||
var mapA = new Map([[6, 5]]);
|
||||
var mapB = new Map();
|
||||
mapB.set(6, 5);
|
||||
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]]);
|
||||
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]]);
|
||||
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(false);
|
||||
});
|
||||
|
||||
it("fails for 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]]);
|
||||
expect(jasmineUnderTest.matchersUtil.equals(mapA, mapB)).toBe(false);
|
||||
});
|
||||
|
||||
describe("when running in an environment with array polyfills", function() {
|
||||
// IE 8 doesn't support `definePropery` on non-DOM nodes
|
||||
if (jasmine.getEnv().ieVersion < 9) { return; }
|
||||
|
||||
@@ -390,6 +390,37 @@ describe("toEqual", function() {
|
||||
expect(compareEquals(actual, expected).message).toEqual(message);
|
||||
});
|
||||
|
||||
it("does not report deep mismatches within Maps", function() {
|
||||
// TODO: implement deep comparison of Map elements
|
||||
jasmine.getEnv().requireFunctioningMaps();
|
||||
|
||||
var actual = new Map([['a', 1]]),
|
||||
expected = new Map([['a', 2]]),
|
||||
message = "Expected Map( [ 'a', 1 ] ) to equal Map( [ 'a', 2 ] ).";
|
||||
|
||||
expect(compareEquals(actual, expected).message).toEqual(message);
|
||||
});
|
||||
|
||||
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], ['b', 1]])]},
|
||||
message = "Expected $.Maps[0] = Map( [ 'a', 1 ] ) to equal Map( [ 'a', 2 ], [ 'b', 1 ] ).";
|
||||
|
||||
expect(compareEquals(actual, expected).message).toEqual(message);
|
||||
});
|
||||
|
||||
it("reports mismatches between Maps of different lengths", function() {
|
||||
jasmine.getEnv().requireFunctioningMaps();
|
||||
|
||||
var actual = new Map([['a', 1]]),
|
||||
expected = new Map([['a', 2]]),
|
||||
message = "Expected Map( [ 'a', 1 ] ) to equal Map( [ 'a', 2 ] ).";
|
||||
|
||||
expect(compareEquals(actual, expected).message).toEqual(message);
|
||||
});
|
||||
|
||||
function isNotRunningInBrowser() {
|
||||
return typeof document === 'undefined'
|
||||
}
|
||||
|
||||
22
spec/helpers/checkForMap.js
Normal file
22
spec/helpers/checkForMap.js
Normal file
@@ -0,0 +1,22 @@
|
||||
(function(env) {
|
||||
function hasFunctioningMaps() {
|
||||
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; }
|
||||
return true;
|
||||
} catch(e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
env.requireFunctioningMaps = function() {
|
||||
if (!hasFunctioningMaps()) {
|
||||
env.pending("Browser has incomplete or missing support for Maps");
|
||||
}
|
||||
};
|
||||
|
||||
})(jasmine.getEnv());
|
||||
@@ -8,6 +8,7 @@
|
||||
"helpers": [
|
||||
"helpers/asyncAwait.js",
|
||||
"helpers/checkForSet.js",
|
||||
"helpers/checkForMap.js",
|
||||
"helpers/nodeDefineJasmineUnderTest.js"
|
||||
],
|
||||
"random": true
|
||||
|
||||
@@ -19,6 +19,7 @@ helpers:
|
||||
- 'helpers/asyncAwait.js'
|
||||
- 'helpers/BrowserFlags.js'
|
||||
- 'helpers/checkForSet.js'
|
||||
- 'helpers/checkForMap.js'
|
||||
- 'helpers/defineJasmineUnderTest.js'
|
||||
spec_files:
|
||||
- '**/*[Ss]pec.js'
|
||||
|
||||
@@ -38,6 +38,8 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.emitScalar('Date(' + value + ')');
|
||||
} else if (value.toString && value.toString() == '[object Set]') {
|
||||
this.emitSet(value);
|
||||
} else if (value.toString && value.toString() == '[object Map]') {
|
||||
this.emitMap(value);
|
||||
} else if (value.toString && typeof value === 'object' && !j$.isArray_(value) && hasCustomToString(value)) {
|
||||
this.emitScalar(value.toString());
|
||||
} else if (j$.util.arrayContains(this.seen, value)) {
|
||||
@@ -68,6 +70,7 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
|
||||
PrettyPrinter.prototype.emitArray = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitSet = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitMap = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitObject = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitScalar = j$.unimplementedMethod_;
|
||||
PrettyPrinter.prototype.emitString = j$.unimplementedMethod_;
|
||||
@@ -144,6 +147,26 @@ getJasmineRequireObj().pp = function(j$) {
|
||||
this.append(' )');
|
||||
};
|
||||
|
||||
StringPrettyPrinter.prototype.emitMap = function(map) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Map');
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
this.append(', ');
|
||||
}
|
||||
this.format(iter.next().value);
|
||||
}
|
||||
if (map.size > size){
|
||||
this.append(', ...');
|
||||
}
|
||||
this.append(' )');
|
||||
};
|
||||
|
||||
StringPrettyPrinter.prototype.emitObject = function(obj) {
|
||||
var ctor = obj.constructor,
|
||||
constructorName;
|
||||
|
||||
@@ -239,12 +239,12 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
} else if (className == '[object Set]') {
|
||||
} else if (className == '[object Set]' || className == '[object Map]') {
|
||||
if (a.size != b.size) {
|
||||
diffBuilder.record(a, b);
|
||||
return false;
|
||||
}
|
||||
var iterA = a.values(), iterB = b.values();
|
||||
var iterA = a.entries(), iterB = b.entries();
|
||||
var valA, valB;
|
||||
do {
|
||||
valA = iterA.next();
|
||||
|
||||
Reference in New Issue
Block a user