-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33dfe57
commit d5d1362
Showing
3 changed files
with
83 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
approvaltests/src/main/java/org/approvaltests/inline/InlineJavaReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.approvaltests.inline; | ||
|
||
import com.spun.util.io.FileUtils; | ||
import org.approvaltests.core.ApprovalFailureReporter; | ||
import org.approvaltests.namer.StackTraceNamer; | ||
|
||
import java.io.File; | ||
|
||
public class InlineJavaReporter implements ApprovalFailureReporter | ||
{ | ||
public final String sourceFilePath; | ||
public final StackTraceNamer stackTraceNamer; | ||
public final ApprovalFailureReporter reporter; | ||
public InlineJavaReporter(ApprovalFailureReporter reporter) | ||
{ | ||
this.reporter = reporter; | ||
this.stackTraceNamer = new StackTraceNamer(); | ||
this.sourceFilePath = stackTraceNamer.getSourceFilePath(); | ||
} | ||
public String getSourceFilePath() | ||
{ | ||
return sourceFilePath; | ||
} | ||
@Override | ||
public boolean report(String received, String approved) | ||
{ | ||
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; | ||
String newSource = createReceived(FileUtils.readFile(received)); | ||
return reporter.report(newSource, sourceFile); | ||
} | ||
public String createReceived(String actual) | ||
{ | ||
String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; | ||
String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt"; | ||
String text = FileUtils.readFile(file); | ||
String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName()); | ||
FileUtils.writeFile(new File(received), fullText); | ||
return received; | ||
} | ||
public static String createNewReceivedFileText(String text, String actual, String methodName) | ||
{ | ||
text = text.replaceAll("\r\n", "\n"); | ||
int start = text.indexOf("void " + methodName + "("); | ||
start = text.indexOf("{", start); | ||
int next = text.indexOf("\n", start); | ||
int end = text.indexOf("}", next); | ||
int endString = text.indexOf("\"\"\";", next); | ||
String part1 = text.substring(0, next); | ||
String part2 = null; | ||
if (0 < endString && endString < end) | ||
{ | ||
// find next newline | ||
endString = text.indexOf("\n", endString); | ||
part2 = text.substring(endString + 1); | ||
} | ||
else | ||
{ | ||
part2 = text.substring(next + 1); | ||
} | ||
String fullText = String.format("%s\n\t\tvar expected = \"\"\"\n%s\t\t\"\"\";\n%s", part1, indent(actual), | ||
part2); | ||
return fullText; | ||
} | ||
public static String indent(String actual) | ||
{ | ||
String[] split = actual.split("\n"); | ||
String output = ""; | ||
for (String line : split) | ||
{ | ||
output += "\t\t" + line + "\n"; | ||
} | ||
return output; | ||
} | ||
} |