Skip to content

Commit 7482b68

Browse files
committed
fix(test_lib): allow equality tests for Map
1 parent b2ecdb5 commit 7482b68

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

modules/test_lib/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ name: test_lib
22
environment:
33
sdk: '>=1.4.0'
44
dependencies:
5+
facade:
6+
path: ../facade
57
dev_dependencies:
68
guinness: ">=0.1.16 <0.2.0"

modules/test_lib/src/test_lib.es6

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ window.print = function(msg) {
2020

2121
window.beforeEach(function() {
2222
jasmine.addMatchers({
23+
// Custom handler for Map to give nice error messages in JavaScript
24+
toEqual: function(util, customEqualityTesters) {
25+
return {
26+
compare: function(actual, expected) {
27+
var pass;
28+
if (actual instanceof Map) {
29+
pass = actual.size === expected.size;
30+
if (pass) {
31+
actual.forEach( (v,k) => {
32+
pass = pass && util.equals(v, expected.get(k));
33+
});
34+
}
35+
return {
36+
pass: pass,
37+
get message() {
38+
return `Expected ${mapToString(actual)} ${(pass ? 'not' : '')} to equal to ${mapToString(expected)}`;
39+
}
40+
};
41+
} else {
42+
return {
43+
pass: util.equals(actual, expected)
44+
}
45+
}
46+
}
47+
};
48+
},
49+
2350
toBePromise: function() {
2451
return {
2552
compare: function (actual, expectedClass) {
@@ -49,3 +76,15 @@ window.beforeEach(function() {
4976
}
5077
});
5178
});
79+
80+
81+
function mapToString(m) {
82+
if (!m) {
83+
return ''+m;
84+
}
85+
var res = [];
86+
m.forEach( (v,k) => {
87+
res.push(`${k}:${v}`);
88+
});
89+
return `{ ${res.join(',')} }`;
90+
}
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {describe, it, iit, expect} from 'test_lib/test_lib';
1+
import {describe, it, iit, ddescribe, expect} from 'test_lib/test_lib';
2+
import {MapWrapper} from 'facade/collection';
23

34
class TestObj {
45
constructor(prop) {
@@ -7,12 +8,12 @@ class TestObj {
78
}
89

910
export function main() {
10-
describe("test_lib", function () {
11-
describe("equality", function () {
12-
it("should structurally compare objects", function () {
13-
var expected = new TestObj(new TestObj({"one" : [1,2]}));
14-
var actual = new TestObj(new TestObj({"one" : [1,2]}));
15-
var falseActual = new TestObj(new TestObj({"one" : [1,3]}));
11+
describe('test_lib', () => {
12+
describe('equality', () => {
13+
it('should structurally compare objects', () => {
14+
var expected = new TestObj(new TestObj({'one' : [1,2]}));
15+
var actual = new TestObj(new TestObj({'one' : [1,2]}));
16+
var falseActual = new TestObj(new TestObj({'one' : [1,3]}));
1617

1718
expect(actual).toEqual(expected);
1819
expect(falseActual).not.toEqual(expected);
@@ -22,5 +23,28 @@ export function main() {
2223
expect([{'a':'b'}]).toEqual([{'a':'b'}]);
2324
});
2425
});
26+
27+
describe('toEqual for Maps', () => {
28+
it('should detect equality for same reference', () => {
29+
var m1 = MapWrapper.createFromStringMap({'a': 1});
30+
expect(m1).toEqual(m1);
31+
});
32+
33+
it('should detect equality for same content', () => {
34+
expect(MapWrapper.createFromStringMap({'a': 1})).toEqual(MapWrapper.createFromStringMap({'a': 1}));
35+
});
36+
37+
it('should detect missing entries', () => {
38+
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({}));
39+
});
40+
41+
it('should detect different values', () => {
42+
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 2}));
43+
});
44+
45+
it('should detect additional entries', () => {
46+
expect(MapWrapper.createFromStringMap({'a': 1})).not.toEqual(MapWrapper.createFromStringMap({'a': 1, 'b': 1}));
47+
});
48+
});
2549
});
2650
}

0 commit comments

Comments
 (0)