mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2025-02-01 04:31:21 +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() {
|
boolean updateGlobalReplacements() {
|
||||||
if (!textWithoutReplacements.containsPlaceholders()) {
|
if (!textWithoutReplacements.containsUnreplacedPlaceholders()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,12 +40,22 @@ public final class StringWithPlaceholders {
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsPlaceholders() {
|
public boolean containsUnreplacedPlaceholders() {
|
||||||
return string != null && stringParts != null;
|
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) {
|
public boolean anyPlaceholderMatch(Predicate<PlaceholderOccurrence> filter) {
|
||||||
if (!containsPlaceholders()) {
|
if (stringParts == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +72,7 @@ public final class StringWithPlaceholders {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean anyLiteralPartMatch(Predicate<String> filter) {
|
public boolean anyLiteralPartMatch(Predicate<String> filter) {
|
||||||
if (!containsPlaceholders()) {
|
if (stringParts == null) {
|
||||||
return filter.test(string);
|
return filter.test(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +88,7 @@ public final class StringWithPlaceholders {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull StringWithPlaceholders partiallyReplacePlaceholders(PlaceholderReplaceFunction replaceFunction) {
|
public @NotNull StringWithPlaceholders partiallyReplacePlaceholders(PlaceholderReplaceFunction replaceFunction) {
|
||||||
if (!containsPlaceholders()) {
|
if (stringParts == null) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +120,7 @@ public final class StringWithPlaceholders {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String replaceParts(PlaceholderReplaceFunction placeholderReplaceFunction, UnaryOperator<String> literalPartReplaceFunction) {
|
public String replaceParts(PlaceholderReplaceFunction placeholderReplaceFunction, UnaryOperator<String> literalPartReplaceFunction) {
|
||||||
if (!containsPlaceholders()) {
|
if (string == null || stringParts == null) {
|
||||||
return literalPartReplaceFunction.apply(string);
|
return literalPartReplaceFunction.apply(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,18 +25,17 @@ class StringWithPlaceholdersTest {
|
|||||||
StringWithPlaceholders s = StringWithPlaceholders.of(input);
|
StringWithPlaceholders s = StringWithPlaceholders.of(input);
|
||||||
|
|
||||||
assertThat(s.replacePlaceholders(occurrence -> "#")).isEqualTo(expectedOutput);
|
assertThat(s.replacePlaceholders(occurrence -> "#")).isEqualTo(expectedOutput);
|
||||||
assertThat(s.containsPlaceholders()).isEqualTo(expectedContainsPlaceholders);
|
assertThat(s.containsUnreplacedPlaceholders()).isEqualTo(expectedContainsPlaceholders);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest(name = "[{index}] {0} -> {1}")
|
@ParameterizedTest(name = "[{index}] {0} -> {1}")
|
||||||
@MethodSource("replacementsTestArguments")
|
@MethodSource("replacementsTestArguments")
|
||||||
void partialReplacements(String input, String expectedOutput) {
|
void partialReplacements(String input, String expectedOutput) {
|
||||||
boolean expectedContainsPlaceholders = expectedOutput.contains("#");
|
|
||||||
StringWithPlaceholders s = StringWithPlaceholders.of(input);
|
StringWithPlaceholders s = StringWithPlaceholders.of(input);
|
||||||
|
|
||||||
assertThat(s.partiallyReplacePlaceholders(occurrence -> "#").getString()).isEqualTo(expectedOutput);
|
assertThat(s.partiallyReplacePlaceholders(occurrence -> "#").getString()).isEqualTo(expectedOutput);
|
||||||
assertThat(s.partiallyReplacePlaceholders(occurrence -> null).replacePlaceholders(occurrence -> "#")).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() {
|
static Stream<Arguments> replacementsTestArguments() {
|
||||||
|
Loading…
Reference in New Issue
Block a user