From cfb88393594df6b02c4a7a09aa299683702ad765 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 26 Mar 2025 21:59:57 -0700 Subject: [PATCH] Add a method to compare text ignoring line separators and whitespaces --- .../bld/extension/GeneratedVersionTest.java | 49 +++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/test/java/rife/bld/extension/GeneratedVersionTest.java b/src/test/java/rife/bld/extension/GeneratedVersionTest.java index 28d192a..2f7a054 100644 --- a/src/test/java/rife/bld/extension/GeneratedVersionTest.java +++ b/src/test/java/rife/bld/extension/GeneratedVersionTest.java @@ -82,6 +82,30 @@ class GeneratedVersionTest { } } + /** + * 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); + } + @Test void testBuildCustomTemplate() { var gv = new GeneratedVersion(); @@ -91,21 +115,18 @@ class GeneratedVersionTest { gv.setProjectName("My App"); gv.setClassName("MyVersion"); - var eol = System.lineSeparator(); var t = gv.buildTemplate(); - assertThat(t.getContent()).isEqualTo("package com.example.my;" + eol + - eol + - "public final class MyVersion {" + eol + - " public static final int PROJECT = \"My App\";" + eol + - " public static final int MAJOR = 2;" + eol + - " public static final int MINOR = 1;" + eol + - " public static final int REVISION = 3;" + eol + - " public static final String QUALIFIER = \"\";" + eol + - eol + - " private MyVersion() {" + eol + - " // no-op" + eol + - " }" + eol + - "}" + eol); + assertThat(compareTextIgnoringLineSeparators(t.getContent(),"package com.example.my;" + + "public final class MyVersion {" + + " public static final int PROJECT = \"My App\";" + + " public static final int MAJOR = 2;" + + " public static final int MINOR = 1;" + + " public static final int REVISION = 3;" + + " public static final String QUALIFIER = \"\";" + + " private MyVersion() {" + + " // no-op" + + " }" + + "}")).isTrue(); } @Test