From 8ecb70758b2e8448811595f9ce3c4af68597752a Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 26 Mar 2025 15:11:27 -0700 Subject: [PATCH] Add method to compare text ignoring line separators and whitespaces --- .../erik/semver/VersionProcessorTest.java | 93 +++++++++++-------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java b/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java index 97622d8..c49af74 100644 --- a/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java +++ b/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java @@ -55,6 +55,30 @@ class VersionProcessorTest { private final VersionProcessor processor = new VersionProcessor(); 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") @Test void testCompileTemplate() @@ -69,45 +93,40 @@ class VersionProcessorTest { try (var writer = new StringWriter()) { mustache.execute(writer, version).flush(); - assertEquals(String.format(""" - /* - * This file is automatically generated. - * Do not modify! -- ALL CHANGES WILL BE ERASED! - */ - - package %s; - - import java.util.Date; - - /** - * Provides semantic version information. - * - * @author Semantic Version Annotation Processor - */ - public final class %s { - public static final String PROJECT = "%s"; - public static final Date BUILDDATE = new Date(L); - public static final int MAJOR = %d; - public static final int MINOR = %d; - public static final int PATCH = %d; - public static final String PRERELEASE = "%s"; - public static final String PRERELEASE_PREFIX = "%s"; - public static final String BUILDMETA = "%s"; - public static final String BUILDMETA_PREFIX = "%s"; - public static final String SEPARATOR = "%s"; - public static final String VERSION = ""; - - /** - * Disables the default constructor. - */ - private %s() { - throw new UnsupportedOperationException("Illegal constructor call."); - } - } - """, version.packageName(), version.className(), version.project(), version.major(), + assertTrue(compareTextIgnoringLineSeparators( + String.format("/*" + + " * This file is automatically generated." + + " * Do not modify! -- ALL CHANGES WILL BE ERASED!" + + " */" + + "package %s;" + + "import java.util.Date;" + + "/**" + + " * Provides semantic version information." + + " *" + + " * @author Semantic Version Annotation Processor" + + " */" + + "public final class %s {" + + " public static final String PROJECT = \"%s\";" + + " public static final Date BUILDDATE = new Date(L);" + + " public static final int MAJOR = %d;" + + " public static final int MINOR = %d;" + + " public static final int PATCH = %d;" + + " public static final String PRERELEASE = \"%s\";" + + " public static final String PRERELEASE_PREFIX = \"%s\";" + + " public static final String BUILDMETA = \"%s\";" + + " public static final String BUILDMETA_PREFIX = \"%s\";" + + " public static final String SEPARATOR = \"%s\";" + + " public static final String VERSION = \"\";" + + " /**" + + " * Disables the default constructor." + + " */" + + " 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.buildMeta(), version.buildMetaPrefix(), version.separator(), version.className()), - writer.toString()); + writer.toString())); } }