Files
jasmine/spec/core/asymmetric_equality/SetContainingSpec.js
Steve Gravrock dec67bd535 Don't require matchers and asymmetric equality testers to pass custom object formatters back to Jasmine
This makes it easier to write high quality matchers and asymmetric equality
testers, and is also a step toward supporting custom object formatters.

Previously, Jasmine passed custom object formatters as the second argument
to matcher factories and as and the second argument to asymmetric equality
testers' `asymmetricMatch` method. Matchers and asymmetric equality testers
were responsible for passing the custom object formatters to methods like
`matchersUtil#equals`:

  function toEqual(util, customEqualityTesters) {
    return {
      compare: function(actual, expected) {
        // ...
        result.pass = util.equals(actual, expected, customEqualityTesters, diffBuilder);

And:

  ArrayContaining.prototype.asymmetricMatch = function(other, customTesters) {
    // ...
    for (var i = 0; i < this.sample.length; i++) {
      var item = this.sample[i];
      if (!j$.matchersUtil.contains(other, item, customTesters)) {
        return false;
      }
    }

With this change, that is no longer necessary. Matchers and asymmetric
equality testers can ignore the existence of custom equality testers and
still fully support them:

  function toEqual(util) {
    return {
      compare: function(actual, expected) {
        // ...
        result.pass = util.equals(actual, expected, diffBuilder);

And:

  ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) {
    // ...
    for (var i = 0; i < this.sample.length; i++) {
      var item = this.sample[i];
      if (!matchersUtil.contains(other, item)) {
        return false;
      }
    }

The old interfaces are still supported, for now, but will be deprecated
in a future commit and removed in the next major release after that.

In addition to making matchers and custom equality testers simpler,
this change sets the stage for adding support for custom object
formatters. Those will be architecturally similar to custom equality
testers, and by injecting a `MatchersUtil` instance everywhere we can
add them without requiring user code to pass them around as used to be
the case with custom object formatters.
2020-02-10 17:25:50 -08:00

125 lines
4.1 KiB
JavaScript

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);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(containing.asymmetricMatch(actualSet, matchersUtil)).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);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(containing.asymmetricMatch(actualSet, matchersUtil)).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([
jasmineUnderTest.stringMatching(/^foo\d/),
jasmineUnderTest.arrayContaining([2, 3]),
]);
var containing = new jasmineUnderTest.SetContaining(containingSet);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(containing.asymmetricMatch(actualSet, matchersUtil)).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);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(containing.asymmetricMatch(actualSet, matchersUtil)).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);
var matchersUtil = new jasmineUnderTest.MatchersUtil();
expect(containing.asymmetricMatch(actualSet, matchersUtil)).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']));
var matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [tester]});
expect(containing.asymmetricMatch(actualSet, matchersUtil)).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/);
});
});