fix(test_lib): allow equality tests for `Map`

This commit is contained in:
Tobias Bosch 2014-11-17 17:39:39 -08:00
parent b2ecdb5da7
commit 7482b682d6
3 changed files with 72 additions and 7 deletions

View File

@ -2,5 +2,7 @@ name: test_lib
environment: environment:
sdk: '>=1.4.0' sdk: '>=1.4.0'
dependencies: dependencies:
facade:
path: ../facade
dev_dependencies: dev_dependencies:
guinness: ">=0.1.16 <0.2.0" guinness: ">=0.1.16 <0.2.0"

View File

@ -20,6 +20,33 @@ window.print = function(msg) {
window.beforeEach(function() { window.beforeEach(function() {
jasmine.addMatchers({ jasmine.addMatchers({
// Custom handler for Map to give nice error messages in JavaScript
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)
}
}
}
};
},
toBePromise: function() { toBePromise: function() {
return { return {
compare: function (actual, expectedClass) { compare: function (actual, expectedClass) {
@ -49,3 +76,15 @@ window.beforeEach(function() {
} }
}); });
}); });
function mapToString(m) {
if (!m) {
return ''+m;
}
var res = [];
m.forEach( (v,k) => {
res.push(`${k}:${v}`);
});
return `{ ${res.join(',')} }`;
}

View File

@ -1,4 +1,5 @@
import {describe, it, iit, expect} from 'test_lib/test_lib'; import {describe, it, iit, ddescribe, expect} from 'test_lib/test_lib';
import {MapWrapper} from 'facade/collection';
class TestObj { class TestObj {
constructor(prop) { constructor(prop) {
@ -7,12 +8,12 @@ class TestObj {
} }
export function main() { export function main() {
describe("test_lib", function () { describe('test_lib', () => {
describe("equality", function () { describe('equality', () => {
it("should structurally compare objects", function () { it('should structurally compare objects', () => {
var expected = new TestObj(new TestObj({"one" : [1,2]})); var expected = new TestObj(new TestObj({'one' : [1,2]}));
var actual = new TestObj(new TestObj({"one" : [1,2]})); var actual = new TestObj(new TestObj({'one' : [1,2]}));
var falseActual = new TestObj(new TestObj({"one" : [1,3]})); var falseActual = new TestObj(new TestObj({'one' : [1,3]}));
expect(actual).toEqual(expected); expect(actual).toEqual(expected);
expect(falseActual).not.toEqual(expected); expect(falseActual).not.toEqual(expected);
@ -22,5 +23,28 @@ export function main() {
expect([{'a':'b'}]).toEqual([{'a':'b'}]); expect([{'a':'b'}]).toEqual([{'a':'b'}]);
}); });
}); });
describe('toEqual for Maps', () => {
it('should detect equality for same reference', () => {
var m1 = MapWrapper.createFromStringMap({'a': 1});
expect(m1).toEqual(m1);
});
it('should detect equality for same content', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).toEqual(MapWrapper.createFromStringMap({'a': 1}));
});
it('should detect missing entries', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({}));
});
it('should detect different values', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 2}));
});
it('should detect additional entries', () => {
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 1, 'b': 1}));
});
});
}); });
} }