|
8 | 8 | import java.util.regex.Matcher; |
9 | 9 | import java.util.regex.Pattern; |
10 | 10 |
|
11 | | -public class InlineJavaReporter implements ApprovalFailureReporter |
12 | | -{ |
13 | | - private final String sourceFilePath; |
14 | | - private final StackTraceNamer stackTraceNamer; |
15 | | - private final ApprovalFailureReporter reporter; |
16 | | - public InlineJavaReporter(ApprovalFailureReporter reporter) |
17 | | - { |
18 | | - this.reporter = reporter; |
19 | | - this.stackTraceNamer = new StackTraceNamer(); |
20 | | - this.sourceFilePath = stackTraceNamer.getSourceFilePath(); |
21 | | - } |
22 | | - public String getSourceFilePath() |
23 | | - { |
24 | | - return sourceFilePath; |
25 | | - } |
26 | | - @Override |
27 | | - public boolean report(String received, String approved) |
28 | | - { |
29 | | - String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; |
30 | | - String newSource = createReceived(FileUtils.readFile(received)); |
31 | | - return reporter.report(newSource, sourceFile); |
32 | | - } |
33 | | - public String createReceived(String actual) |
34 | | - { |
35 | | - String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; |
36 | | - String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt"; |
37 | | - String text = FileUtils.readFile(file); |
38 | | - String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName()); |
39 | | - FileUtils.writeFile(new File(received), fullText); |
40 | | - return received; |
41 | | - } |
42 | | - public static String createNewReceivedFileText(String text, String actual, String methodName) |
43 | | - { |
44 | | - text = text.replaceAll("\r\n", "\n"); |
45 | | - CodeParts codeParts = CodeParts.splitCode(text, methodName); |
46 | | - int start = text.indexOf("void " + methodName + "("); |
47 | | - int startOfLine = text.substring(0, start).lastIndexOf("\n") + 1; |
48 | | - String line = text.substring(startOfLine, start); |
49 | | - String tab = extractLeadingWhitespace(line); |
50 | | - start = text.indexOf("{", start); |
51 | | - int codeBeforeExpected = text.indexOf("\n", start); |
52 | | - int end = text.indexOf("}", codeBeforeExpected); |
53 | | - // if there is an expected = then set the code before to the beginning of this line |
54 | | - int endString = text.indexOf("\"\"\";", codeBeforeExpected); |
55 | | - String part1 = text.substring(0, codeBeforeExpected); |
56 | | - String part2 = null; |
57 | | - if (0 < endString && endString < end) |
58 | | - { |
59 | | - // find next newline |
60 | | - endString = text.indexOf("\n", endString); |
61 | | - part2 = text.substring(endString + 1); |
| 11 | +public class InlineJavaReporter implements ApprovalFailureReporter { |
| 12 | + private final String sourceFilePath; |
| 13 | + private final StackTraceNamer stackTraceNamer; |
| 14 | + private final ApprovalFailureReporter reporter; |
| 15 | + |
| 16 | + public InlineJavaReporter(ApprovalFailureReporter reporter) { |
| 17 | + this.reporter = reporter; |
| 18 | + this.stackTraceNamer = new StackTraceNamer(); |
| 19 | + this.sourceFilePath = stackTraceNamer.getSourceFilePath(); |
| 20 | + } |
| 21 | + |
| 22 | + public String getSourceFilePath() { |
| 23 | + return sourceFilePath; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public boolean report(String received, String approved) { |
| 28 | + String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; |
| 29 | + String newSource = createReceived(FileUtils.readFile(received)); |
| 30 | + return reporter.report(newSource, sourceFile); |
| 31 | + } |
| 32 | + |
| 33 | + public String createReceived(String actual) { |
| 34 | + String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; |
| 35 | + String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt"; |
| 36 | + String text = FileUtils.readFile(file); |
| 37 | + String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName()); |
| 38 | + FileUtils.writeFile(new File(received), fullText); |
| 39 | + return received; |
| 40 | + } |
| 41 | + |
| 42 | + public static String createNewReceivedFileText(String text, String actual, String methodName) { |
| 43 | + text = text.replaceAll("\r\n", "\n"); |
| 44 | + CodeParts codeParts = CodeParts.splitCode(text, methodName); |
| 45 | + if (codeParts.method.contains("expected = \"\"\"")) { |
| 46 | + replaceExpected(codeParts, actual); |
| 47 | + } else { |
| 48 | + addExpected(codeParts, actual); |
| 49 | + } |
| 50 | + return codeParts.getFullCode(); |
| 51 | + } |
| 52 | + |
| 53 | + private static void addExpected(CodeParts codeParts, String actual) { |
| 54 | + int start = codeParts.method.indexOf("{") + 2; |
| 55 | + String before = codeParts.method.substring(0, start); |
| 56 | + String after = codeParts.method.substring(start); |
| 57 | + codeParts.method = before + getExpected(actual, codeParts.tab) + after; |
| 58 | + } |
| 59 | + |
| 60 | + private static String getExpected(String actual, String tab) { |
| 61 | + return String.format("%s%svar expected = \"\"\"\n%s%s%s%s\"\"\";\n", |
| 62 | + tab, tab, indent(actual, tab), tab, tab, tab); |
| 63 | + } |
| 64 | + |
| 65 | + private static void replaceExpected(CodeParts codeParts, String actual) { |
| 66 | + int start = codeParts.method.indexOf("expected = \"\"\""); |
| 67 | + start = codeParts.method.substring(0, start).lastIndexOf("\n") + 1; |
| 68 | + int end = codeParts.method.indexOf("\"\"\";"); |
| 69 | + end = codeParts.method.indexOf("\n", end) + 1; |
| 70 | + String before = codeParts.method.substring(0, start); |
| 71 | + String after = codeParts.method.substring(end); |
| 72 | + codeParts.method = before + getExpected(actual, codeParts.tab) + after; |
62 | 73 | } |
63 | | - else |
64 | | - { |
65 | | - part2 = text.substring(codeBeforeExpected + 1); |
| 74 | + |
| 75 | + public static String indent(String actual, String tab) { |
| 76 | + String[] split = actual.split("\n"); |
| 77 | + String output = ""; |
| 78 | + for (String line : split) { |
| 79 | + output += tab + tab + tab + line + "\n"; |
| 80 | + } |
| 81 | + return output; |
66 | 82 | } |
67 | | - return String.format("%s\n%s%svar expected = \"\"\"\n%s%s%s%s\"\"\";\n%s", part1, tab, tab, |
68 | | - indent(actual, tab), tab, tab, tab, part2); |
69 | | - } |
70 | 83 |
|
71 | | - public static String indent(String actual, String tab) |
72 | | - { |
73 | | - String[] split = actual.split("\n"); |
74 | | - String output = ""; |
75 | | - for (String line : split) |
76 | | - { |
77 | | - output += tab + tab + tab + line + "\n"; |
| 84 | + private static String extractLeadingWhitespace(String text) { |
| 85 | + Pattern pattern = Pattern.compile("^\\s+"); |
| 86 | + Matcher matcher = pattern.matcher(text); |
| 87 | + if (matcher.find()) { |
| 88 | + return matcher.group(); |
| 89 | + } |
| 90 | + return "\t"; |
78 | 91 | } |
79 | | - return output; |
80 | | - } |
81 | | - private static String extractLeadingWhitespace(String text) |
82 | | - { |
83 | | - Pattern pattern = Pattern.compile("^\\s+"); |
84 | | - Matcher matcher = pattern.matcher(text); |
85 | | - if (matcher.find()) |
86 | | - { return matcher.group(); } |
87 | | - return "\t"; |
88 | | - } |
89 | 92 | } |
0 commit comments