feat(facade): add equals method to StringMapWrapper

This commit is contained in:
Brian Ford 2015-05-11 09:08:37 -07:00
parent 5691063ba0
commit aff85b5037
3 changed files with 68 additions and 1 deletions

View File

@ -86,6 +86,17 @@ class StringMapWrapper {
return a.keys.toList();
}
static bool isEmpty(Map m) => m.isEmpty;
static bool equals(Map m1, Map m2) {
if (m1.length != m2.length) {
return false;
}
for (var key in m1.keys) {
if (m1[key] != m2[key]) {
return false;
}
}
return true;
}
}
class ListWrapper {

View File

@ -86,6 +86,22 @@ export class StringMapWrapper {
return m;
}
static equals(m1, m2) {
var k1 = Object.keys(m1);
var k2 = Object.keys(m2);
if (k1.length != k2.length) {
return false;
}
var key;
for (var i = 0; i < k1.length; i++) {
key = k1[i];
if (m1[key] !== m2[key]) {
return false;
}
}
return true;
}
}
export class ListWrapper {

View File

@ -1,7 +1,7 @@
import {describe, it, expect, beforeEach, ddescribe, iit, xit}
from 'angular2/test_lib';
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {List, ListWrapper, StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
export function main() {
describe('ListWrapper', () => {
@ -69,4 +69,44 @@ export function main() {
});
});
});
describe('StringMapWrapper', () => {
describe('equals', () => {
it('should return true when comparing empty maps', () => {
expect(StringMapWrapper.equals({}, {})).toBe(true);
});
it('should return true when comparing the same map', () => {
var m1 = {'a': 1, 'b': 2, 'c': 3};
expect(StringMapWrapper.equals(m1, m1)).toBe(true);
});
it('should return true when comparing different maps with the same keys and values', () => {
var m1 = {'a': 1, 'b': 2, 'c': 3};
var m2 = {'a': 1, 'b': 2, 'c': 3};
expect(StringMapWrapper.equals(m1, m2)).toBe(true);
});
it('should return false when comparing maps with different numbers of keys', () => {
var m1 = {'a': 1, 'b': 2, 'c': 3};
var m2 = {'a': 1, 'b': 2, 'c': 3, 'd': 4};
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
});
it('should return false when comparing maps with different keys', () => {
var m1 = {'a': 1, 'b': 2, 'c': 3};
var m2 = {'a': 1, 'b': 2, 'CC': 3};
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
});
it('should return false when comparing maps with different values', () => {
var m1 = {'a': 1, 'b': 2, 'c': 3};
var m2 = {'a': 1, 'b': 20, 'c': 3};
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
});
});
});
}