Merge branch 'add-map-set-matchers' of https://github.com/eventlistener/jasmine
* Merges #1741 from @eventlistener
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
describe('MapContaining', function() {
|
||||
function MapI(iterable) { // for IE11
|
||||
var map = new Map();
|
||||
iterable.forEach(function(kv) {
|
||||
map.set(kv[0], kv[1]);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
jasmine.getEnv().requireFunctioningMaps();
|
||||
});
|
||||
|
||||
|
||||
it('matches any actual map to an empty map', function() {
|
||||
var actualMap = new MapI([['foo', 'bar']]);
|
||||
var containing = new jasmineUnderTest.MapContaining(new Map());
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap)).toBe(true);
|
||||
});
|
||||
|
||||
it('matches when all the key/value pairs in sample have matches in actual', function() {
|
||||
var actualMap = new MapI([
|
||||
['foo', [1, 2, 3]],
|
||||
[{'foo': 'bar'}, 'baz'],
|
||||
['other', 'any'],
|
||||
]);
|
||||
|
||||
var containingMap = new MapI([
|
||||
[{'foo': 'bar'}, 'baz'],
|
||||
['foo', [1, 2, 3]],
|
||||
]);
|
||||
var containing = new jasmineUnderTest.MapContaining(containingMap);
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap)).toBe(true);
|
||||
});
|
||||
|
||||
it('does not match when a key is not in actual', function() {
|
||||
var actualMap = new MapI([
|
||||
['foo', [1, 2, 3]],
|
||||
[{'foo': 'not a bar'}, 'baz'],
|
||||
]);
|
||||
|
||||
var containingMap = new MapI([
|
||||
[{'foo': 'bar'}, 'baz'],
|
||||
['foo', [1, 2, 3]],
|
||||
]);
|
||||
var containing = new jasmineUnderTest.MapContaining(containingMap);
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap)).toBe(false);
|
||||
});
|
||||
|
||||
it('does not match when a value is not in actual', function() {
|
||||
var actualMap = new MapI([
|
||||
['foo', [1, 2, 3]],
|
||||
[{'foo': 'bar'}, 'baz'],
|
||||
]);
|
||||
|
||||
var containingMap = new MapI([
|
||||
[{'foo': 'bar'}, 'baz'],
|
||||
['foo', [1, 2]],
|
||||
]);
|
||||
var containing = new jasmineUnderTest.MapContaining(containingMap);
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap)).toBe(false);
|
||||
});
|
||||
|
||||
it('matches when all the key/value pairs in sample have asymmetric matches in actual', function() {
|
||||
var actualMap = new MapI([
|
||||
['foo1', 'not a bar'],
|
||||
['foo2', 'bar'],
|
||||
['baz', [1, 2, 3, 4]],
|
||||
]);
|
||||
|
||||
var containingMap = new MapI([
|
||||
[
|
||||
jasmine.stringMatching(/^foo\d/),
|
||||
'bar'
|
||||
],
|
||||
[
|
||||
'baz',
|
||||
jasmine.arrayContaining([2, 3])
|
||||
],
|
||||
]);
|
||||
var containing = new jasmineUnderTest.MapContaining(containingMap);
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap)).toBe(true);
|
||||
});
|
||||
|
||||
it('does not match when a key in sample has no asymmetric matches in actual', function() {
|
||||
var actualMap = new MapI([
|
||||
['a-foo1', 'bar'],
|
||||
['baz', [1, 2, 3, 4]],
|
||||
]);
|
||||
|
||||
var containingMap = new MapI([
|
||||
[
|
||||
jasmine.stringMatching(/^foo\d/),
|
||||
'bar'
|
||||
],
|
||||
[
|
||||
'baz',
|
||||
jasmine.arrayContaining([2, 3])
|
||||
],
|
||||
]);
|
||||
var containing = new jasmineUnderTest.MapContaining(containingMap);
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap)).toBe(false);
|
||||
});
|
||||
|
||||
it('does not match when a value in sample has no asymmetric matches in actual', function() {
|
||||
var actualMap = new MapI([
|
||||
['foo1', 'bar'],
|
||||
['baz', [1, 2, 3, 4]],
|
||||
]);
|
||||
|
||||
var containingMap = new MapI([
|
||||
[
|
||||
jasmine.stringMatching(/^foo\d/),
|
||||
'bar'
|
||||
],
|
||||
[
|
||||
'baz',
|
||||
jasmine.arrayContaining([4, 5])
|
||||
],
|
||||
]);
|
||||
var containing = new jasmineUnderTest.MapContaining(containingMap);
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap)).toBe(false);
|
||||
});
|
||||
|
||||
it('matches recursively', function() {
|
||||
var actualMap = new MapI([
|
||||
['foo', new MapI([['foo1', 1], ['foo2', 2]])],
|
||||
[new MapI([[1, 'bar1'], [2, 'bar2']]), 'bar'],
|
||||
['other', 'any'],
|
||||
]);
|
||||
|
||||
var containingMap = new MapI([
|
||||
[
|
||||
'foo',
|
||||
new jasmineUnderTest.MapContaining(new MapI([['foo1', 1]]))
|
||||
],
|
||||
[
|
||||
new jasmineUnderTest.MapContaining(new MapI([[2, 'bar2']])),
|
||||
'bar'
|
||||
],
|
||||
]);
|
||||
var containing = new jasmineUnderTest.MapContaining(containingMap);
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap)).toBe(true);
|
||||
});
|
||||
|
||||
it('uses custom equality testers', function() {
|
||||
function tester(a, b) {
|
||||
// treat all negative numbers as equal
|
||||
return (typeof a == 'number' && typeof b == 'number') ? (a < 0 && b < 0) : a === b;
|
||||
}
|
||||
var actualMap = new MapI([['foo', -1]]);
|
||||
var containing = new jasmineUnderTest.MapContaining(new MapI([['foo', -2]]));
|
||||
|
||||
expect(containing.asymmetricMatch(actualMap, [tester])).toBe(true);
|
||||
});
|
||||
|
||||
it('does not match when actual is not a map', function() {
|
||||
var containingMap = new MapI([['foo', 'bar']]);
|
||||
expect(new jasmineUnderTest.MapContaining(containingMap).asymmetricMatch('foo')).toBe(false);
|
||||
expect(new jasmineUnderTest.MapContaining(containingMap).asymmetricMatch(-1)).toBe(false);
|
||||
expect(new jasmineUnderTest.MapContaining(containingMap).asymmetricMatch({'foo': 'bar'})).toBe(false);
|
||||
});
|
||||
|
||||
it('throws an error when sample is not a map', function() {
|
||||
expect(function() {
|
||||
new jasmineUnderTest.MapContaining({'foo': 'bar'}).asymmetricMatch(new Map());
|
||||
}).toThrowError(/You must provide a map/);
|
||||
});
|
||||
|
||||
it('defines a `jasmineToString` method', function() {
|
||||
var containing = new jasmineUnderTest.MapContaining(new Map());
|
||||
|
||||
expect(containing.jasmineToString()).toMatch(/^<jasmine\.mapContaining/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
describe('SetContaining', function() {
|
||||
function SetI(iterable) { // for IE11
|
||||
var set = new Set();
|
||||
iterable.forEach(function(v) {
|
||||
set.add(v);
|
||||
});
|
||||
return set;
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
jasmine.getEnv().requireFunctioningSets();
|
||||
});
|
||||
|
||||
|
||||
it('matches any actual set to an empty set', function() {
|
||||
var actualSet = new SetI(['foo', 'bar']);
|
||||
var containing = new jasmineUnderTest.SetContaining(new Set());
|
||||
|
||||
expect(containing.asymmetricMatch(actualSet)).toBe(true);
|
||||
});
|
||||
|
||||
it('matches when all the values in sample have matches in actual', function() {
|
||||
var actualSet = new SetI([
|
||||
{'foo': 'bar'}, 'baz', [1, 2, 3]
|
||||
]);
|
||||
|
||||
var containingSet = new SetI([
|
||||
[1, 2, 3], {'foo': 'bar'}
|
||||
]);
|
||||
var containing = new jasmineUnderTest.SetContaining(containingSet);
|
||||
|
||||
expect(containing.asymmetricMatch(actualSet)).toBe(true);
|
||||
});
|
||||
|
||||
it('does not match when a value is not in actual', function() {
|
||||
var actualSet = new SetI([
|
||||
{'foo': 'bar'}, 'baz', [1, 2, 3]
|
||||
]);
|
||||
|
||||
var containingSet = new SetI([
|
||||
[1, 2], {'foo': 'bar'}
|
||||
]);
|
||||
var containing = new jasmineUnderTest.SetContaining(containingSet);
|
||||
|
||||
expect(containing.asymmetricMatch(actualSet)).toBe(false);
|
||||
});
|
||||
|
||||
it('matches when all the values in sample have asymmetric matches in actual', function() {
|
||||
var actualSet = new SetI([
|
||||
[1, 2, 3, 4], 'other', 'foo1'
|
||||
]);
|
||||
|
||||
var containingSet = new SetI([
|
||||
jasmine.stringMatching(/^foo\d/),
|
||||
jasmine.arrayContaining([2, 3]),
|
||||
]);
|
||||
var containing = new jasmineUnderTest.SetContaining(containingSet);
|
||||
|
||||
expect(containing.asymmetricMatch(actualSet)).toBe(true);
|
||||
});
|
||||
|
||||
it('does not match when a value in sample has no asymmetric matches in actual', function() {
|
||||
var actualSet = new SetI([
|
||||
'a-foo1', [1, 2, 3, 4], 'other'
|
||||
]);
|
||||
|
||||
var containingSet = new SetI([
|
||||
jasmine.stringMatching(/^foo\d/),
|
||||
jasmine.arrayContaining([2, 3]),
|
||||
]);
|
||||
var containing = new jasmineUnderTest.SetContaining(containingSet);
|
||||
|
||||
expect(containing.asymmetricMatch(actualSet)).toBe(false);
|
||||
});
|
||||
|
||||
it('matches recursively', function() {
|
||||
var actualSet = new SetI([
|
||||
'foo', new SetI([1, 'bar', 2]), 'other'
|
||||
]);
|
||||
|
||||
var containingSet = new SetI([
|
||||
new jasmineUnderTest.SetContaining(new SetI(['bar'])), 'foo'
|
||||
]);
|
||||
var containing = new jasmineUnderTest.SetContaining(containingSet);
|
||||
|
||||
expect(containing.asymmetricMatch(actualSet)).toBe(true);
|
||||
});
|
||||
|
||||
it('uses custom equality testers', function() {
|
||||
function tester(a, b) {
|
||||
// treat all negative numbers as equal
|
||||
return (typeof a == 'number' && typeof b == 'number') ? (a < 0 && b < 0) : a === b;
|
||||
}
|
||||
var actualSet = new SetI(['foo', -1]);
|
||||
var containing = new jasmineUnderTest.SetContaining(new SetI([-2, 'foo']));
|
||||
|
||||
expect(containing.asymmetricMatch(actualSet, [tester])).toBe(true);
|
||||
});
|
||||
|
||||
it('does not match when actual is not a set', function() {
|
||||
var containingSet = new SetI(['foo']);
|
||||
expect(new jasmineUnderTest.SetContaining(containingSet).asymmetricMatch('foo')).toBe(false);
|
||||
expect(new jasmineUnderTest.SetContaining(containingSet).asymmetricMatch(1)).toBe(false);
|
||||
expect(new jasmineUnderTest.SetContaining(containingSet).asymmetricMatch(['foo'])).toBe(false);
|
||||
});
|
||||
|
||||
it('throws an error when sample is not a set', function() {
|
||||
expect(function() {
|
||||
new jasmineUnderTest.SetContaining({'foo': 'bar'}).asymmetricMatch(new Set());
|
||||
}).toThrowError(/You must provide a set/);
|
||||
});
|
||||
|
||||
it('defines a `jasmineToString` method', function() {
|
||||
var containing = new jasmineUnderTest.SetContaining(new Set());
|
||||
|
||||
expect(containing.jasmineToString()).toMatch(/^<jasmine\.setContaining/);
|
||||
});
|
||||
});
|
||||
@@ -290,6 +290,32 @@ describe("matchersUtil", function() {
|
||||
expect(jasmineUnderTest.matchersUtil.equals(containing, obj)).toBe(true);
|
||||
});
|
||||
|
||||
it("passes when MapContaining is used", function() {
|
||||
jasmine.getEnv().requireFunctioningMaps();
|
||||
|
||||
var obj = new Map();
|
||||
obj.set(1, 2);
|
||||
obj.set('foo', 'bar');
|
||||
var containing = new jasmineUnderTest.MapContaining(new Map());
|
||||
containing.sample.set('foo', 'bar');
|
||||
|
||||
expect(jasmineUnderTest.matchersUtil.equals(obj, containing)).toBe(true);
|
||||
expect(jasmineUnderTest.matchersUtil.equals(containing, obj)).toBe(true);
|
||||
});
|
||||
|
||||
it("passes when SetContaining is used", function() {
|
||||
jasmine.getEnv().requireFunctioningSets();
|
||||
|
||||
var obj = new Set();
|
||||
obj.add(1);
|
||||
obj.add('foo');
|
||||
var containing = new jasmineUnderTest.SetContaining(new Set());
|
||||
containing.sample.add(1);
|
||||
|
||||
expect(jasmineUnderTest.matchersUtil.equals(obj, containing)).toBe(true);
|
||||
expect(jasmineUnderTest.matchersUtil.equals(containing, obj)).toBe(true);
|
||||
});
|
||||
|
||||
it("passes when an asymmetric equality tester returns true", function() {
|
||||
var tester = { asymmetricMatch: function(other) { return true; } };
|
||||
|
||||
@@ -613,6 +639,26 @@ describe("matchersUtil", function() {
|
||||
testFunction('foo', 'bar');
|
||||
expect(jasmineUnderTest.matchersUtil.contains(capturedArgs, 'bar')).toBe(true);
|
||||
});
|
||||
|
||||
it("passes for set members", function() {
|
||||
jasmine.getEnv().requireFunctioningSets();
|
||||
|
||||
var setItem = {'foo': 'bar'};
|
||||
var set = new Set();
|
||||
set.add(setItem);
|
||||
|
||||
expect(jasmineUnderTest.matchersUtil.contains(set, setItem)).toBe(true);
|
||||
});
|
||||
|
||||
// documenting current behavior
|
||||
it("fails (!) for objects that equal to a set member", function() {
|
||||
jasmine.getEnv().requireFunctioningSets();
|
||||
|
||||
var set = new Set();
|
||||
set.add({'foo': 'bar'});
|
||||
|
||||
expect(jasmineUnderTest.matchersUtil.contains(set, {'foo': 'bar'})).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildMessage", function() {
|
||||
|
||||
Reference in New Issue
Block a user