Add method to compare text ignoring line separators and whitespaces

This commit is contained in:
Erik C. Thauvin 2025-03-26 15:11:27 -07:00
parent 90e584c0f1
commit 8ecb70758b
Signed by: erik
GPG key ID: 776702A6A2DA330E

View file

@ -55,6 +55,30 @@ class VersionProcessorTest {
private final VersionProcessor processor = new VersionProcessor(); private final VersionProcessor processor = new VersionProcessor();
private final VersionTest version = new VersionTest(); private final VersionTest version = new VersionTest();
/**
* Compares two strings by removing all line separators and whitespace.
*
* @param text1 The first text to compare
* @param text2 The second text to compare
* @return true if the texts are equivalent when line separators are ignored, false otherwise
*/
static boolean compareTextIgnoringLineSeparators(String text1, String text2) {
// Handle null cases
if (text1 == null && text2 == null) {
return true;
}
if (text1 == null || text2 == null) {
return false;
}
// Remove all line separators and whitespace
var cleanedText1 = text1.replaceAll("\\r?\\n|\\r|\\s", "");
var cleanedText2 = text2.replaceAll("\\r?\\n|\\r|\\s", "");
// Compare the cleaned strings
return cleanedText1.equals(cleanedText2);
}
@SuppressWarnings("PMD.AvoidAccessibilityAlteration") @SuppressWarnings("PMD.AvoidAccessibilityAlteration")
@Test @Test
void testCompileTemplate() void testCompileTemplate()
@ -69,45 +93,40 @@ class VersionProcessorTest {
try (var writer = new StringWriter()) { try (var writer = new StringWriter()) {
mustache.execute(writer, version).flush(); mustache.execute(writer, version).flush();
assertEquals(String.format(""" assertTrue(compareTextIgnoringLineSeparators(
/* String.format("/*" +
* This file is automatically generated. " * This file is automatically generated." +
* Do not modify! -- ALL CHANGES WILL BE ERASED! " * Do not modify! -- ALL CHANGES WILL BE ERASED!" +
*/ " */" +
"package %s;" +
package %s; "import java.util.Date;" +
"/**" +
import java.util.Date; " * Provides semantic version information." +
" *" +
/** " * @author <a href=\"https://github.com/ethauvin/semver\">Semantic Version Annotation Processor</a>" +
* Provides semantic version information. " */" +
* "public final class %s {" +
* @author <a href="https://github.com/ethauvin/semver">Semantic Version Annotation Processor</a> " public static final String PROJECT = \"%s\";" +
*/ " public static final Date BUILDDATE = new Date(L);" +
public final class %s { " public static final int MAJOR = %d;" +
public static final String PROJECT = "%s"; " public static final int MINOR = %d;" +
public static final Date BUILDDATE = new Date(L); " public static final int PATCH = %d;" +
public static final int MAJOR = %d; " public static final String PRERELEASE = \"%s\";" +
public static final int MINOR = %d; " public static final String PRERELEASE_PREFIX = \"%s\";" +
public static final int PATCH = %d; " public static final String BUILDMETA = \"%s\";" +
public static final String PRERELEASE = "%s"; " public static final String BUILDMETA_PREFIX = \"%s\";" +
public static final String PRERELEASE_PREFIX = "%s"; " public static final String SEPARATOR = \"%s\";" +
public static final String BUILDMETA = "%s"; " public static final String VERSION = \"\";" +
public static final String BUILDMETA_PREFIX = "%s"; " /**" +
public static final String SEPARATOR = "%s"; " * Disables the default constructor." +
public static final String VERSION = ""; " */" +
" private %s() {" +
/** " throw new UnsupportedOperationException(\"Illegal constructor call.\");" +
* Disables the default constructor. " }" +
*/ "}", version.packageName(), version.className(), version.project(), version.major(),
private %s() {
throw new UnsupportedOperationException("Illegal constructor call.");
}
}
""", version.packageName(), version.className(), version.project(), version.major(),
version.minor(), version.patch(), version.preRelease(), version.preReleasePrefix(), version.minor(), version.patch(), version.preRelease(), version.preReleasePrefix(),
version.buildMeta(), version.buildMetaPrefix(), version.separator(), version.className()), version.buildMeta(), version.buildMetaPrefix(), version.separator(), version.className()),
writer.toString()); writer.toString()));
} }
} }