Skip to content

Commit 48e5460

Browse files
committed
- F InlineOptions.semiAutomaticWithPreviousApproved()
1 parent 2f264d8 commit 48e5460

File tree

5 files changed

+80
-8
lines changed

5 files changed

+80
-8
lines changed

approvaltests-tests/src/test/java/org/approvaltests/inline/InlineApprovalsTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,40 @@ private static Mutable<String> hijackInlineReporter(Options options)
177177
reporter.createNewReceivedFileText = (s, a, m) -> result.set(a);
178178
return result;
179179
}
180-
}
180+
@Test
181+
void testSemiAutomaticMessage()
182+
{
183+
var expected = """
184+
41
185+
***** DELETE ME TO APPROVE *****
186+
""";
187+
var options = new Options().inline(expected, InlineOptions.semiAutomatic());
188+
try
189+
{
190+
Approvals.verify("41", options);
191+
}
192+
catch (Throwable e)
193+
{
194+
}
195+
Approvals.verify(expected);
196+
}
197+
@Test
198+
void testSemiAutomaticWithPreviousApproved()
199+
{
200+
var expected = """
201+
42
202+
***** DELETE ME TO APPROVE *****
203+
vvvvv PREVIOUS RESULT vvvvv
204+
41
205+
""";
206+
var options = new Options().inline(expected, InlineOptions.semiAutomaticWithPreviousApproved());
207+
try
208+
{
209+
Approvals.verify("42", options);
210+
}
211+
catch (Throwable e)
212+
{
213+
}
214+
Approvals.verify(expected);
215+
}
216+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
41
2+
***** DELETE ME TO APPROVE *****
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
42
2+
***** DELETE ME TO APPROVE *****
3+
vvvvv PREVIOUS RESULT vvvvv
4+
41

approvaltests/src/main/java/org/approvaltests/inline/InlineJavaReporter.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import com.spun.util.StringUtils;
44
import com.spun.util.io.FileUtils;
5-
import org.apache.commons.lang3.function.TriFunction;
65
import org.approvaltests.core.ApprovalFailureReporter;
76
import org.approvaltests.core.ApprovalReporterWithCleanUp;
87
import org.approvaltests.namer.StackTraceNamer;
8+
import org.lambda.functions.Function2;
99
import org.lambda.functions.Function3;
1010

1111
import java.io.File;
@@ -16,16 +16,21 @@ public class InlineJavaReporter implements ApprovalFailureReporter, ApprovalRepo
1616
{
1717
private final String sourceFilePath;
1818
private final StackTraceNamer stackTraceNamer;
19+
private Function2<String, String, String> footerCreator;
1920
public ApprovalFailureReporter reporter;
20-
private final String additionalLines;
21+
private String additionalLines;
2122
public Function3<String, String, String, String> createNewReceivedFileText;
22-
public InlineJavaReporter(ApprovalFailureReporter reporter, boolean addApprovalLine)
23+
public InlineJavaReporter(ApprovalFailureReporter reporter, Function2<String, String, String> footerCreator)
2324
{
2425
this.reporter = reporter;
2526
this.stackTraceNamer = new StackTraceNamer();
2627
this.sourceFilePath = stackTraceNamer.getSourceFilePath();
27-
this.additionalLines = addApprovalLine ? "***** DELETE ME TO APPROVE *****\n" : "";
2828
this.createNewReceivedFileText = InlineJavaReporter::createNewReceivedFileText;
29+
if (footerCreator == null)
30+
{
31+
footerCreator = (source, actual) -> "";
32+
}
33+
this.footerCreator = footerCreator;
2934
}
3035
public String getSourceFilePath()
3136
{
@@ -34,6 +39,7 @@ public String getSourceFilePath()
3439
@Override
3540
public boolean report(String received, String approved)
3641
{
42+
additionalLines = footerCreator.call(received, approved);
3743
String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
3844
String newSource = createReceived(FileUtils.readFile(received));
3945
return reporter.report(newSource, sourceFile);
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package org.approvaltests.inline;
22

3+
import com.spun.util.io.FileUtils;
34
import org.approvaltests.core.Options;
45
import org.approvaltests.reporters.AutoApproveReporter;
56

67
public interface InlineOptions
78
{
9+
String DELETE_ME_TO_APPROVE = "***** DELETE ME TO APPROVE *****\n";
10+
String PREVIOUS_RESULT = "vvvvv PREVIOUS RESULT vvvvv\n";
811
Options apply(Options options);
912
public static InlineOptions showCode(boolean doShowCode)
1013
{
1114
if (doShowCode)
1215
{
13-
return options -> options.withReporter(new InlineJavaReporter(options.getReporter(), false));
16+
return options -> options.withReporter(new InlineJavaReporter(options.getReporter(), null));
1417
}
1518
else
1619
{
@@ -19,10 +22,31 @@ public static InlineOptions showCode(boolean doShowCode)
1922
}
2023
public static InlineOptions automatic()
2124
{
22-
return options -> options.withReporter(new InlineJavaReporter(new AutoApproveReporter(), false));
25+
return options -> options.withReporter(new InlineJavaReporter(new AutoApproveReporter(), null));
2326
}
2427
public static InlineOptions semiAutomatic()
2528
{
26-
return options -> options.withReporter(new InlineJavaReporter(new AutoApproveReporter(), true));
29+
return options -> options
30+
.withReporter(new InlineJavaReporter(new AutoApproveReporter(), (x, y) -> DELETE_ME_TO_APPROVE));
31+
}
32+
public static InlineOptions semiAutomaticWithPreviousApproved()
33+
{
34+
return options -> options.withReporter(
35+
new InlineJavaReporter(new AutoApproveReporter(), InlineOptions::createPreviousCaptureFooter));
36+
}
37+
static String createPreviousCaptureFooter(String receivedPath, String approvedPath)
38+
{
39+
String approvedText = FileUtils.readFile((approvedPath));
40+
approvedText = approvedText.substring(0, approvedText.lastIndexOf("\n"));
41+
int previousResultIndex = approvedText.lastIndexOf(PREVIOUS_RESULT);
42+
if (previousResultIndex != -1)
43+
{
44+
approvedText = approvedText.substring(previousResultIndex + PREVIOUS_RESULT.length());
45+
}
46+
String receivedText = FileUtils.readFile((receivedPath));
47+
receivedText = receivedText.substring(0, receivedText.lastIndexOf("\n"));
48+
if (!receivedText.equals(approvedText))
49+
{ return DELETE_ME_TO_APPROVE + PREVIOUS_RESULT + approvedText; }
50+
return "";
2751
}
2852
}

0 commit comments

Comments
 (0)