From 9d34713a970d9d10450279eac173307ebf9e3350 Mon Sep 17 00:00:00 2001 From: Lars Eckart Date: Mon, 6 May 2024 20:07:03 +0300 Subject: [PATCH] - F *breaking change* Introduce InlineOptions --- .../inline/InlineApprovalsTest.java | 6 ++-- .../java/org/approvaltests/core/Options.java | 7 ++-- .../inline/InlineComparator.java | 25 ++++---------- .../approvaltests/inline/InlineOptions.java | 33 +++++++++++++++++++ 4 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 approvaltests/src/main/java/org/approvaltests/inline/InlineOptions.java diff --git a/approvaltests-tests/src/test/java/org/approvaltests/inline/InlineApprovalsTest.java b/approvaltests-tests/src/test/java/org/approvaltests/inline/InlineApprovalsTest.java index 75f820a0..eaff5c55 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/inline/InlineApprovalsTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/inline/InlineApprovalsTest.java @@ -103,8 +103,8 @@ void testyMctest() { @UseReporter(QuietReporter.class) public void testReportingCode() { - Options inlineWithCode = new Options().inline("", true); - Options inlineNoCode = new Options().inline("", false); + Options inlineWithCode = new Options().inline("", InlineOptions.showCode(true)); + Options inlineNoCode = new Options().inline("", InlineOptions.showCode(false)); var resultWithCode = inlineWithCode.getReporter(); assertEquals(InlineJavaReporter.class, resultWithCode.getClass()); assertEquals(QuietReporter.class, @@ -116,7 +116,7 @@ void testEmptyLineAtTheEnd() var expected = """ Jeff Jeffty Jeff born on Jeffteen of Jeff, Nineteen-eighty-Jeff - + """; Approvals.verify(greet("Jeff"), new Options().inline(expected)); } diff --git a/approvaltests/src/main/java/org/approvaltests/core/Options.java b/approvaltests/src/main/java/org/approvaltests/core/Options.java index 739545d1..239fb3c8 100644 --- a/approvaltests/src/main/java/org/approvaltests/core/Options.java +++ b/approvaltests/src/main/java/org/approvaltests/core/Options.java @@ -5,6 +5,7 @@ import org.approvaltests.ReporterFactory; import org.approvaltests.approvers.FileApprover; import org.approvaltests.inline.InlineComparator; +import org.approvaltests.inline.InlineOptions; import org.approvaltests.namer.ApprovalNamer; import org.approvaltests.namer.NamerWrapper; import org.approvaltests.scrubbers.NoOpScrubber; @@ -32,11 +33,11 @@ public Options and(Function1 optionsUpdate) } public Options inline(String expected) { - return inline(expected, true); + return inline(expected, InlineOptions.showCode(true)); } - public Options inline(String expected, boolean showCode) + public Options inline(String expected, InlineOptions inlineOptions) { - InlineComparator inline = new InlineComparator(expected, showCode ? this.getReporter() : null); + InlineComparator inline = new InlineComparator(expected, inlineOptions); return inline.setForOptions(this); } public Options(Scrubber scrubber) diff --git a/approvaltests/src/main/java/org/approvaltests/inline/InlineComparator.java b/approvaltests/src/main/java/org/approvaltests/inline/InlineComparator.java index d904fbbd..72507e71 100644 --- a/approvaltests/src/main/java/org/approvaltests/inline/InlineComparator.java +++ b/approvaltests/src/main/java/org/approvaltests/inline/InlineComparator.java @@ -1,7 +1,6 @@ package org.approvaltests.inline; import com.spun.util.io.FileUtils; -import org.approvaltests.core.ApprovalFailureReporter; import org.approvaltests.core.Options; import org.approvaltests.namer.ApprovalNamer; @@ -13,17 +12,14 @@ public class InlineComparator implements ApprovalNamer { - private ApprovalFailureReporter reporter = null; - private final String expected; - private File approvedFile; - private File receivedFile; - public InlineComparator(String expected, ApprovalFailureReporter reporter) + private final InlineOptions inlineOptions; + private final String expected; + private File approvedFile; + private File receivedFile; + public InlineComparator(String expected, InlineOptions inlineOptions) { this.expected = expected; - if (reporter != null) - { - this.reporter = new InlineJavaReporter(reporter); - } + this.inlineOptions = inlineOptions; } @Override public File getApprovedFile(String extensionWithDot) @@ -73,16 +69,9 @@ public String getSourceFilePath() { return ""; } - public boolean report(String received, String approved) - { - return reporter.report(received, approved); - } public Options setForOptions(Options options) { - if (reporter != null) - { - options = options.withReporter(reporter); - } + options = inlineOptions.apply(options); return options.forFile().withNamer(this); } } diff --git a/approvaltests/src/main/java/org/approvaltests/inline/InlineOptions.java b/approvaltests/src/main/java/org/approvaltests/inline/InlineOptions.java new file mode 100644 index 00000000..aaf13278 --- /dev/null +++ b/approvaltests/src/main/java/org/approvaltests/inline/InlineOptions.java @@ -0,0 +1,33 @@ +package org.approvaltests.inline; + +import org.approvaltests.core.Options; + +public interface InlineOptions +{ + Options apply(Options options); + public static InlineOptions showCode(boolean doShowCode) + { + if (doShowCode) + { + return new ShowCodeInlineOptions(); + } + else + { + return new DoNotShowCodeInlineOptions(); + } + } + public static class ShowCodeInlineOptions implements InlineOptions + { + public Options apply(Options options) + { + return options.withReporter(new InlineJavaReporter(options.getReporter())); + } + } + public static class DoNotShowCodeInlineOptions implements InlineOptions + { + public Options apply(Options options) + { + return options; + } + } +}