Java Code Examples for com.github.difflib.UnifiedDiffUtils#generateUnifiedDiff()

The following examples show how to use com.github.difflib.UnifiedDiffUtils#generateUnifiedDiff() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Computes a unified diff of the input strings, returning the empty string if the {@code
 * expected} and {@code actual} are equal.
 */
@Nonnull
@VisibleForTesting
static String getPatch(
    String expected, String actual, String expectedFileName, String actualFileName)
    throws DiffException {
  List<String> referenceLines = Arrays.asList(expected.split("\n"));
  List<String> testLines = Arrays.asList(actual.split("\n"));
  Patch<String> patch = DiffUtils.diff(referenceLines, testLines);
  if (patch.getDeltas().isEmpty()) {
    return "";
  } else {
    List<String> patchLines =
        UnifiedDiffUtils.generateUnifiedDiff(
            expectedFileName, actualFileName, referenceLines, patch, 3);
    return StringUtils.join(patchLines, "\n");
  }
}
 
Example 2
Source File: TextDiffSubject.java    From nomulus with Apache License 2.0 5 votes vote down vote up
static String generateUnifiedDiff(
    ImmutableList<String> expectedContent, ImmutableList<String> actualContent) {
  Patch<String> diff;
  try {
    diff = DiffUtils.diff(expectedContent, actualContent);
  } catch (DiffException e) {
    throw new RuntimeException(e);
  }
  List<String> unifiedDiff =
      UnifiedDiffUtils.generateUnifiedDiff("expected", "actual", expectedContent, diff, 0);

  return Joiner.on('\n').join(unifiedDiff);
}