|
14 | 14 |
|
15 | 15 | public class InlineComparator implements ApprovalNamer, ApprovalFailureReporter |
16 | 16 | { |
17 | | - private final String sourceFilePath; |
18 | | - private final StackTraceNamer stackTraceNamer; |
| 17 | + private final InlineJavaReporter inlineJavaReporter; |
19 | 18 | private String expected; |
20 | | - private final ApprovalFailureReporter reporter; |
21 | 19 | private File approvedFile; |
22 | 20 | private File receivedFile; |
23 | 21 | public InlineComparator(String expected, ApprovalFailureReporter reporter) |
24 | 22 | { |
25 | 23 | this.expected = expected; |
26 | | - this.reporter = reporter; |
27 | | - stackTraceNamer = new StackTraceNamer(); |
28 | | - sourceFilePath = stackTraceNamer.getSourceFilePath(); |
| 24 | + inlineJavaReporter = new InlineJavaReporter(reporter); |
29 | 25 | } |
30 | 26 | @Override |
31 | 27 | public File getApprovedFile(String extensionWithDot) |
@@ -73,61 +69,21 @@ public String getApprovalName() |
73 | 69 | @Override |
74 | 70 | public String getSourceFilePath() |
75 | 71 | { |
76 | | - return sourceFilePath; |
| 72 | + return inlineJavaReporter.getSourceFilePath(); |
77 | 73 | } |
78 | 74 | @Override |
79 | 75 | public boolean report(String received, String approved) |
80 | 76 | { |
81 | | - String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; |
82 | | - String newSource = createReceived(FileUtils.readFile(received)); |
83 | | - return reporter.report(newSource, sourceFile); |
| 77 | + return inlineJavaReporter.report(received, approved); |
84 | 78 | } |
85 | 79 | private String createReceived(String actual) |
86 | 80 | { |
87 | | - String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; |
88 | | - String received = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt"; |
89 | | - String text = FileUtils.readFile(file); |
90 | | - String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName()); |
91 | | - FileUtils.writeFile(new File(received), fullText); |
92 | | - return received; |
93 | | - } |
94 | | - public static String createNewReceivedFileText(String text, String actual, String methodName) |
95 | | - { |
96 | | - text = text.replaceAll("\r\n", "\n"); |
97 | | - int start = text.indexOf("void " + methodName + "("); |
98 | | - start = text.indexOf("{", start); |
99 | | - int next = text.indexOf("\n", start); |
100 | | - int end = text.indexOf("}", next); |
101 | | - int endString = text.indexOf("\"\"\";", next); |
102 | | - String part1 = text.substring(0, next); |
103 | | - String part2 = null; |
104 | | - if (0 < endString && endString < end) |
105 | | - { |
106 | | - // find next newline |
107 | | - endString = text.indexOf("\n", endString); |
108 | | - part2 = text.substring(endString + 1); |
109 | | - } |
110 | | - else |
111 | | - { |
112 | | - part2 = text.substring(next + 1); |
113 | | - } |
114 | | - String fullText = String.format("%s\n\t\tvar expected = \"\"\"\n%s\t\t\"\"\";\n%s", part1, indent(actual), |
115 | | - part2); |
116 | | - return fullText; |
117 | | - } |
118 | | - public static String indent(String actual) |
119 | | - { |
120 | | - String[] split = actual.split("\n"); |
121 | | - String output = ""; |
122 | | - for (String line : split) |
123 | | - { |
124 | | - output += "\t\t" + line + "\n"; |
125 | | - } |
126 | | - return output; |
| 81 | + return inlineJavaReporter.createReceived(actual); |
127 | 82 | } |
| 83 | + |
128 | 84 | public Options setForOptions(Options options) |
129 | 85 | { |
130 | | - if (reporter != null) |
| 86 | + if (inlineJavaReporter.reporter != null) |
131 | 87 | { |
132 | 88 | options = options.withReporter(this); |
133 | 89 | } |
|
0 commit comments