Merge branch 'custom-object-formatters' into cof-merge-candidate
This commit is contained in:
@@ -115,4 +115,36 @@ describe('Custom Async Matchers (Integration)', function() {
|
||||
env.addReporter({ specDone: specExpectations, jasmineDone: done });
|
||||
env.execute();
|
||||
});
|
||||
|
||||
it("provides custom equality testers to the matcher factory via matchersUtil", function(done) {
|
||||
jasmine.getEnv().requirePromises();
|
||||
|
||||
var matcherFactory = function (matchersUtil) {
|
||||
return {
|
||||
compare: function (actual, expected) {
|
||||
return Promise.resolve({pass: matchersUtil.equals(actual[0], expected)});
|
||||
}
|
||||
};
|
||||
},
|
||||
customEqualityFn = jasmine.createSpy("customEqualityFn").and.callFake(function (a, b) {
|
||||
return a.toString() === b;
|
||||
});
|
||||
|
||||
env.it("spec with expectation", function() {
|
||||
env.addCustomEqualityTester(customEqualityFn);
|
||||
env.addAsyncMatchers({
|
||||
toBeArrayWithFirstElement: matcherFactory
|
||||
});
|
||||
|
||||
return env.expectAsync([1, 2]).toBeArrayWithFirstElement("1");
|
||||
});
|
||||
|
||||
var specExpectations = function(result) {
|
||||
expect(customEqualityFn).toHaveBeenCalledWith(1, "1");
|
||||
expect(result.failedExpectations).toEqual([]);
|
||||
};
|
||||
|
||||
env.addReporter({ specDone: specExpectations, jasmineDone: done });
|
||||
env.execute();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
describe("Custom Matchers (Integration)", function() {
|
||||
describe("Custom Matchers (Integration)", function () {
|
||||
var env;
|
||||
var fakeTimer;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
env = new jasmineUnderTest.Env();
|
||||
env.configure({random: false});
|
||||
});
|
||||
@@ -11,13 +11,13 @@ describe("Custom Matchers (Integration)", function() {
|
||||
env.cleanup_();
|
||||
});
|
||||
|
||||
it("allows adding more matchers local to a spec", function(done) {
|
||||
env.it('spec defining a custom matcher', function() {
|
||||
it("allows adding more matchers local to a spec", function (done) {
|
||||
env.it('spec defining a custom matcher', function () {
|
||||
env.addMatchers({
|
||||
matcherForSpec: function() {
|
||||
matcherForSpec: function () {
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
return { pass: false, message: "matcherForSpec: actual: " + actual + "; expected: " + expected };
|
||||
compare: function (actual, expected) {
|
||||
return {pass: false, message: "matcherForSpec: actual: " + actual + "; expected: " + expected};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,8 @@ describe("Custom Matchers (Integration)", function() {
|
||||
|
||||
it("supports asymmetric equality testers that take a list of custom equality testers", function(done) {
|
||||
// TODO: remove this in the next major release.
|
||||
spyOn(jasmineUnderTest, 'getEnv').and.returnValue(env);
|
||||
|
||||
env.it("spec using custom asymmetric equality tester", function() {
|
||||
var customEqualityFn = function(a, b) {
|
||||
if (a === 2 && b === "two") {
|
||||
@@ -233,4 +235,34 @@ describe("Custom Matchers (Integration)", function() {
|
||||
env.addReporter({ specDone: specExpectations, jasmineDone: done });
|
||||
env.execute();
|
||||
});
|
||||
|
||||
it("provides custom equality testers to the matcher factory via matchersUtil", function (done) {
|
||||
var matcherFactory = function (matchersUtil) {
|
||||
return {
|
||||
compare: function (actual, expected) {
|
||||
return {pass: matchersUtil.equals(actual[0], expected)};
|
||||
}
|
||||
};
|
||||
},
|
||||
customEqualityFn = jasmine.createSpy("customEqualityFn").and.callFake(function (a, b) {
|
||||
return a.toString() === b;
|
||||
});
|
||||
|
||||
env.it("spec with expectation", function () {
|
||||
env.addCustomEqualityTester(customEqualityFn);
|
||||
env.addMatchers({
|
||||
toBeArrayWithFirstElement: matcherFactory
|
||||
});
|
||||
|
||||
env.expect([1, 2]).toBeArrayWithFirstElement("1");
|
||||
});
|
||||
|
||||
var specExpectations = function (result) {
|
||||
expect(customEqualityFn).toHaveBeenCalledWith(1, "1");
|
||||
expect(result.failedExpectations).toEqual([]);
|
||||
};
|
||||
|
||||
env.addReporter({specDone: specExpectations, jasmineDone: done});
|
||||
env.execute();
|
||||
});
|
||||
});
|
||||
|
||||
67
spec/core/integration/CustomObjectFormatterSpec.js
Normal file
67
spec/core/integration/CustomObjectFormatterSpec.js
Normal file
@@ -0,0 +1,67 @@
|
||||
describe("Custom object formatters", function() {
|
||||
var env;
|
||||
|
||||
beforeEach(function() {
|
||||
env = new jasmineUnderTest.Env();
|
||||
env.configure({random: false});
|
||||
});
|
||||
|
||||
it("scopes custom object formatters to a spec", function(done) {
|
||||
env.it('a spec with custom pretty-printer', function() {
|
||||
env.addCustomObjectFormatter(function(obj) { return 'custom(' + obj + ')'; });
|
||||
env.expect(42).toBeUndefined();
|
||||
});
|
||||
|
||||
env.it('a spec without custom pretty-printer', function() {
|
||||
env.expect(42).toBeUndefined();
|
||||
});
|
||||
|
||||
var specResults = [];
|
||||
var specDone = function(result) {
|
||||
specResults.push(result);
|
||||
};
|
||||
var expectations = function() {
|
||||
expect(specResults[0].failedExpectations[0].message).toEqual("Expected custom(42) to be undefined.");
|
||||
expect(specResults[1].failedExpectations[0].message).toEqual("Expected 42 to be undefined.");
|
||||
done();
|
||||
};
|
||||
env.addReporter({ specDone:specDone, jasmineDone: expectations});
|
||||
|
||||
env.execute();
|
||||
});
|
||||
|
||||
it("scopes custom object formatters to a suite", function(done) {
|
||||
env.it('a spec without custom pretty-printer', function() {
|
||||
env.expect(42).toBeUndefined();
|
||||
});
|
||||
|
||||
env.describe('with custom pretty-printer', function() {
|
||||
env.beforeEach(function() {
|
||||
env.addCustomObjectFormatter(function(obj) { return 'custom(' + obj + ')'; });
|
||||
});
|
||||
|
||||
env.it('a spec', function() {
|
||||
env.expect(42).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
var specResults = [];
|
||||
var specDone = function(result) {
|
||||
specResults.push(result);
|
||||
};
|
||||
var expectations = function() {
|
||||
expect(specResults[0].failedExpectations[0].message).toEqual("Expected 42 to be undefined.");
|
||||
expect(specResults[1].failedExpectations[0].message).toEqual("Expected custom(42) to be undefined.");
|
||||
done();
|
||||
};
|
||||
env.addReporter({ specDone:specDone, jasmineDone: expectations});
|
||||
|
||||
env.execute();
|
||||
});
|
||||
|
||||
it("throws an exception if you try to add a custom object formatter outside a runable", function() {
|
||||
expect(function() {
|
||||
env.addCustomObjectFormatter(function() {});
|
||||
}).toThrowError('Custom object formatters must be added in a before function or a spec')
|
||||
});
|
||||
});
|
||||
@@ -2584,4 +2584,33 @@ describe("Env integration", function() {
|
||||
|
||||
env.execute();
|
||||
});
|
||||
|
||||
it("supports asymmetric equality testers that take a matchersUtil", function(done) {
|
||||
var env = new jasmineUnderTest.Env();
|
||||
|
||||
env.it("spec using custom asymmetric equality tester", function() {
|
||||
var customEqualityFn = function(a, b) {
|
||||
if (a === 2 && b === "two") {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
var arrayWithFirstElement = function(sample) {
|
||||
return {
|
||||
asymmetricMatch: function (actual, matchersUtil) {
|
||||
return matchersUtil.equals(sample, actual[0]);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
env.addCustomEqualityTester(customEqualityFn);
|
||||
env.expect(["two"]).toEqual(arrayWithFirstElement(2));
|
||||
});
|
||||
|
||||
var specExpectations = function(result) {
|
||||
expect(result.status).toEqual('passed');
|
||||
};
|
||||
|
||||
env.addReporter({ specDone: specExpectations, jasmineDone: done });
|
||||
env.execute();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -56,6 +56,27 @@ describe('Matchers (Integration)', function() {
|
||||
});
|
||||
}
|
||||
|
||||
function verifyFailsWithCustomObjectFormatters(config) {
|
||||
it('uses custom object formatters', function(done) {
|
||||
env.it('a spec', function () {
|
||||
env.addCustomObjectFormatter(config.formatter);
|
||||
config.expectations(env);
|
||||
});
|
||||
|
||||
var specExpectations = function (result) {
|
||||
expect(result.status).toEqual('failed');
|
||||
expect(result.failedExpectations.length)
|
||||
.withContext('Number of failed expectations')
|
||||
.toEqual(1);
|
||||
expect(result.failedExpectations[0].message)
|
||||
.toEqual(config.expectedMessage);
|
||||
};
|
||||
|
||||
env.addReporter({specDone: specExpectations, jasmineDone: done});
|
||||
env.execute();
|
||||
});
|
||||
}
|
||||
|
||||
function verifyPassesAsync(expectations) {
|
||||
it('passes', function(done) {
|
||||
jasmine.getEnv().requirePromises();
|
||||
@@ -107,6 +128,30 @@ describe('Matchers (Integration)', function() {
|
||||
});
|
||||
}
|
||||
|
||||
function verifyFailsWithCustomObjectFormattersAsync(config) {
|
||||
it('uses custom object formatters', function(done) {
|
||||
var env = new jasmineUnderTest.Env();
|
||||
jasmine.getEnv().requirePromises();
|
||||
env.it('a spec', function () {
|
||||
env.addCustomObjectFormatter(config.formatter);
|
||||
return config.expectations(env);
|
||||
});
|
||||
|
||||
var specExpectations = function (result) {
|
||||
expect(result.status).toEqual('failed');
|
||||
expect(result.failedExpectations.length)
|
||||
.withContext('Number of failed expectations')
|
||||
.toEqual(1);
|
||||
expect(result.failedExpectations[0].message)
|
||||
.toEqual(config.expectedMessage);
|
||||
};
|
||||
|
||||
env.addReporter({specDone: specExpectations, jasmineDone: done});
|
||||
env.execute();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
describe('nothing', function() {
|
||||
verifyPasses(function(env) {
|
||||
env.expect().nothing();
|
||||
@@ -223,6 +268,16 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(2).toBeNaN();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
env.expect(1).toBeNaN();
|
||||
},
|
||||
expectedMessage: 'Expected |1| to be NaN.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeNegativeInfinity', function() {
|
||||
@@ -233,6 +288,16 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(2).toBeNegativeInfinity();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
env.expect(1).toBeNegativeInfinity();
|
||||
},
|
||||
expectedMessage: 'Expected |1| to be -Infinity.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeNull', function() {
|
||||
@@ -253,6 +318,16 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(2).toBePositiveInfinity();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
env.expect(1).toBePositiveInfinity();
|
||||
},
|
||||
expectedMessage: 'Expected |1| to be Infinity.'
|
||||
})
|
||||
});
|
||||
|
||||
describe('toBeResolved', function() {
|
||||
@@ -276,6 +351,17 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFailsAsync(function(env) {
|
||||
return env.expectAsync(Promise.resolve('foo')).toBeResolvedTo('bar');
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormattersAsync({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
return env.expectAsync(Promise.resolve('x')).toBeResolvedTo('y');
|
||||
},
|
||||
expectedMessage: 'Expected a promise to be resolved to |y| ' +
|
||||
'but it was resolved to |x|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeRejected', function() {
|
||||
@@ -299,6 +385,17 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFailsAsync(function(env) {
|
||||
return env.expectAsync(Promise.resolve()).toBeRejectedWith('nope');
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormattersAsync({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
return env.expectAsync(Promise.reject('x')).toBeRejectedWith('y');
|
||||
},
|
||||
expectedMessage: 'Expected a promise to be rejected with |y| ' +
|
||||
'but it was rejected with |x|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeRejectedWithError', function() {
|
||||
@@ -309,6 +406,17 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFailsAsync(function(env) {
|
||||
return env.expectAsync(Promise.resolve()).toBeRejectedWithError(Error);
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormattersAsync({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
return env.expectAsync(Promise.reject('foo')).toBeRejectedWithError('foo');
|
||||
},
|
||||
expectedMessage: 'Expected a promise to be rejected with Error: |foo| ' +
|
||||
'but it was rejected with |foo|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toBeTrue', function() {
|
||||
@@ -365,6 +473,20 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect('a').toEqual('b');
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
if (val === 5) {
|
||||
return "five"
|
||||
} else if (val === 4) {
|
||||
return "four"
|
||||
}
|
||||
},
|
||||
expectations: function(env) {
|
||||
env.expect([{foo: 4}]).toEqual([{foo: 5}]);
|
||||
},
|
||||
expectedMessage: 'Expected $[0].foo = four to equal five.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toHaveBeenCalled', function() {
|
||||
@@ -423,6 +545,19 @@ describe('Matchers (Integration)', function() {
|
||||
var spy = env.createSpy();
|
||||
env.expect(spy).toHaveBeenCalledWith('foo');
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
var spy = env.createSpy('foo');
|
||||
env.expect(spy).toHaveBeenCalledWith('x');
|
||||
},
|
||||
expectedMessage: 'Expected spy foo to have been called with:\n' +
|
||||
' |x|\n' +
|
||||
'but it was never called.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toHaveClass', function() {
|
||||
@@ -455,12 +590,28 @@ describe('Matchers (Integration)', function() {
|
||||
|
||||
describe('toThrow', function() {
|
||||
verifyPasses(function(env) {
|
||||
env.expect(function() { throw new Error(); }).toThrow();
|
||||
env.addCustomEqualityTester(function(a, b) {
|
||||
return a.toString() === b.toString();
|
||||
});
|
||||
env.expect(function() { throw '5'; }).toThrow(5);
|
||||
});
|
||||
|
||||
verifyFails(function(env) {
|
||||
env.expect(function() {}).toThrow();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
var spy = env.createSpy('foo');
|
||||
env.expect(function() {
|
||||
throw 'x'
|
||||
}).not.toThrow();
|
||||
},
|
||||
expectedMessage: 'Expected function not to throw, but it threw |x|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toThrowError', function() {
|
||||
@@ -471,6 +622,19 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(function() { }).toThrowError();
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
var spy = env.createSpy('foo');
|
||||
env.expect(function() {
|
||||
throw 'x'
|
||||
}).toThrowError();
|
||||
},
|
||||
expectedMessage: 'Expected function to throw an Error, but it threw |x|.'
|
||||
});
|
||||
});
|
||||
|
||||
describe('toThrowMatching', function() {
|
||||
@@ -485,5 +649,21 @@ describe('Matchers (Integration)', function() {
|
||||
verifyFails(function(env) {
|
||||
env.expect(throws).toThrowMatching(function() { return false; });
|
||||
});
|
||||
|
||||
verifyFailsWithCustomObjectFormatters({
|
||||
formatter: function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
expectations: function(env) {
|
||||
var spy = env.createSpy('foo');
|
||||
env.expect(function() {
|
||||
throw new Error('nope')
|
||||
}).toThrowMatching(function() {
|
||||
return false;
|
||||
});
|
||||
},
|
||||
expectedMessage: 'Expected function to throw an exception matching ' +
|
||||
'a predicate, but it threw Error with message |nope|.'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user