Skip to content

Commit b86308f

Browse files
committed
- r added tests for InlineJavaReporter
we're not sure this are good tests but we don't want to not have them either right now
1 parent 86d2680 commit b86308f

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
import org.approvaltests.Approvals;
44
import org.approvaltests.core.Options;
5+
import org.approvaltests.reporters.AutoApproveReporter;
56
import org.approvaltests.reporters.DiffMergeReporter;
67
import org.approvaltests.reporters.FirstWorkingReporter;
78
import org.approvaltests.reporters.QuietReporter;
89
import org.approvaltests.reporters.UseReporter;
910
import org.junit.jupiter.api.Test;
11+
import org.lambda.actions.Action1;
12+
import org.lambda.utils.Mutable;
1013

1114
import java.util.List;
1215

1316
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
1418

1519
public class InlineApprovalsTest
1620
{
@@ -128,4 +132,50 @@ private String greet(String name)
128132
129133
""".formatted(name, name, name, name, name, name);
130134
}
131-
}
135+
@Test
136+
void testSemiAutomatic()
137+
{
138+
var expected = """
139+
hello Lars
140+
***** DELETE ME TO APPROVE *****
141+
""";
142+
Options options = new Options().inline("", InlineOptions.semiAutomatic());
143+
Mutable<String> result = hijackInlineReporter(options, AutoApproveReporter.class);
144+
Action1<Throwable> assertion = e -> assertEquals(expected, result.get());
145+
assertApprovalFailure("hello Lars", options, assertion);
146+
}
147+
@Test
148+
void testAutomatic()
149+
{
150+
var expected = """
151+
hello Oskar
152+
""";
153+
Options options = new Options().inline("", InlineOptions.automatic());
154+
Mutable<String> result = hijackInlineReporter(options, AutoApproveReporter.class);
155+
Action1<Throwable> assertion = e -> assertEquals(expected, result.get());
156+
assertApprovalFailure("hello Oskar", options, assertion);
157+
}
158+
private static void assertApprovalFailure(String actual, Options options, Action1<Throwable> azzert)
159+
{
160+
boolean failed = true;
161+
try
162+
{
163+
Approvals.verify(actual, options);
164+
failed = false;
165+
}
166+
catch (Throwable t)
167+
{
168+
azzert.call(t);
169+
}
170+
assertTrue(failed, "Approval should have failed");
171+
}
172+
private static Mutable<String> hijackInlineReporter(Options options, Class<AutoApproveReporter> reporterClass)
173+
{
174+
InlineJavaReporter reporter = (InlineJavaReporter) options.getReporter();
175+
assertEquals(reporter.reporter.getClass(), reporterClass);
176+
reporter.reporter = new QuietReporter();
177+
Mutable<String> result = new Mutable<>("");
178+
reporter.createNewReceivedFileText = (s, a, m) -> result.set(a);
179+
return result;
180+
}
181+
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@
22

33
import com.spun.util.StringUtils;
44
import com.spun.util.io.FileUtils;
5+
import org.apache.commons.lang3.function.TriFunction;
56
import org.approvaltests.core.ApprovalFailureReporter;
67
import org.approvaltests.core.ApprovalReporterWithCleanUp;
78
import org.approvaltests.namer.StackTraceNamer;
9+
import org.lambda.functions.Function3;
810

911
import java.io.File;
1012
import java.util.regex.Matcher;
1113
import java.util.regex.Pattern;
1214

1315
public class InlineJavaReporter implements ApprovalFailureReporter, ApprovalReporterWithCleanUp
1416
{
15-
private final String sourceFilePath;
16-
private final StackTraceNamer stackTraceNamer;
17-
private final ApprovalFailureReporter reporter;
18-
private final String additionalLines;
17+
private final String sourceFilePath;
18+
private final StackTraceNamer stackTraceNamer;
19+
public ApprovalFailureReporter reporter;
20+
private final String additionalLines;
21+
public Function3<String, String, String, String> createNewReceivedFileText;
1922
public InlineJavaReporter(ApprovalFailureReporter reporter, boolean addApprovalLine)
2023
{
2124
this.reporter = reporter;
2225
this.stackTraceNamer = new StackTraceNamer();
2326
this.sourceFilePath = stackTraceNamer.getSourceFilePath();
24-
this.additionalLines = addApprovalLine ? "***** DELETE ME TO APPROVE *****" : "";
27+
this.additionalLines = addApprovalLine ? "***** DELETE ME TO APPROVE *****\n" : "";
28+
this.createNewReceivedFileText = InlineJavaReporter::createNewReceivedFileText;
2529
}
2630
public String getSourceFilePath()
2731
{
@@ -39,7 +43,7 @@ public String createReceived(String actual)
3943
String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java";
4044
String received = getReceivedFileName();
4145
String text = FileUtils.readFile(file);
42-
String fullText = createNewReceivedFileText(text, actual + additionalLines,
46+
String fullText = this.createNewReceivedFileText.call(text, actual + additionalLines,
4347
this.stackTraceNamer.getInfo().getMethodName());
4448
FileUtils.writeFile(new File(received), fullText);
4549
return received;
@@ -48,10 +52,10 @@ private String getReceivedFileName()
4852
{
4953
return sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt";
5054
}
51-
public static String createNewReceivedFileText(String text, String actual, String methodName)
55+
public static String createNewReceivedFileText(String javaSourceCode, String actual, String methodName)
5256
{
53-
text = text.replaceAll("\r\n", "\n");
54-
CodeParts codeParts = CodeParts.splitCode(text, methodName);
57+
javaSourceCode = javaSourceCode.replaceAll("\r\n", "\n");
58+
CodeParts codeParts = CodeParts.splitCode(javaSourceCode, methodName);
5559
if (codeParts.method.contains("expected = \"\"\""))
5660
{
5761
replaceExpected(codeParts, actual);

0 commit comments

Comments
 (0)