fix(test_lib): support comparing Maps in nested structures

This commit is contained in:
Tobias Bosch 2015-01-27 14:52:15 -08:00
parent af41fa9ac4
commit ec935565ca
2 changed files with 40 additions and 32 deletions

View File

@ -20,33 +20,47 @@ window.print = function(msg) {
}
};
// Some Map polyfills don't polyfill Map.toString correctly, which
// gives us bad error messages in tests.
// The only way to do this in Jasmine is to monkey patch a method
// to the object :-(
window.Map.prototype.jasmineToString = function() {
var m = this;
if (!m) {
return ''+m;
}
var res = [];
m.forEach( (v,k) => {
res.push(`${k}:${v}`);
});
return `{ ${res.join(',')} }`;
}
window.beforeEach(function() {
jasmine.addMatchers({
// Custom handler for Map to give nice error messages in JavaScript
// Custom handler for Map as Jasmine does not support it yet
toEqual: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var pass;
if (actual instanceof Map) {
pass = actual.size === expected.size;
if (pass) {
actual.forEach( (v,k) => {
pass = pass && util.equals(v, expected.get(k));
});
}
return {
pass: pass,
get message() {
return `Expected ${mapToString(actual)} ${(pass ? 'not' : '')} to equal to ${mapToString(expected)}`;
}
};
} else {
return {
pass: util.equals(actual, expected)
}
}
return {
pass: util.equals(actual, expected, [compareMap])
};
}
};
function compareMap(actual, expected) {
if (actual instanceof Map) {
var pass = actual.size === expected.size;
if (pass) {
actual.forEach( (v,k) => {
pass = pass && util.equals(v, expected.get(k));
});
}
return pass;
} else {
return undefined;
}
}
},
toBePromise: function() {
@ -134,17 +148,6 @@ export class SpyObject {
}
function mapToString(m) {
if (!m) {
return ''+m;
}
var res = [];
m.forEach( (v,k) => {
res.push(`${k}:${v}`);
});
return `{ ${res.join(',')} }`;
}
function elementText(n) {
var hasShadowRoot = (n) => n instanceof Element && n.shadowRoot;
var hasNodes = (n) => n.childNodes && n.childNodes.length > 0;

View File

@ -26,9 +26,14 @@ export function main() {
expect(falseActual).not.toEqual(expected);
});
it('should work for arrays of maps', () => {
it('should work for arrays of string maps', () => {
expect([{'a':'b'}]).toEqual([{'a':'b'}]);
});
it('should work for arrays of real maps', () => {
expect([MapWrapper.createFromStringMap({'a':'b'})]).toEqual([MapWrapper.createFromStringMap({'a':'b'})]);
expect([MapWrapper.createFromStringMap({'a':'b'})]).not.toEqual([MapWrapper.createFromStringMap({'a':'c'})]);
});
});
describe('toEqual for Maps', () => {