Files
jasmine/src/core/Expectation.js
Steve Gravrock 10dbf8be98 Indent multiline failure messages in the output of withContext
This makes it easier to see where each failure message begins and ends.

Before:
   Some context: a
   multiline
   message

After:
   Some context:
       a
       multiline
       message
2019-09-28 12:38:28 -07:00

207 lines
5.4 KiB
JavaScript

getJasmineRequireObj().Expectation = function(j$) {
/**
* Matchers that come with Jasmine out of the box.
* @namespace matchers
*/
function Expectation(options) {
this.expector = new j$.Expector(options);
var customMatchers = options.customMatchers || {};
for (var matcherName in customMatchers) {
this[matcherName] = wrapSyncCompare(
matcherName,
customMatchers[matcherName]
);
}
}
/**
* Add some context for an {@link expect}
* @function
* @name matchers#withContext
* @since 3.3.0
* @param {String} message - Additional context to show when the matcher fails
* @return {matchers}
*/
Expectation.prototype.withContext = function withContext(message) {
return addFilter(this, new ContextAddingFilter(message));
};
/**
* Invert the matcher following this {@link expect}
* @member
* @name matchers#not
* @since 1.3.0
* @type {matchers}
* @example
* expect(something).not.toBe(true);
*/
Object.defineProperty(Expectation.prototype, 'not', {
get: function() {
return addFilter(this, syncNegatingFilter);
}
});
/**
* Asynchronous matchers.
* @namespace async-matchers
*/
function AsyncExpectation(options) {
var global = options.global || j$.getGlobal();
this.expector = new j$.Expector(options);
if (!global.Promise) {
throw new Error(
'expectAsync is unavailable because the environment does not support promises.'
);
}
var customAsyncMatchers = options.customAsyncMatchers || {};
for (var matcherName in customAsyncMatchers) {
this[matcherName] = wrapAsyncCompare(
matcherName,
customAsyncMatchers[matcherName]
);
}
}
/**
* Add some context for an {@link expectAsync}
* @function
* @name async-matchers#withContext
* @since 3.3.0
* @param {String} message - Additional context to show when the async matcher fails
* @return {async-matchers}
*/
AsyncExpectation.prototype.withContext = function withContext(message) {
return addFilter(this, new ContextAddingFilter(message));
};
/**
* Invert the matcher following this {@link expectAsync}
* @member
* @name async-matchers#not
* @type {async-matchers}
* @example
* await expectAsync(myPromise).not.toBeResolved();
* @example
* return expectAsync(myPromise).not.toBeResolved();
*/
Object.defineProperty(AsyncExpectation.prototype, 'not', {
get: function() {
return addFilter(this, asyncNegatingFilter);
}
});
function wrapSyncCompare(name, matcherFactory) {
return function() {
var result = this.expector.compare(name, matcherFactory, arguments);
this.expector.processResult(result);
};
}
function wrapAsyncCompare(name, matcherFactory) {
return function() {
var self = this;
// Capture the call stack here, before we go async, so that it will contain
// frames that are relevant to the user instead of just parts of Jasmine.
var errorForStack = j$.util.errorWithStack();
return this.expector
.compare(name, matcherFactory, arguments)
.then(function(result) {
self.expector.processResult(result, errorForStack);
});
};
}
function addCoreMatchers(prototype, matchers, wrapper) {
for (var matcherName in matchers) {
var matcher = matchers[matcherName];
prototype[matcherName] = wrapper(matcherName, matcher);
}
}
function addFilter(source, filter) {
var result = Object.create(source);
result.expector = source.expector.addFilter(filter);
return result;
}
function negatedFailureMessage(result, matcherName, args, util) {
if (result.message) {
if (j$.isFunction_(result.message)) {
return result.message();
} else {
return result.message;
}
}
args = args.slice();
args.unshift(true);
args.unshift(matcherName);
return util.buildFailureMessage.apply(null, args);
}
function negate(result) {
result.pass = !result.pass;
return result;
}
var syncNegatingFilter = {
selectComparisonFunc: function(matcher) {
function defaultNegativeCompare() {
return negate(matcher.compare.apply(null, arguments));
}
return matcher.negativeCompare || defaultNegativeCompare;
},
buildFailureMessage: negatedFailureMessage
};
var asyncNegatingFilter = {
selectComparisonFunc: function(matcher) {
function defaultNegativeCompare() {
return matcher.compare.apply(this, arguments).then(negate);
}
return matcher.negativeCompare || defaultNegativeCompare;
},
buildFailureMessage: negatedFailureMessage
};
function ContextAddingFilter(message) {
this.message = message;
}
ContextAddingFilter.prototype.modifyFailureMessage = function(msg) {
var nl = msg.indexOf('\n');
if (nl === -1) {
return this.message + ': ' + msg;
} else {
return this.message + ':\n' + indent(msg);
}
};
function indent(s) {
return s.replace(/^/gm, ' ');
}
return {
factory: function(options) {
return new Expectation(options || {});
},
addCoreMatchers: function(matchers) {
addCoreMatchers(Expectation.prototype, matchers, wrapSyncCompare);
},
asyncFactory: function(options) {
return new AsyncExpectation(options || {});
},
addAsyncCoreMatchers: function(matchers) {
addCoreMatchers(AsyncExpectation.prototype, matchers, wrapAsyncCompare);
}
};
};