Skip to content

Commit

Permalink
- B handle empty lines at end of output when using inline
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Mar 4, 2024
1 parent 732570f commit 49e544f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
4 changes: 2 additions & 2 deletions approvaltests-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>15</source>
<target>15</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ private Double sum(Integer[] integers)
{
return Queryable.of(integers).sum();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import org.approvaltests.Approvals;
import org.approvaltests.core.Options;
import org.approvaltests.inline.InlineJavaReporter;
import org.approvaltests.reporters.DiffMergeReporter;
import org.approvaltests.reporters.FirstWorkingReporter;
import org.approvaltests.reporters.QuietReporter;
import org.approvaltests.reporters.UseReporter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;

import java.util.List;

Expand Down Expand Up @@ -105,4 +102,22 @@ public void testReportingCode()
assertEquals(QuietReporter.class,
((FirstWorkingReporter) inlineNoCode.getReporter()).getReporters()[1].getClass());
}
@Test
void testEmptyLineAtTheEnd()
{
var expected = """
Jeff Jeffty Jeff
born on Jeffteen of Jeff, Nineteen-eighty-Jeff
""";
Approvals.verify(greet("Jeff"), new Options().inline(expected));
}
private String greet(String name)
{
return """
%s %sty %s
born on %steen of %s, Nineteen-eighty-%s
""".formatted(name, name, name, name, name, name);
}
}
6 changes: 5 additions & 1 deletion approvaltests-util-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.spun.util;

import org.approvaltests.Approvals;
import org.approvaltests.reporters.AutoApproveReporter;
import org.approvaltests.reporters.UseReporter;
import org.approvaltests.utils.parseinput.ParseInput;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -117,4 +120,15 @@ public ReplaceUseCase(String startingString, String find, String replace, String
this.expectedString = expectedString;
}
}
@Test
@UseReporter(AutoApproveReporter.class)
public void testSplitting()
{
var expected = """
1a2aa3aa -> [1, 2, , 3, ]
1a2aa3a -> [1, 2, , 3]
1a2aa3 -> [1, 2, , 3]
""";
ParseInput.from(expected).verifyAll(s -> Arrays.toString(StringUtils.splitt(s, "a")));
}
}
10 changes: 10 additions & 0 deletions approvaltests-util/src/main/java/com/spun/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,14 @@ public static String removeFromEnd(String contents, int length)
{
return contents.substring(0, contents.length() - length);
}
public static String[] splitt(String input, String pattern)
{
if (input.endsWith(pattern))
{
input = input + " ";
String[] splitted = input.split(pattern, -1);
return ArrayUtils.getSubsection(splitted, 0, splitted.length - 1);
}
return input.split(pattern);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.approvaltests.inline;

import com.spun.util.StringUtils;
import com.spun.util.io.FileUtils;
import org.approvaltests.core.ApprovalFailureReporter;
import org.approvaltests.core.ApprovalReporterWithCleanUp;
Expand Down Expand Up @@ -82,7 +83,7 @@ private static void replaceExpected(CodeParts codeParts, String actual)
}
public static String indent(String actual, String tab)
{
String[] split = actual.split("\n");
String[] split = StringUtils.splitt(actual, "\n");
String output = "";
for (String line : split)
{
Expand Down

0 comments on commit 49e544f

Please sign in to comment.