本文整理汇总了TypeScript中diff.createPatch函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createPatch函数的具体用法?TypeScript createPatch怎么用?TypeScript createPatch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createPatch函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createUnifiedDiff
/**
* Returns a string highlighting the differences between the actual
* and expected strings.
*
* @protected
* @param {*} actual
* @param {*} expected
* @returns {string}
* @memberof LiveDocReporter
*/
protected createUnifiedDiff(actual, expected): string {
var indent = '';
const _this = this;
function cleanUp(line) {
if (line[0] === '+') {
return indent + _this.colorTheme.statusPass(line);
}
if (line[0] === '-') {
return indent + _this.colorTheme.statusFail(line);
}
if (line.match(/@@/)) {
return '--';
}
if (line.match(/\\ No newline/)) {
return null;
}
return indent + line;
}
function notBlank(line) {
return typeof line !== 'undefined' && line !== null;
}
var msg = diff.createPatch('string', actual.toString(), expected.toString());
var lines = msg.split('\n').splice(5);
return (
'\n' +
_this.colorTheme.statusPass('+ expected') +
' ' +
_this.colorTheme.statusFail('- actual') +
'\n\n' +
lines
.map(cleanUp)
.filter(notBlank)
.join('\n')
);
}
示例2: compareAgainstGolden
/**
* compareAgainstGoldens compares a test output against the content in a golden
* path, updating the content of the golden when UPDATE_GOLDENS is true.
*
* @param output The expected output, where the empty string indicates
* the file is expected to exist and be empty, while null indicates
* the file is expected to not exist. (This subtlety is used for
* externs files, where the majority of tests are not expected to
* produce one.)
*/
function compareAgainstGolden(output: string|null, path: string) {
const golden = readGolden(path);
let patchedGolden = patchGolden(golden, path);
// Make sure we have proper line endings when testing on Windows.
if (patchedGolden != null) patchedGolden = patchedGolden.replace(/\r\n/g, '\n');
if (output != null) output = output.replace(/\r\n/g, '\n');
const patchPath = calcPatchPath(path);
if (UPDATE_GOLDENS) {
if (golden !== null && golden !== output) {
console.log(`Updating golden patch file for ${path} with ${patchPath}`);
const patchOutput =
diff.createPatch(path, golden || '', output || '', 'golden', 'tsickle with transformer')!;
fs.writeFileSync(patchPath, patchOutput, 'utf-8');
} else {
// The desired golden state is for there to be no output file.
// Ensure no file exists.
try {
fs.unlinkSync(patchPath);
} catch (e) {
// ignore.
}
}
} else {
expect(output).to.equal(patchedGolden, `${path} with ${patchPath}`);
}
}
示例3: switch
diffResult.diffSet.forEach(function(entry) {
switch (entry.state) {
case 'equal':
return;
case 'left':
const expectedFileRelPath =
path.join(entry.relativePath || '/', entry.name1);
errorOutputLines.push((chalk.bold.green(' + ' + expectedFileRelPath)));
return;
case 'right':
const actualFileRelPath =
path.join(entry.relativePath || '/', entry.name2);
errorOutputLines.push((chalk.bold.red(' - ' + actualFileRelPath)));
return;
case 'distinct':
const diffedFileRelPath =
path.join(entry.relativePath || '/', entry.name1);
const expectedFilePath = path.join(entry.path1, entry.name1);
const actualFilePath = path.join(entry.path2, entry.name2);
const patch = diff.createPatch(
'string',
fs.readFileSync(expectedFilePath, 'utf8'),
fs.readFileSync(actualFilePath, 'utf8'),
'expected',
'converted');
errorOutputLines.push((chalk.bold.red('<> ' + diffedFileRelPath)));
errorOutputLines.push(formatDiffPatch(patch));
return;
default:
throw new Error('Unexpected diff-entry format: ' + entry);
}
});
示例4:
const errorMsgFromJson = (actual: string, expected: string): string => `
----------[expected]----------
${expected}
----------[actual]------------
${actual}
----------[diff]--------------
${JsDiff.createPatch("Diff", expected, actual, "expected", "actual")}
------------------------------
`;
示例5: verifyAgainstGoldenFile
export function verifyAgainstGoldenFile(
entrypoint: string, goldenFile: string, options: SerializationOptions = {}): string {
const actual = publicApi(entrypoint, options);
const expected = fs.readFileSync(goldenFile).toString();
if (actual === expected) {
return '';
} else {
const patch = createPatch(goldenFile, expected, actual, 'Golden file', 'Generated API');
// Remove the header of the patch
const start = patch.indexOf('\n', patch.indexOf('\n') + 1) + 1;
return patch.substring(start);
}
}
示例6: it
it(`Package "${testPackage.dir}"`, () => {
if (actual !== expected) {
// Compute the patch and strip the header
let patch =
createPatch(goldenFile, expected, actual, 'Golden file', 'Generated file', {context: 5});
const endOfHeader = patch.indexOf('\n', patch.indexOf('\n') + 1) + 1;
patch = patch.substring(endOfHeader);
// Use string concatentation instead of whitespace inside a single template string
// to make the structure message explicit.
const failureMessage = `example ng_package differs from golden file\n` +
` Diff:\n` +
` ${patch}\n\n` +
` To accept the new golden file, run:\n` +
` bazel run ${process.env['BAZEL_TARGET']}.accept\n`;
fail(failureMessage);
}
});
示例7: getEngineByTargetDir
args.files.forEach(filePath => {
if (opts.verbose) {
console.warn(`processing ${filePath}...`);
}
const content = fs.readFileSync(filePath, { encoding: "utf8" });
const engine = getEngineByTargetDir(path.dirname(filePath));
const changeSet = engine.makeChangeSet(filePath);
if (changeSet.diffs.length !== 0) {
invalidFiles.push(filePath);
}
if (opts.stdout) {
const result = changeSet.applyChangeSets(content);
process.stdout.write(result);
} else if (opts.diff) {
const result = changeSet.applyChangeSets(content);
const patch = diff.createPatch(filePath, content, result, "before", "replaced");
console.log(patch);
} else if (opts.replace) {
const result = changeSet.applyChangeSets(content);
if (content !== result) {
fs.writeFileSync(filePath, result);
console.warn(`replaced ${filePath}`);
}
} else {
changeSet.diffs.forEach(diff => {
const before = changeSet.content.substr(diff.index, diff.tailIndex - diff.index);
const after = diff.newText;
if (after == null) {
return;
}
const lineColumn = indexToLineColumn(diff.index, changeSet.content);
console.log(`${changeSet.filePath}(${lineColumn.line + 1},${lineColumn.column + 1}): ${before} â ${after}`);
});
}
});
示例8: callback
loadFile: (index, callback) => {
index; // $ExpectType ParsedDiff
callback(undefined, one);
},
patched: (index, content) => {
index; // $ExpectType ParsedDiff
verifyApply.push(content);
},
complete: (err?: Error) => {
if (err) {
throw err;
}
verifyApply.forEach(result => {
if (result !== newStr) {
throw new Error('Result did not match newStr');
}
});
},
});
}
const uniDiffPatch = diff.structuredPatch('oldFile.ts', 'newFile.ts', one, other, 'old', 'new', {
context: 1,
});
verifyPatchMethods(one, other, uniDiffPatch);
const uniDiffStr = diff.createPatch('file.ts', one, other, 'old', 'new', { context: 1 });
const uniDiffApply = diff.parsePatch(uniDiffStr)[0];
verifyApplyMethods(one, other, uniDiffApply);