Added a jasmine.exactly asymmetric equality tester

This commit is contained in:
Steve Gravrock
2022-05-14 17:01:38 -07:00
parent c24b2f5a73
commit 0c87d47318
6 changed files with 93 additions and 13 deletions

View File

@@ -71,20 +71,9 @@ describe('Suite', function() {
suite.beforeAll(outerBefore);
suite.beforeAll(innerBefore);
function sameInstance(expected) {
return {
asymmetricMatch: function(actual) {
return actual === expected;
},
jasmineToString: function() {
return `<same instance as ${expected}>`;
}
};
}
expect(suite.beforeAllFns).toEqual([
{ fn: outerBefore.fn, type: 'beforeAll', suite: sameInstance(suite) },
{ fn: innerBefore.fn, type: 'beforeAll', suite: sameInstance(suite) }
{ fn: outerBefore.fn, type: 'beforeAll', suite: jasmine.exactly(suite) },
{ fn: innerBefore.fn, type: 'beforeAll', suite: jasmine.exactly(suite) }
]);
});

View File

@@ -0,0 +1,30 @@
describe('Exactly', function() {
it('passes for primitives that are ===', function() {
const exactly = new jasmineUnderTest.Exactly(17);
expect(exactly.asymmetricMatch(17)).toBeTrue();
});
it('fails for primitives that are not ===', function() {
const exactly = new jasmineUnderTest.Exactly(42);
expect(exactly.asymmetricMatch('42')).toBeFalse();
});
it('passes for the same object instance', function() {
const obj = {};
const exactly = new jasmineUnderTest.Exactly(obj);
expect(exactly.asymmetricMatch(obj)).toBeTrue();
});
it('fails for different object instances, even if they are deep value equal', function() {
const exactly = new jasmineUnderTest.Exactly({});
expect(exactly.asymmetricMatch({})).toBeFalse();
});
it('describes itself for use in diffs and pretty printing', function() {
const exactly = new jasmineUnderTest.Exactly({ foo: ['bar'] });
const pp = jasmineUnderTest.basicPrettyPrinter_;
expect(exactly.jasmineToString(pp)).toEqual(
"<jasmine.exactly(Object({ foo: [ 'bar' ] }))>"
);
});
});