From ec935565ca365fb16e0ad62742e8356ac1a1fed2 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Tue, 27 Jan 2015 14:52:15 -0800 Subject: [PATCH] fix(test_lib): support comparing Maps in nested structures --- modules/test_lib/src/test_lib.es6 | 65 ++++++++++++++------------ modules/test_lib/test/test_lib_spec.js | 7 ++- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/modules/test_lib/src/test_lib.es6 b/modules/test_lib/src/test_lib.es6 index b979c5c5b0..eee4f7ab50 100644 --- a/modules/test_lib/src/test_lib.es6 +++ b/modules/test_lib/src/test_lib.es6 @@ -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; diff --git a/modules/test_lib/test/test_lib_spec.js b/modules/test_lib/test/test_lib_spec.js index d1d04db2b9..ed0d46ef4a 100644 --- a/modules/test_lib/test/test_lib_spec.js +++ b/modules/test_lib/test/test_lib_spec.js @@ -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', () => {