Added support for custom object formatters

Custom object formatters allow users to customize how an object is
stringified in matcher failure messages. This can already be done by
adding a `jasmineToString` method to the objects in question. But
it's not always desirable or possible to do that, particularly when
objects of a given "type" do not inherit from a specific prototype.
For instance, suppose a web service returns a list of foos that are
deserialized from JSON, e.g.:

   { fooId: 42, /* more properties */ }

The only way to define `jasmineToString` on those is by writing code to
add it to each instance at runtime. But a custom object formatter can
recognize that the object it's looking at is a foo and format it
accordingly:

   jasmine.addCustomObjectFormatter(function(obj) {
      if (typeof obj.fooId !== 'number') {
            return undefined;
        }

        return '[Foo with ID ' + obj.fooId + ']';
    });

Unlike `jasmineToString`, custom object formatters are scoped to a
particular spec or suite and don't require any changes to the code
under test.
This commit is contained in:
Steve Gravrock
2020-01-11 14:51:12 -08:00
committed by Steve Gravrock
parent 1f23f1e4d2
commit 25816a6e77
25 changed files with 591 additions and 73 deletions

View File

@@ -1,9 +1,11 @@
getJasmineRequireObj().makePrettyPrinter = function(j$) {
function SinglePrettyPrintRun() {
function SinglePrettyPrintRun(customObjectFormatters, pp) {
this.customObjectFormatters_ = customObjectFormatters;
this.ppNestLevel_ = 0;
this.seen = [];
this.length = 0;
this.stringParts = [];
this.pp_ = pp;
}
function hasCustomToString(value) {
@@ -24,7 +26,11 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
SinglePrettyPrintRun.prototype.format = function(value) {
this.ppNestLevel_++;
try {
if (j$.util.isUndefined(value)) {
var customFormatResult = this.applyCustomFormatters_(value);
if (customFormatResult) {
this.emitScalar(customFormatResult);
} else if (j$.util.isUndefined(value)) {
this.emitScalar('undefined');
} else if (value === null) {
this.emitScalar('null');
@@ -33,7 +39,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} else if (value === j$.getGlobal()) {
this.emitScalar('<global>');
} else if (value.jasmineToString) {
this.emitScalar(value.jasmineToString());
this.emitScalar(value.jasmineToString(this.pp_));
} else if (typeof value === 'string') {
this.emitString(value);
} else if (j$.isSpy(value)) {
@@ -95,6 +101,18 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
}
};
SinglePrettyPrintRun.prototype.applyCustomFormatters_ = function(value) {
var i, result;
for (i = 0; i < this.customObjectFormatters_.length; i++) {
result = this.customObjectFormatters_[i](value);
if (result !== undefined) {
return result;
}
}
};
SinglePrettyPrintRun.prototype.iterateObject = function(obj, fn) {
var objKeys = keys(obj, j$.isArray_(obj));
var isGetter = function isGetter(prop) {};
@@ -365,11 +383,16 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
return extraKeys;
}
return function() {
return function(value) {
var prettyPrinter = new SinglePrettyPrintRun();
return function(customObjectFormatters) {
var pp = function(value) {
var prettyPrinter = new SinglePrettyPrintRun(
customObjectFormatters || [],
pp
);
prettyPrinter.format(value);
return prettyPrinter.stringParts.join('');
};
return pp;
};
};