assertthat
Version:
assertthat provides fluent TDD.
94 lines (93 loc) • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareStrings = void 0;
const compareArrays_1 = require("../forArrays/compareArrays");
const intersperse_1 = require("../../utils/intersperse");
const errors_1 = require("../../errors");
const maximumStringLengthForPreciveDiff_1 = require("../../constants/maximumStringLengthForPreciveDiff");
const costs_1 = require("../../constants/costs");
const EqualDiff_1 = require("../../diffs/EqualDiff");
const ArrayDiffSegment_1 = require("../../diffs/forArrays/ArrayDiffSegment");
const StringDiff_1 = require("../../diffs/forStrings/StringDiff");
const convertArrayDiffToStringDiff = function (arrayDiff) {
const convertedStringDiff = (0, StringDiff_1.stringDiff)({
segments: [],
cost: 0
});
for (const arrayDiffSegment of arrayDiff.segments) {
if ((0, ArrayDiffSegment_1.isEqualDiffSegment)(arrayDiffSegment)) {
convertedStringDiff.segments.push({
equal: arrayDiffSegment.equal.join(''),
cost: arrayDiffSegment.cost
});
}
if ((0, ArrayDiffSegment_1.isChangeDiffSegment)(arrayDiffSegment)) {
const replaceSegment = {
replace: '',
replaceWith: '',
cost: 0
};
for (const change of arrayDiffSegment.change) {
if (!(0, StringDiff_1.isStringDiff)(change)) {
throw new errors_1.InvalidOperation('Tried to convert ArrayDiff with sub-diff to StringDiff.');
}
replaceSegment.replace += change.segments[0].addition;
replaceSegment.replaceWith += change.segments[1].omission;
replaceSegment.cost += change.cost;
}
convertedStringDiff.segments.push(replaceSegment);
}
if ((0, ArrayDiffSegment_1.isOmissionDiffSegment)(arrayDiffSegment)) {
convertedStringDiff.segments.push({
omission: arrayDiffSegment.omission.join(''),
cost: arrayDiffSegment.cost
});
}
if ((0, ArrayDiffSegment_1.isAdditionDiffSegment)(arrayDiffSegment)) {
convertedStringDiff.segments.push({
addition: arrayDiffSegment.addition.join(''),
cost: arrayDiffSegment.cost
});
}
convertedStringDiff.cost += arrayDiffSegment.cost;
}
return convertedStringDiff;
};
const compareStrings = function (actual, expected) {
if (actual === expected) {
return (0, EqualDiff_1.equalDiff)({
value: actual
});
}
let actualExploded = [...actual];
if (actualExploded.length > maximumStringLengthForPreciveDiff_1.maximumStringLengthForPreciseDiff) {
actualExploded = (0, intersperse_1.intersperse)(actual.split(' '), ' ');
}
if (actualExploded.length > maximumStringLengthForPreciveDiff_1.maximumStringLengthForPreciseDiff) {
actualExploded = (0, intersperse_1.intersperse)(actual.split('\n'), '\n');
}
let expectedExploded = [...expected];
if (expectedExploded.length > maximumStringLengthForPreciveDiff_1.maximumStringLengthForPreciseDiff) {
expectedExploded = (0, intersperse_1.intersperse)(expected.split(' '), ' ');
}
if (expectedExploded.length > maximumStringLengthForPreciveDiff_1.maximumStringLengthForPreciseDiff) {
expectedExploded = (0, intersperse_1.intersperse)(expected.split('\n'), '\n');
}
if (actualExploded.length <= 1 && expectedExploded.length <= 1) {
return (0, StringDiff_1.stringDiff)({
segments: [
{ addition: actual, cost: costs_1.unequalCharCost / 2 },
{ omission: expected, cost: costs_1.unequalCharCost / 2 }
],
cost: costs_1.unequalCharCost
});
}
const result = (0, compareArrays_1.compareArrays)(actualExploded, expectedExploded);
if ((0, EqualDiff_1.isEqualDiff)(result)) {
return (0, EqualDiff_1.equalDiff)({
value: actual
});
}
return convertArrayDiffToStringDiff(result);
};
exports.compareStrings = compareStrings;