Converted DiffBuilder, ObjectPath, MismatchTree, and SinglePrettyPrintRun to ES6 classes
This commit is contained in:
@@ -4925,81 +4925,102 @@ getJasmineRequireObj().toBeResolvedTo = function(j$) {
|
||||
};
|
||||
|
||||
getJasmineRequireObj().DiffBuilder = function(j$) {
|
||||
return function DiffBuilder(config) {
|
||||
const prettyPrinter =
|
||||
class DiffBuilder {
|
||||
constructor(config) {
|
||||
this.prettyPrinter_ =
|
||||
(config || {}).prettyPrinter || j$.makePrettyPrinter();
|
||||
const mismatches = new j$.MismatchTree();
|
||||
let path = new j$.ObjectPath();
|
||||
let actualRoot = undefined;
|
||||
let expectedRoot = undefined;
|
||||
this.mismatches_ = new j$.MismatchTree();
|
||||
this.path_ = new j$.ObjectPath();
|
||||
this.actualRoot_ = undefined;
|
||||
this.expectedRoot_ = undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
setRoots: function(actual, expected) {
|
||||
actualRoot = actual;
|
||||
expectedRoot = expected;
|
||||
},
|
||||
setRoots(actual, expected) {
|
||||
this.actualRoot_ = actual;
|
||||
this.expectedRoot_ = expected;
|
||||
}
|
||||
|
||||
recordMismatch: function(formatter) {
|
||||
mismatches.add(path, formatter);
|
||||
},
|
||||
recordMismatch(formatter) {
|
||||
this.mismatches_.add(this.path_, formatter);
|
||||
}
|
||||
|
||||
getMessage: function() {
|
||||
getMessage() {
|
||||
const messages = [];
|
||||
|
||||
mismatches.traverse(function(path, isLeaf, formatter) {
|
||||
const { actual, expected } = dereferencePath(
|
||||
path,
|
||||
actualRoot,
|
||||
expectedRoot,
|
||||
prettyPrinter
|
||||
);
|
||||
this.mismatches_.traverse((path, isLeaf, formatter) => {
|
||||
const { actual, expected } = this.dereferencePath_(path);
|
||||
|
||||
if (formatter) {
|
||||
messages.push(formatter(actual, expected, path, prettyPrinter));
|
||||
messages.push(formatter(actual, expected, path, this.prettyPrinter_));
|
||||
return true;
|
||||
}
|
||||
|
||||
const actualCustom = prettyPrinter.customFormat_(actual);
|
||||
const expectedCustom = prettyPrinter.customFormat_(expected);
|
||||
const actualCustom = this.prettyPrinter_.customFormat_(actual);
|
||||
const expectedCustom = this.prettyPrinter_.customFormat_(expected);
|
||||
const useCustom = !(
|
||||
j$.util.isUndefined(actualCustom) &&
|
||||
j$.util.isUndefined(expectedCustom)
|
||||
);
|
||||
|
||||
if (useCustom) {
|
||||
messages.push(
|
||||
wrapPrettyPrinted(actualCustom, expectedCustom, path)
|
||||
);
|
||||
messages.push(wrapPrettyPrinted(actualCustom, expectedCustom, path));
|
||||
return false; // don't recurse further
|
||||
}
|
||||
|
||||
if (isLeaf) {
|
||||
messages.push(
|
||||
defaultFormatter(actual, expected, path, prettyPrinter)
|
||||
);
|
||||
messages.push(this.defaultFormatter_(actual, expected, path));
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return messages.join('\n');
|
||||
},
|
||||
}
|
||||
|
||||
withPath: function(pathComponent, block) {
|
||||
const oldPath = path;
|
||||
path = path.add(pathComponent);
|
||||
withPath(pathComponent, block) {
|
||||
const oldPath = this.path_;
|
||||
this.path_ = this.path_.add(pathComponent);
|
||||
block();
|
||||
path = oldPath;
|
||||
this.path_ = oldPath;
|
||||
}
|
||||
|
||||
dereferencePath_(objectPath) {
|
||||
let actual = this.actualRoot_;
|
||||
let expected = this.expectedRoot_;
|
||||
|
||||
const handleAsymmetricExpected = () => {
|
||||
if (
|
||||
j$.isAsymmetricEqualityTester_(expected) &&
|
||||
j$.isFunction_(expected.valuesForDiff_)
|
||||
) {
|
||||
const asymmetricResult = expected.valuesForDiff_(
|
||||
actual,
|
||||
this.prettyPrinter_
|
||||
);
|
||||
expected = asymmetricResult.self;
|
||||
actual = asymmetricResult.other;
|
||||
}
|
||||
};
|
||||
|
||||
function defaultFormatter(actual, expected, path, prettyPrinter) {
|
||||
handleAsymmetricExpected();
|
||||
|
||||
for (const pc of objectPath.components) {
|
||||
actual = actual[pc];
|
||||
expected = expected[pc];
|
||||
handleAsymmetricExpected();
|
||||
}
|
||||
|
||||
return { actual: actual, expected: expected };
|
||||
}
|
||||
|
||||
defaultFormatter_(actual, expected, path) {
|
||||
return wrapPrettyPrinted(
|
||||
prettyPrinter(actual),
|
||||
prettyPrinter(expected),
|
||||
this.prettyPrinter_(actual),
|
||||
this.prettyPrinter_(expected),
|
||||
path
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function wrapPrettyPrinted(actual, expected, path) {
|
||||
return (
|
||||
@@ -5012,30 +5033,8 @@ getJasmineRequireObj().DiffBuilder = function(j$) {
|
||||
'.'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
function dereferencePath(objectPath, actual, expected, pp) {
|
||||
function handleAsymmetricExpected() {
|
||||
if (
|
||||
j$.isAsymmetricEqualityTester_(expected) &&
|
||||
j$.isFunction_(expected.valuesForDiff_)
|
||||
) {
|
||||
const asymmetricResult = expected.valuesForDiff_(actual, pp);
|
||||
expected = asymmetricResult.self;
|
||||
actual = asymmetricResult.other;
|
||||
}
|
||||
}
|
||||
|
||||
handleAsymmetricExpected();
|
||||
|
||||
for (const pc of objectPath.components) {
|
||||
actual = actual[pc];
|
||||
expected = expected[pc];
|
||||
handleAsymmetricExpected();
|
||||
}
|
||||
|
||||
return { actual: actual, expected: expected };
|
||||
}
|
||||
return DiffBuilder;
|
||||
};
|
||||
|
||||
getJasmineRequireObj().MatchersUtil = function(j$) {
|
||||
@@ -5735,14 +5734,15 @@ getJasmineRequireObj().MismatchTree = function(j$) {
|
||||
the expected and actual object graphs. MismatchTree maintains that context
|
||||
and provides it via the traverse method.
|
||||
*/
|
||||
function MismatchTree(path) {
|
||||
class MismatchTree {
|
||||
constructor(path) {
|
||||
this.path = path || new j$.ObjectPath([]);
|
||||
this.formatter = undefined;
|
||||
this.children = [];
|
||||
this.isMismatch = false;
|
||||
}
|
||||
|
||||
MismatchTree.prototype.add = function(path, formatter) {
|
||||
add(path, formatter) {
|
||||
if (path.depth() === 0) {
|
||||
this.formatter = formatter;
|
||||
this.isMismatch = true;
|
||||
@@ -5758,9 +5758,9 @@ getJasmineRequireObj().MismatchTree = function(j$) {
|
||||
|
||||
child.add(path, formatter);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
MismatchTree.prototype.traverse = function(visit) {
|
||||
traverse(visit) {
|
||||
const hasChildren = this.children.length > 0;
|
||||
|
||||
if (this.isMismatch || hasChildren) {
|
||||
@@ -5770,14 +5770,15 @@ getJasmineRequireObj().MismatchTree = function(j$) {
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
MismatchTree.prototype.child = function(key) {
|
||||
child(key) {
|
||||
return this.children.find(child => {
|
||||
const pathEls = child.path.components;
|
||||
return pathEls[pathEls.length - 1] === key;
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return MismatchTree;
|
||||
};
|
||||
@@ -5817,29 +5818,31 @@ getJasmineRequireObj().NullDiffBuilder = function(j$) {
|
||||
};
|
||||
|
||||
getJasmineRequireObj().ObjectPath = function(j$) {
|
||||
function ObjectPath(components) {
|
||||
class ObjectPath {
|
||||
constructor(components) {
|
||||
this.components = components || [];
|
||||
}
|
||||
|
||||
ObjectPath.prototype.toString = function() {
|
||||
toString() {
|
||||
if (this.components.length) {
|
||||
return '$' + this.components.map(formatPropertyAccess).join('');
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
ObjectPath.prototype.add = function(component) {
|
||||
add(component) {
|
||||
return new ObjectPath(this.components.concat([component]));
|
||||
};
|
||||
}
|
||||
|
||||
ObjectPath.prototype.shift = function() {
|
||||
shift() {
|
||||
return new ObjectPath(this.components.slice(1));
|
||||
};
|
||||
}
|
||||
|
||||
ObjectPath.prototype.depth = function() {
|
||||
depth() {
|
||||
return this.components.length;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function formatPropertyAccess(prop) {
|
||||
if (typeof prop === 'number' || typeof prop === 'symbol') {
|
||||
@@ -5850,7 +5853,7 @@ getJasmineRequireObj().ObjectPath = function(j$) {
|
||||
return '.' + prop;
|
||||
}
|
||||
|
||||
return "['" + prop + "']";
|
||||
return `['${prop}']`;
|
||||
}
|
||||
|
||||
function isValidIdentifier(string) {
|
||||
@@ -6415,7 +6418,7 @@ getJasmineRequireObj().toEqual = function(j$) {
|
||||
var result = {
|
||||
pass: false
|
||||
},
|
||||
diffBuilder = j$.DiffBuilder({ prettyPrinter: matchersUtil.pp });
|
||||
diffBuilder = new j$.DiffBuilder({ prettyPrinter: matchersUtil.pp });
|
||||
|
||||
result.pass = matchersUtil.equals(actual, expected, diffBuilder);
|
||||
|
||||
@@ -7521,7 +7524,8 @@ getJasmineRequireObj().NeverSkipPolicy = function(j$) {
|
||||
};
|
||||
|
||||
getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
function SinglePrettyPrintRun(customObjectFormatters, pp) {
|
||||
class SinglePrettyPrintRun {
|
||||
constructor(customObjectFormatters, pp) {
|
||||
this.customObjectFormatters_ = customObjectFormatters;
|
||||
this.ppNestLevel_ = 0;
|
||||
this.seen = [];
|
||||
@@ -7530,22 +7534,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
this.pp_ = pp;
|
||||
}
|
||||
|
||||
function hasCustomToString(value) {
|
||||
// value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g.
|
||||
// iframe, web worker)
|
||||
try {
|
||||
return (
|
||||
j$.isFunction_(value.toString) &&
|
||||
value.toString !== Object.prototype.toString &&
|
||||
value.toString() !== Object.prototype.toString.call(value)
|
||||
);
|
||||
} catch (e) {
|
||||
// The custom toString() threw.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.format = function(value) {
|
||||
format(value) {
|
||||
this.ppNestLevel_++;
|
||||
try {
|
||||
const customFormatResult = this.applyCustomFormatters_(value);
|
||||
@@ -7621,13 +7610,13 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
} finally {
|
||||
this.ppNestLevel_--;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.applyCustomFormatters_ = function(value) {
|
||||
applyCustomFormatters_(value) {
|
||||
return customFormat(value, this.customObjectFormatters_);
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.iterateObject = function(obj, fn) {
|
||||
iterateObject(obj, fn) {
|
||||
const objKeys = j$.MatchersUtil.keys(obj, j$.isArray_(obj));
|
||||
const length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
|
||||
|
||||
@@ -7636,17 +7625,17 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
return objKeys.length > length;
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitScalar = function(value) {
|
||||
emitScalar(value) {
|
||||
this.append(value);
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitString = function(value) {
|
||||
emitString(value) {
|
||||
this.append("'" + value + "'");
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitArray = function(array) {
|
||||
emitArray(array) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Array');
|
||||
return;
|
||||
@@ -7681,9 +7670,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
this.append(' ]');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitSet = function(set) {
|
||||
emitSet(set) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Set');
|
||||
return;
|
||||
@@ -7706,9 +7695,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
this.append(', ...');
|
||||
}
|
||||
this.append(' )');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitMap = function(map) {
|
||||
emitMap(map) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Map');
|
||||
return;
|
||||
@@ -7731,9 +7720,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
this.append(', ...');
|
||||
}
|
||||
this.append(' )');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitObject = function(obj) {
|
||||
emitObject(obj) {
|
||||
const ctor = obj.constructor;
|
||||
const constructorName =
|
||||
typeof ctor === 'function' && obj instanceof ctor
|
||||
@@ -7764,9 +7753,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
this.append(' })');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitTypedArray = function(arr) {
|
||||
emitTypedArray(arr) {
|
||||
const constructorName = j$.fnNameFor(arr.constructor);
|
||||
const limitedArray = Array.prototype.slice.call(
|
||||
arr,
|
||||
@@ -7780,9 +7769,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
this.append(constructorName + ' [ ' + itemsString + ' ]');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitDomElement = function(el) {
|
||||
emitDomElement(el) {
|
||||
const tagName = el.tagName.toLowerCase();
|
||||
let out = '<' + tagName;
|
||||
|
||||
@@ -7801,9 +7790,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
this.append(out);
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.formatProperty = function(obj, property) {
|
||||
formatProperty(obj, property) {
|
||||
if (typeof property === 'symbol') {
|
||||
this.append(property.toString());
|
||||
} else {
|
||||
@@ -7812,9 +7801,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
|
||||
this.append(': ');
|
||||
this.format(obj[property]);
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.append = function(value) {
|
||||
append(value) {
|
||||
// This check protects us from the rare case where an object has overriden
|
||||
// `toString()` with an invalid implementation (returning a non-string).
|
||||
if (typeof value !== 'string') {
|
||||
@@ -7828,7 +7817,23 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
if (result.truncated) {
|
||||
throw new MaxCharsReachedError();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function hasCustomToString(value) {
|
||||
// value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g.
|
||||
// iframe, web worker)
|
||||
try {
|
||||
return (
|
||||
j$.isFunction_(value.toString) &&
|
||||
value.toString !== Object.prototype.toString &&
|
||||
value.toString() !== Object.prototype.toString.call(value)
|
||||
);
|
||||
} catch (e) {
|
||||
// The custom toString() threw.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function truncate(s, maxlen) {
|
||||
if (s.length <= maxlen) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
describe('DiffBuilder', function() {
|
||||
it('records the actual and expected objects', function() {
|
||||
const diffBuilder = jasmineUnderTest.DiffBuilder();
|
||||
const diffBuilder = new jasmineUnderTest.DiffBuilder();
|
||||
diffBuilder.setRoots({ x: 'actual' }, { x: 'expected' });
|
||||
diffBuilder.recordMismatch();
|
||||
|
||||
@@ -10,7 +10,7 @@ describe('DiffBuilder', function() {
|
||||
});
|
||||
|
||||
it('prints the path at which the difference was found', function() {
|
||||
const diffBuilder = jasmineUnderTest.DiffBuilder();
|
||||
const diffBuilder = new jasmineUnderTest.DiffBuilder();
|
||||
diffBuilder.setRoots({ foo: { x: 'actual' } }, { foo: { x: 'expected' } });
|
||||
|
||||
diffBuilder.withPath('foo', function() {
|
||||
@@ -23,7 +23,7 @@ describe('DiffBuilder', function() {
|
||||
});
|
||||
|
||||
it('prints multiple messages, separated by newlines', function() {
|
||||
const diffBuilder = jasmineUnderTest.DiffBuilder();
|
||||
const diffBuilder = new jasmineUnderTest.DiffBuilder();
|
||||
diffBuilder.setRoots({ foo: 1, bar: 3 }, { foo: 2, bar: 4 });
|
||||
|
||||
diffBuilder.withPath('foo', function() {
|
||||
@@ -40,7 +40,7 @@ describe('DiffBuilder', function() {
|
||||
});
|
||||
|
||||
it('allows customization of the message', function() {
|
||||
const diffBuilder = jasmineUnderTest.DiffBuilder();
|
||||
const diffBuilder = new jasmineUnderTest.DiffBuilder();
|
||||
diffBuilder.setRoots({ x: 'bar' }, { x: 'foo' });
|
||||
|
||||
function darthVaderFormatter(actual, expected, path) {
|
||||
@@ -68,7 +68,7 @@ describe('DiffBuilder', function() {
|
||||
const prettyPrinter = function(val) {
|
||||
return '|' + val + '|';
|
||||
},
|
||||
diffBuilder = jasmineUnderTest.DiffBuilder({
|
||||
diffBuilder = new jasmineUnderTest.DiffBuilder({
|
||||
prettyPrinter: prettyPrinter
|
||||
});
|
||||
prettyPrinter.customFormat_ = function() {};
|
||||
@@ -86,7 +86,7 @@ describe('DiffBuilder', function() {
|
||||
it('passes the injected pretty-printer to the diff formatter', function() {
|
||||
const diffFormatter = jasmine.createSpy('diffFormatter'),
|
||||
prettyPrinter = function() {},
|
||||
diffBuilder = jasmineUnderTest.DiffBuilder({
|
||||
diffBuilder = new jasmineUnderTest.DiffBuilder({
|
||||
prettyPrinter: prettyPrinter
|
||||
});
|
||||
prettyPrinter.customFormat_ = function() {};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
function SinglePrettyPrintRun(customObjectFormatters, pp) {
|
||||
class SinglePrettyPrintRun {
|
||||
constructor(customObjectFormatters, pp) {
|
||||
this.customObjectFormatters_ = customObjectFormatters;
|
||||
this.ppNestLevel_ = 0;
|
||||
this.seen = [];
|
||||
@@ -8,22 +9,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
this.pp_ = pp;
|
||||
}
|
||||
|
||||
function hasCustomToString(value) {
|
||||
// value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g.
|
||||
// iframe, web worker)
|
||||
try {
|
||||
return (
|
||||
j$.isFunction_(value.toString) &&
|
||||
value.toString !== Object.prototype.toString &&
|
||||
value.toString() !== Object.prototype.toString.call(value)
|
||||
);
|
||||
} catch (e) {
|
||||
// The custom toString() threw.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.format = function(value) {
|
||||
format(value) {
|
||||
this.ppNestLevel_++;
|
||||
try {
|
||||
const customFormatResult = this.applyCustomFormatters_(value);
|
||||
@@ -99,13 +85,13 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
} finally {
|
||||
this.ppNestLevel_--;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.applyCustomFormatters_ = function(value) {
|
||||
applyCustomFormatters_(value) {
|
||||
return customFormat(value, this.customObjectFormatters_);
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.iterateObject = function(obj, fn) {
|
||||
iterateObject(obj, fn) {
|
||||
const objKeys = j$.MatchersUtil.keys(obj, j$.isArray_(obj));
|
||||
const length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
|
||||
|
||||
@@ -114,17 +100,17 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
return objKeys.length > length;
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitScalar = function(value) {
|
||||
emitScalar(value) {
|
||||
this.append(value);
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitString = function(value) {
|
||||
emitString(value) {
|
||||
this.append("'" + value + "'");
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitArray = function(array) {
|
||||
emitArray(array) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Array');
|
||||
return;
|
||||
@@ -159,9 +145,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
this.append(' ]');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitSet = function(set) {
|
||||
emitSet(set) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Set');
|
||||
return;
|
||||
@@ -184,9 +170,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
this.append(', ...');
|
||||
}
|
||||
this.append(' )');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitMap = function(map) {
|
||||
emitMap(map) {
|
||||
if (this.ppNestLevel_ > j$.MAX_PRETTY_PRINT_DEPTH) {
|
||||
this.append('Map');
|
||||
return;
|
||||
@@ -209,9 +195,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
this.append(', ...');
|
||||
}
|
||||
this.append(' )');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitObject = function(obj) {
|
||||
emitObject(obj) {
|
||||
const ctor = obj.constructor;
|
||||
const constructorName =
|
||||
typeof ctor === 'function' && obj instanceof ctor
|
||||
@@ -242,9 +228,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
this.append(' })');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitTypedArray = function(arr) {
|
||||
emitTypedArray(arr) {
|
||||
const constructorName = j$.fnNameFor(arr.constructor);
|
||||
const limitedArray = Array.prototype.slice.call(
|
||||
arr,
|
||||
@@ -258,9 +244,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
this.append(constructorName + ' [ ' + itemsString + ' ]');
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.emitDomElement = function(el) {
|
||||
emitDomElement(el) {
|
||||
const tagName = el.tagName.toLowerCase();
|
||||
let out = '<' + tagName;
|
||||
|
||||
@@ -279,9 +265,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
}
|
||||
|
||||
this.append(out);
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.formatProperty = function(obj, property) {
|
||||
formatProperty(obj, property) {
|
||||
if (typeof property === 'symbol') {
|
||||
this.append(property.toString());
|
||||
} else {
|
||||
@@ -290,9 +276,9 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
|
||||
this.append(': ');
|
||||
this.format(obj[property]);
|
||||
};
|
||||
}
|
||||
|
||||
SinglePrettyPrintRun.prototype.append = function(value) {
|
||||
append(value) {
|
||||
// This check protects us from the rare case where an object has overriden
|
||||
// `toString()` with an invalid implementation (returning a non-string).
|
||||
if (typeof value !== 'string') {
|
||||
@@ -306,7 +292,23 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
||||
if (result.truncated) {
|
||||
throw new MaxCharsReachedError();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function hasCustomToString(value) {
|
||||
// value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g.
|
||||
// iframe, web worker)
|
||||
try {
|
||||
return (
|
||||
j$.isFunction_(value.toString) &&
|
||||
value.toString !== Object.prototype.toString &&
|
||||
value.toString() !== Object.prototype.toString.call(value)
|
||||
);
|
||||
} catch (e) {
|
||||
// The custom toString() threw.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function truncate(s, maxlen) {
|
||||
if (s.length <= maxlen) {
|
||||
|
||||
@@ -1,79 +1,100 @@
|
||||
getJasmineRequireObj().DiffBuilder = function(j$) {
|
||||
return function DiffBuilder(config) {
|
||||
const prettyPrinter =
|
||||
class DiffBuilder {
|
||||
constructor(config) {
|
||||
this.prettyPrinter_ =
|
||||
(config || {}).prettyPrinter || j$.makePrettyPrinter();
|
||||
const mismatches = new j$.MismatchTree();
|
||||
let path = new j$.ObjectPath();
|
||||
let actualRoot = undefined;
|
||||
let expectedRoot = undefined;
|
||||
this.mismatches_ = new j$.MismatchTree();
|
||||
this.path_ = new j$.ObjectPath();
|
||||
this.actualRoot_ = undefined;
|
||||
this.expectedRoot_ = undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
setRoots: function(actual, expected) {
|
||||
actualRoot = actual;
|
||||
expectedRoot = expected;
|
||||
},
|
||||
setRoots(actual, expected) {
|
||||
this.actualRoot_ = actual;
|
||||
this.expectedRoot_ = expected;
|
||||
}
|
||||
|
||||
recordMismatch: function(formatter) {
|
||||
mismatches.add(path, formatter);
|
||||
},
|
||||
recordMismatch(formatter) {
|
||||
this.mismatches_.add(this.path_, formatter);
|
||||
}
|
||||
|
||||
getMessage: function() {
|
||||
getMessage() {
|
||||
const messages = [];
|
||||
|
||||
mismatches.traverse(function(path, isLeaf, formatter) {
|
||||
const { actual, expected } = dereferencePath(
|
||||
path,
|
||||
actualRoot,
|
||||
expectedRoot,
|
||||
prettyPrinter
|
||||
);
|
||||
this.mismatches_.traverse((path, isLeaf, formatter) => {
|
||||
const { actual, expected } = this.dereferencePath_(path);
|
||||
|
||||
if (formatter) {
|
||||
messages.push(formatter(actual, expected, path, prettyPrinter));
|
||||
messages.push(formatter(actual, expected, path, this.prettyPrinter_));
|
||||
return true;
|
||||
}
|
||||
|
||||
const actualCustom = prettyPrinter.customFormat_(actual);
|
||||
const expectedCustom = prettyPrinter.customFormat_(expected);
|
||||
const actualCustom = this.prettyPrinter_.customFormat_(actual);
|
||||
const expectedCustom = this.prettyPrinter_.customFormat_(expected);
|
||||
const useCustom = !(
|
||||
j$.util.isUndefined(actualCustom) &&
|
||||
j$.util.isUndefined(expectedCustom)
|
||||
);
|
||||
|
||||
if (useCustom) {
|
||||
messages.push(
|
||||
wrapPrettyPrinted(actualCustom, expectedCustom, path)
|
||||
);
|
||||
messages.push(wrapPrettyPrinted(actualCustom, expectedCustom, path));
|
||||
return false; // don't recurse further
|
||||
}
|
||||
|
||||
if (isLeaf) {
|
||||
messages.push(
|
||||
defaultFormatter(actual, expected, path, prettyPrinter)
|
||||
);
|
||||
messages.push(this.defaultFormatter_(actual, expected, path));
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return messages.join('\n');
|
||||
},
|
||||
}
|
||||
|
||||
withPath: function(pathComponent, block) {
|
||||
const oldPath = path;
|
||||
path = path.add(pathComponent);
|
||||
withPath(pathComponent, block) {
|
||||
const oldPath = this.path_;
|
||||
this.path_ = this.path_.add(pathComponent);
|
||||
block();
|
||||
path = oldPath;
|
||||
this.path_ = oldPath;
|
||||
}
|
||||
|
||||
dereferencePath_(objectPath) {
|
||||
let actual = this.actualRoot_;
|
||||
let expected = this.expectedRoot_;
|
||||
|
||||
const handleAsymmetricExpected = () => {
|
||||
if (
|
||||
j$.isAsymmetricEqualityTester_(expected) &&
|
||||
j$.isFunction_(expected.valuesForDiff_)
|
||||
) {
|
||||
const asymmetricResult = expected.valuesForDiff_(
|
||||
actual,
|
||||
this.prettyPrinter_
|
||||
);
|
||||
expected = asymmetricResult.self;
|
||||
actual = asymmetricResult.other;
|
||||
}
|
||||
};
|
||||
|
||||
function defaultFormatter(actual, expected, path, prettyPrinter) {
|
||||
handleAsymmetricExpected();
|
||||
|
||||
for (const pc of objectPath.components) {
|
||||
actual = actual[pc];
|
||||
expected = expected[pc];
|
||||
handleAsymmetricExpected();
|
||||
}
|
||||
|
||||
return { actual: actual, expected: expected };
|
||||
}
|
||||
|
||||
defaultFormatter_(actual, expected, path) {
|
||||
return wrapPrettyPrinted(
|
||||
prettyPrinter(actual),
|
||||
prettyPrinter(expected),
|
||||
this.prettyPrinter_(actual),
|
||||
this.prettyPrinter_(expected),
|
||||
path
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function wrapPrettyPrinted(actual, expected, path) {
|
||||
return (
|
||||
@@ -86,28 +107,6 @@ getJasmineRequireObj().DiffBuilder = function(j$) {
|
||||
'.'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
function dereferencePath(objectPath, actual, expected, pp) {
|
||||
function handleAsymmetricExpected() {
|
||||
if (
|
||||
j$.isAsymmetricEqualityTester_(expected) &&
|
||||
j$.isFunction_(expected.valuesForDiff_)
|
||||
) {
|
||||
const asymmetricResult = expected.valuesForDiff_(actual, pp);
|
||||
expected = asymmetricResult.self;
|
||||
actual = asymmetricResult.other;
|
||||
}
|
||||
}
|
||||
|
||||
handleAsymmetricExpected();
|
||||
|
||||
for (const pc of objectPath.components) {
|
||||
actual = actual[pc];
|
||||
expected = expected[pc];
|
||||
handleAsymmetricExpected();
|
||||
}
|
||||
|
||||
return { actual: actual, expected: expected };
|
||||
}
|
||||
return DiffBuilder;
|
||||
};
|
||||
|
||||
@@ -6,14 +6,15 @@ getJasmineRequireObj().MismatchTree = function(j$) {
|
||||
the expected and actual object graphs. MismatchTree maintains that context
|
||||
and provides it via the traverse method.
|
||||
*/
|
||||
function MismatchTree(path) {
|
||||
class MismatchTree {
|
||||
constructor(path) {
|
||||
this.path = path || new j$.ObjectPath([]);
|
||||
this.formatter = undefined;
|
||||
this.children = [];
|
||||
this.isMismatch = false;
|
||||
}
|
||||
|
||||
MismatchTree.prototype.add = function(path, formatter) {
|
||||
add(path, formatter) {
|
||||
if (path.depth() === 0) {
|
||||
this.formatter = formatter;
|
||||
this.isMismatch = true;
|
||||
@@ -29,9 +30,9 @@ getJasmineRequireObj().MismatchTree = function(j$) {
|
||||
|
||||
child.add(path, formatter);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
MismatchTree.prototype.traverse = function(visit) {
|
||||
traverse(visit) {
|
||||
const hasChildren = this.children.length > 0;
|
||||
|
||||
if (this.isMismatch || hasChildren) {
|
||||
@@ -41,14 +42,15 @@ getJasmineRequireObj().MismatchTree = function(j$) {
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
MismatchTree.prototype.child = function(key) {
|
||||
child(key) {
|
||||
return this.children.find(child => {
|
||||
const pathEls = child.path.components;
|
||||
return pathEls[pathEls.length - 1] === key;
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return MismatchTree;
|
||||
};
|
||||
|
||||
@@ -1,27 +1,29 @@
|
||||
getJasmineRequireObj().ObjectPath = function(j$) {
|
||||
function ObjectPath(components) {
|
||||
class ObjectPath {
|
||||
constructor(components) {
|
||||
this.components = components || [];
|
||||
}
|
||||
|
||||
ObjectPath.prototype.toString = function() {
|
||||
toString() {
|
||||
if (this.components.length) {
|
||||
return '$' + this.components.map(formatPropertyAccess).join('');
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
ObjectPath.prototype.add = function(component) {
|
||||
add(component) {
|
||||
return new ObjectPath(this.components.concat([component]));
|
||||
};
|
||||
}
|
||||
|
||||
ObjectPath.prototype.shift = function() {
|
||||
shift() {
|
||||
return new ObjectPath(this.components.slice(1));
|
||||
};
|
||||
}
|
||||
|
||||
ObjectPath.prototype.depth = function() {
|
||||
depth() {
|
||||
return this.components.length;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function formatPropertyAccess(prop) {
|
||||
if (typeof prop === 'number' || typeof prop === 'symbol') {
|
||||
@@ -32,7 +34,7 @@ getJasmineRequireObj().ObjectPath = function(j$) {
|
||||
return '.' + prop;
|
||||
}
|
||||
|
||||
return "['" + prop + "']";
|
||||
return `['${prop}']`;
|
||||
}
|
||||
|
||||
function isValidIdentifier(string) {
|
||||
|
||||
@@ -14,7 +14,7 @@ getJasmineRequireObj().toEqual = function(j$) {
|
||||
var result = {
|
||||
pass: false
|
||||
},
|
||||
diffBuilder = j$.DiffBuilder({ prettyPrinter: matchersUtil.pp });
|
||||
diffBuilder = new j$.DiffBuilder({ prettyPrinter: matchersUtil.pp });
|
||||
|
||||
result.pass = matchersUtil.equals(actual, expected, diffBuilder);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user