From 96924fdbb92e75ff09fbb6aec6b96608c21edff4 Mon Sep 17 00:00:00 2001 From: Jon Schneider Date: Thu, 18 Aug 2016 16:56:42 -0700 Subject: [PATCH] Provide alternate diffing mechanism --- .../kotlin/com/netflix/java/refactor/JavaSource.kt | 4 ++++ .../com/netflix/java/refactor/JavaSourceDiff.kt | 10 ++++++++++ .../com/netflix/java/refactor/JavaSourceTest.kt | 13 +++++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/com/netflix/java/refactor/JavaSourceDiff.kt diff --git a/src/main/kotlin/com/netflix/java/refactor/JavaSource.kt b/src/main/kotlin/com/netflix/java/refactor/JavaSource.kt index 7ef5ce2..398f502 100644 --- a/src/main/kotlin/com/netflix/java/refactor/JavaSource.kt +++ b/src/main/kotlin/com/netflix/java/refactor/JavaSource.kt @@ -4,6 +4,8 @@ import com.netflix.java.refactor.find.* import com.sun.tools.javac.code.Symbol import com.sun.tools.javac.tree.JCTree import java.nio.file.Files +import java.util.function.Consumer +import kotlin.concurrent.thread class JavaSource(internal val cu: CompilationUnit) { var changedFile = false @@ -51,4 +53,6 @@ class JavaSource(internal val cu: CompilationUnit) { val after = text() return InMemoryDiffEntry(file().toString(), before, after).diff } + + fun beginDiff() = JavaSourceDiff(this) } \ No newline at end of file diff --git a/src/main/kotlin/com/netflix/java/refactor/JavaSourceDiff.kt b/src/main/kotlin/com/netflix/java/refactor/JavaSourceDiff.kt new file mode 100644 index 0000000..b732915 --- /dev/null +++ b/src/main/kotlin/com/netflix/java/refactor/JavaSourceDiff.kt @@ -0,0 +1,10 @@ +package com.netflix.java.refactor + +class JavaSourceDiff(private val source: JavaSource) { + val before = source.text() + + fun gitStylePatch(): String { + val after = source.text() + return InMemoryDiffEntry(source.file().toString(), before, after).diff + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/netflix/java/refactor/JavaSourceTest.kt b/src/test/kotlin/com/netflix/java/refactor/JavaSourceTest.kt index 217a8bd..76f7f83 100644 --- a/src/test/kotlin/com/netflix/java/refactor/JavaSourceTest.kt +++ b/src/test/kotlin/com/netflix/java/refactor/JavaSourceTest.kt @@ -87,11 +87,13 @@ class JavaSourceTest : AbstractRefactorTest() { """) val source = parseJava(a, b, c) - val diff = source.diff { + val diff1 = source.beginDiff() + + val diff2 = source.diff { refactor().changeType("B", "C").fix() } - - assertEquals(""" + + val expectedDiff = """ |diff --git a/${a.absolutePath} b/${a.absolutePath} |index 70f03ee..b82f543 100644 |--- a/${a.absolutePath} @@ -105,6 +107,9 @@ class JavaSourceTest : AbstractRefactorTest() { | } | } | - """.trimMargin(), diff) + """.trimMargin() + + assertEquals(expectedDiff, diff1.gitStylePatch()) + assertEquals(expectedDiff, diff2) } } \ No newline at end of file