Compare commits

...

2 commits

View file

@ -71,6 +71,30 @@ class GeneratedVersionTest {
logger.setUseParentHandlers(false);
}
/**
* 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);
}
static void deleteOnExit(File folder) {
folder.deleteOnExit();
for (var f : Objects.requireNonNull(folder.listFiles())) {
@ -91,21 +115,23 @@ 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);
var v = gv.getProject().version();
assertThat(v).extracting("majorInt", "minorInt", "revisionInt").as("version")
.containsExactly(2, 1, 3);
assertThat(compareTextIgnoringLineSeparators(t.getContent(),
String.format("package %s;" +
"public final class %s {" +
" public static final int PROJECT = \"%s\";" +
" public static final int MAJOR = %d;" +
" public static final int MINOR = %d;" +
" public static final int REVISION = %d;" +
" public static final String QUALIFIER = \"\";" +
" private MyVersion() {" +
" // no-op" +
" }" +
"}", gv.getPackageName(), gv.getClassName(), gv.getProjectName(), v.majorInt(),
v.minorInt(), v.revisionInt()))).as("template").isTrue();
}
@Test