From 61b9af003920d2a867e0274e82beb4ffa6da7e6d Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 22 Nov 2023 20:20:58 +0000 Subject: [PATCH] a reformat code --- .../approvaltests/inline/CodePartsTest.java | 48 ++--- .../org/approvaltests/inline/CodeParts.java | 107 ++++++----- .../inline/InlineJavaReporter.java | 178 +++++++++--------- 3 files changed, 169 insertions(+), 164 deletions(-) diff --git a/approvaltests-tests/src/test/java/org/approvaltests/inline/CodePartsTest.java b/approvaltests-tests/src/test/java/org/approvaltests/inline/CodePartsTest.java index 44cb5d9d..0443dbbd 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/inline/CodePartsTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/inline/CodePartsTest.java @@ -3,28 +3,30 @@ import org.approvaltests.Approvals; import org.junit.jupiter.api.Test; -class CodePartsTest { - @Test - public void testSplit() { - var code = """ - public class Test { - @Test - void testyMctest() { - Customer customer = Customer.create() - .existingCustomer() - .speakingEnglish() - .build(); - stateService.save(TestUtils.userId().build(), customer); - var expected = ""\" - [Customer]: account lookup - [ Bot]: Hi there! - [ Bot]: Let me try to help. - [ Bot]: routes to '12345' - ""\"; - verifyConversation(expected, "account lookup"); - } +class CodePartsTest +{ + @Test + public void testSplit() + { + var code = """ + public class Test { + @Test + void testyMctest() { + Customer customer = Customer.create() + .existingCustomer() + .speakingEnglish() + .build(); + stateService.save(TestUtils.userId().build(), customer); + var expected = ""\" + [Customer]: account lookup + [ Bot]: Hi there! + [ Bot]: Let me try to help. + [ Bot]: routes to '12345' + ""\"; + verifyConversation(expected, "account lookup"); } - """; - Approvals.verify(CodeParts.splitCode(code, "testyMctest")); - } + } + """; + Approvals.verify(CodeParts.splitCode(code, "testyMctest")); + } } diff --git a/approvaltests/src/main/java/org/approvaltests/inline/CodeParts.java b/approvaltests/src/main/java/org/approvaltests/inline/CodeParts.java index ebdde174..9ac17d0a 100644 --- a/approvaltests/src/main/java/org/approvaltests/inline/CodeParts.java +++ b/approvaltests/src/main/java/org/approvaltests/inline/CodeParts.java @@ -5,60 +5,59 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -public class CodeParts { - public String before; - public String method; - public String after; - public String tab; - - public static CodeParts splitCode(String text, String methodName) { - CodeParts codeParts = new CodeParts(); - String[] lines = text.split("\n"); - int start = 0; - int end = 0; - for (int i = 0; i < lines.length; i++) { - String line = lines[i]; - if (start == 0) { - if (line.contains("void " + methodName + "(")) { - start = i; - codeParts.tab = extractLeadingWhitespace(line); - - } - } else if (end == 0) { - if (line.startsWith(codeParts.tab + "}")) { - end = i + 1; - break; - } - } - } - codeParts.before = String.join("\n", ArrayUtils.getSubsection(lines, 0, start)); - codeParts.method = String.join("\n", ArrayUtils.getSubsection(lines, start, end)); - codeParts.after = String.join("\n", ArrayUtils.getSubsection(lines, end, lines.length)); - return codeParts; - } - - public String getFullCode() { - return before + "\n" + - method + "\n" + - after; - } - - private static String extractLeadingWhitespace(String text) +public class CodeParts +{ + public String before; + public String method; + public String after; + public String tab; + public static CodeParts splitCode(String text, String methodName) + { + CodeParts codeParts = new CodeParts(); + String[] lines = text.split("\n"); + int start = 0; + int end = 0; + for (int i = 0; i < lines.length; i++) { - Pattern pattern = Pattern.compile("^\\s+"); - Matcher matcher = pattern.matcher(text); - if (matcher.find()) - { return matcher.group(); } - return "\t"; - } - - @Override - public String toString() { - return "CodeParts{\n" + - "before:\n" + before + "\n" + - "method:\n" + method + "\n" + - "after:\n" + after + "\n" + - "tab:\n" + tab + "\n" + - '}'; + String line = lines[i]; + if (start == 0) + { + if (line.contains("void " + methodName + "(")) + { + start = i; + codeParts.tab = extractLeadingWhitespace(line); + } + } + else if (end == 0) + { + if (line.startsWith(codeParts.tab + "}")) + { + end = i + 1; + break; + } + } } + codeParts.before = String.join("\n", ArrayUtils.getSubsection(lines, 0, start)); + codeParts.method = String.join("\n", ArrayUtils.getSubsection(lines, start, end)); + codeParts.after = String.join("\n", ArrayUtils.getSubsection(lines, end, lines.length)); + return codeParts; + } + public String getFullCode() + { + return before + "\n" + method + "\n" + after; + } + private static String extractLeadingWhitespace(String text) + { + Pattern pattern = Pattern.compile("^\\s+"); + Matcher matcher = pattern.matcher(text); + if (matcher.find()) + { return matcher.group(); } + return "\t"; + } + @Override + public String toString() + { + return "CodeParts{\n" + "before:\n" + before + "\n" + "method:\n" + method + "\n" + "after:\n" + after + "\n" + + "tab:\n" + tab + "\n" + '}'; + } } diff --git a/approvaltests/src/main/java/org/approvaltests/inline/InlineJavaReporter.java b/approvaltests/src/main/java/org/approvaltests/inline/InlineJavaReporter.java index 17bd4af2..c6ca5a74 100644 --- a/approvaltests/src/main/java/org/approvaltests/inline/InlineJavaReporter.java +++ b/approvaltests/src/main/java/org/approvaltests/inline/InlineJavaReporter.java @@ -9,94 +9,98 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -public class InlineJavaReporter implements ApprovalFailureReporter, ApprovalReporterWithCleanUp { - private final String sourceFilePath; - private final StackTraceNamer stackTraceNamer; - private final ApprovalFailureReporter reporter; - - public InlineJavaReporter(ApprovalFailureReporter reporter) { - this.reporter = reporter; - this.stackTraceNamer = new StackTraceNamer(); - this.sourceFilePath = stackTraceNamer.getSourceFilePath(); - } - - public String getSourceFilePath() { - return sourceFilePath; +public class InlineJavaReporter implements ApprovalFailureReporter, ApprovalReporterWithCleanUp +{ + private final String sourceFilePath; + private final StackTraceNamer stackTraceNamer; + private final ApprovalFailureReporter reporter; + public InlineJavaReporter(ApprovalFailureReporter reporter) + { + this.reporter = reporter; + this.stackTraceNamer = new StackTraceNamer(); + this.sourceFilePath = stackTraceNamer.getSourceFilePath(); + } + public String getSourceFilePath() + { + return sourceFilePath; + } + @Override + public boolean report(String received, String approved) + { + String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; + String newSource = createReceived(FileUtils.readFile(received)); + return reporter.report(newSource, sourceFile); + } + public String createReceived(String actual) + { + String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; + String received = getReceivedFileName(); + String text = FileUtils.readFile(file); + String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName()); + FileUtils.writeFile(new File(received), fullText); + return received; + } + private String getReceivedFileName() + { + return sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt"; + } + public static String createNewReceivedFileText(String text, String actual, String methodName) + { + text = text.replaceAll("\r\n", "\n"); + CodeParts codeParts = CodeParts.splitCode(text, methodName); + if (codeParts.method.contains("expected = \"\"\"")) + { + replaceExpected(codeParts, actual); } - - @Override - public boolean report(String received, String approved) { - String sourceFile = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; - String newSource = createReceived(FileUtils.readFile(received)); - return reporter.report(newSource, sourceFile); - } - - public String createReceived(String actual) { - String file = sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".java"; - String received = getReceivedFileName(); - String text = FileUtils.readFile(file); - String fullText = createNewReceivedFileText(text, actual, this.stackTraceNamer.getInfo().getMethodName()); - FileUtils.writeFile(new File(received), fullText); - return received; + else + { + addExpected(codeParts, actual); } - - private String getReceivedFileName() { - return sourceFilePath + stackTraceNamer.getInfo().getClassName() + ".received.txt"; - } - - public static String createNewReceivedFileText(String text, String actual, String methodName) { - text = text.replaceAll("\r\n", "\n"); - CodeParts codeParts = CodeParts.splitCode(text, methodName); - if (codeParts.method.contains("expected = \"\"\"")) { - replaceExpected(codeParts, actual); - } else { - addExpected(codeParts, actual); - } - return codeParts.getFullCode(); - } - - private static void addExpected(CodeParts codeParts, String actual) { - int start = codeParts.method.indexOf("{") + 2; - String before = codeParts.method.substring(0, start); - String after = codeParts.method.substring(start); - codeParts.method = before + getExpected(actual, codeParts.tab) + after; - } - - private static String getExpected(String actual, String tab) { - return String.format("%s%svar expected = \"\"\"\n%s%s%s%s\"\"\";\n", - tab, tab, indent(actual, tab), tab, tab, tab); - } - - private static void replaceExpected(CodeParts codeParts, String actual) { - int start = codeParts.method.indexOf("expected = \"\"\""); - start = codeParts.method.substring(0, start).lastIndexOf("\n") + 1; - int end = codeParts.method.indexOf("\"\"\";"); - end = codeParts.method.indexOf("\n", end) + 1; - String before = codeParts.method.substring(0, start); - String after = codeParts.method.substring(end); - codeParts.method = before + getExpected(actual, codeParts.tab) + after; - } - - public static String indent(String actual, String tab) { - String[] split = actual.split("\n"); - String output = ""; - for (String line : split) { - output += tab + tab + tab + line + "\n"; - } - return output; - } - - private static String extractLeadingWhitespace(String text) { - Pattern pattern = Pattern.compile("^\\s+"); - Matcher matcher = pattern.matcher(text); - if (matcher.find()) { - return matcher.group(); - } - return "\t"; - } - - @Override - public void cleanUp(String received, String approved) { - FileUtils.delete(getReceivedFileName()); + return codeParts.getFullCode(); + } + private static void addExpected(CodeParts codeParts, String actual) + { + int start = codeParts.method.indexOf("{") + 2; + String before = codeParts.method.substring(0, start); + String after = codeParts.method.substring(start); + codeParts.method = before + getExpected(actual, codeParts.tab) + after; + } + private static String getExpected(String actual, String tab) + { + return String.format("%s%svar expected = \"\"\"\n%s%s%s%s\"\"\";\n", tab, tab, indent(actual, tab), tab, tab, + tab); + } + private static void replaceExpected(CodeParts codeParts, String actual) + { + int start = codeParts.method.indexOf("expected = \"\"\""); + start = codeParts.method.substring(0, start).lastIndexOf("\n") + 1; + int end = codeParts.method.indexOf("\"\"\";"); + end = codeParts.method.indexOf("\n", end) + 1; + String before = codeParts.method.substring(0, start); + String after = codeParts.method.substring(end); + codeParts.method = before + getExpected(actual, codeParts.tab) + after; + } + public static String indent(String actual, String tab) + { + String[] split = actual.split("\n"); + String output = ""; + for (String line : split) + { + output += tab + tab + tab + line + "\n"; } + return output; + } + private static String extractLeadingWhitespace(String text) + { + Pattern pattern = Pattern.compile("^\\s+"); + Matcher matcher = pattern.matcher(text); + if (matcher.find()) + { return matcher.group(); } + return "\t"; + } + @Override + public void cleanUp(String received, String approved) + { + FileUtils.delete(getReceivedFileName()); + } }