Fix StringWithPlaceholders.containsPlaceholders() and update test

This commit is contained in:
filoghost 2021-08-04 21:32:36 +02:00
parent 2d99a0c36a
commit 3432a33ae6
3 changed files with 19 additions and 10 deletions

View File

@ -60,7 +60,7 @@ class DisplayText {
}
boolean updateGlobalReplacements() {
if (!textWithoutReplacements.containsPlaceholders()) {
if (!textWithoutReplacements.containsUnreplacedPlaceholders()) {
return false;
}

View File

@ -40,12 +40,22 @@ public final class StringWithPlaceholders {
return string;
}
public boolean containsPlaceholders() {
return string != null && stringParts != null;
public boolean containsUnreplacedPlaceholders() {
if (string == null || stringParts == null) {
return false;
}
for (StringPart stringPart : stringParts) {
if (stringPart instanceof PlaceholderPart) {
return true;
}
}
return false;
}
public boolean anyPlaceholderMatch(Predicate<PlaceholderOccurrence> filter) {
if (!containsPlaceholders()) {
if (stringParts == null) {
return false;
}
@ -62,7 +72,7 @@ public final class StringWithPlaceholders {
}
public boolean anyLiteralPartMatch(Predicate<String> filter) {
if (!containsPlaceholders()) {
if (stringParts == null) {
return filter.test(string);
}
@ -78,7 +88,7 @@ public final class StringWithPlaceholders {
}
public @NotNull StringWithPlaceholders partiallyReplacePlaceholders(PlaceholderReplaceFunction replaceFunction) {
if (!containsPlaceholders()) {
if (stringParts == null) {
return this;
}
@ -110,7 +120,7 @@ public final class StringWithPlaceholders {
}
public String replaceParts(PlaceholderReplaceFunction placeholderReplaceFunction, UnaryOperator<String> literalPartReplaceFunction) {
if (!containsPlaceholders()) {
if (string == null || stringParts == null) {
return literalPartReplaceFunction.apply(string);
}

View File

@ -25,18 +25,17 @@ class StringWithPlaceholdersTest {
StringWithPlaceholders s = StringWithPlaceholders.of(input);
assertThat(s.replacePlaceholders(occurrence -> "#")).isEqualTo(expectedOutput);
assertThat(s.containsPlaceholders()).isEqualTo(expectedContainsPlaceholders);
assertThat(s.containsUnreplacedPlaceholders()).isEqualTo(expectedContainsPlaceholders);
}
@ParameterizedTest(name = "[{index}] {0} -> {1}")
@MethodSource("replacementsTestArguments")
void partialReplacements(String input, String expectedOutput) {
boolean expectedContainsPlaceholders = expectedOutput.contains("#");
StringWithPlaceholders s = StringWithPlaceholders.of(input);
assertThat(s.partiallyReplacePlaceholders(occurrence -> "#").getString()).isEqualTo(expectedOutput);
assertThat(s.partiallyReplacePlaceholders(occurrence -> null).replacePlaceholders(occurrence -> "#")).isEqualTo(expectedOutput);
assertThat(s.containsPlaceholders()).isEqualTo(expectedContainsPlaceholders);
assertThat(s.partiallyReplacePlaceholders(occurrence -> "#").containsUnreplacedPlaceholders()).isFalse();
}
static Stream<Arguments> replacementsTestArguments() {