mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2025-01-20 06:41:20 +01:00
Fix StringWithPlaceholders.containsPlaceholders() and update test
This commit is contained in:
parent
2d99a0c36a
commit
3432a33ae6
@ -60,7 +60,7 @@ class DisplayText {
|
||||
}
|
||||
|
||||
boolean updateGlobalReplacements() {
|
||||
if (!textWithoutReplacements.containsPlaceholders()) {
|
||||
if (!textWithoutReplacements.containsUnreplacedPlaceholders()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user