Skip to content

Commit

Permalink
Merge pull request #633 from KyoriPowered/fix/618
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 authored Dec 12, 2021
2 parents d087edc + 32147a2 commit 818e582
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private TextReplacementRenderer() {
}
if (children != null) {
children.add(replaced);
first = false;
}
}
} else {
Expand Down
24 changes: 21 additions & 3 deletions api/src/test/java/net/kyori/adventure/text/TextAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import net.kyori.examination.string.MultiLineStringExaminer;
import org.junit.jupiter.api.Assertions;

public final class TextAssertions {
private static final Set<TextDecoration> DECORATIONS = ImmutableSet.copyOf(TextDecoration.values());
Expand All @@ -51,8 +53,24 @@ public static void assertDecorations(final Style style, final Set<TextDecoration
private static void assertDecorationsAre(final Style style, final Set<TextDecoration> decorations, final TextDecoration.State state) {
if (!decorations.isEmpty()) {
for (final TextDecoration decoration : decorations) {
assertEquals(state, style.decoration(decoration));
Assertions.assertEquals(state, style.decoration(decoration));
}
}
}

public static void assertEquals(final Component expected, final Component actual) {
Assertions.assertEquals(prettyPrint(expected), prettyPrint(actual));
}

public static void assertEquals(final Component expected, final Component actual, final String message) {
Assertions.assertEquals(prettyPrint(expected), prettyPrint(actual), message);
}

public static void assertEquals(final Component expected, final Component actual, final Supplier<String> message) {
Assertions.assertEquals(prettyPrint(expected), prettyPrint(actual), message);
}

private static final String prettyPrint(final Component component) {
return component.examine(MultiLineStringExaminer.simpleEscaping()).collect(Collectors.joining("\n"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,25 @@ void testFullMatchReplaceWithStyle() {
final Component expected = Component.text("world", NamedTextColor.RED, TextDecoration.BOLD);
assertEquals(expected, replaced);
}

// https://github.com/KyoriPowered/adventure/issues/618
@Test
void testDoesNotDuplicateComponents() {
final Component original = Component.text()
.content("%n")
.append(Component.text("duplicate me"), Component.text("b%n"))
.build();

final Component replaced = original.replaceText(c -> c.matchLiteral("%n").replacement("*"));

final Component expected = Component.text()
.content("*")
.append(
Component.text("duplicate me"),
Component.text("b").append(Component.text("*"))
)
.build();

TextAssertions.assertEquals(expected, replaced);
}
}

0 comments on commit 818e582

Please sign in to comment.