Skip to content

Commit

Permalink
wip: test: add tests for legacy parser
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia committed Aug 28, 2024
1 parent 9e2f59c commit e0a101c
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.kyori.adventure.text.serializer.legacy.CharacterAndFormat;
import net.kyori.adventure.text.serializer.legacy.Reset;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
Expand All @@ -39,6 +40,7 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.rexcantor64.triton.language.TranslationManager.JSON_TYPE_TAG;
Expand Down Expand Up @@ -66,6 +68,10 @@ public class LegacyParser implements MessageParser {
private static final char HEX_PREFIX = '#';
private static final char HEX_CODE = 'x';
private static final String VALID_COLOR_CODES = "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx";
private static final Pattern FORMATTING_STRIP_PATTERN = Pattern.compile(
CLICK_DELIM + "\\d[\\w-]{36}|" + HOVER_DELIM + "[\\w-]{36}|" + CLICK_END_DELIM + "|" + HOVER_END_DELIM
+ "|" + SECTION_CHAR + "[0-9A-Fa-fK-Ok-oRrXx]"
);

/**
* @see MessageParser#translateString(String, Localized, FeatureSyntax)
Expand Down Expand Up @@ -194,6 +200,7 @@ TranslationResult<SerializedComponent> translateComponent(
key = key.substring(0, indexes.get(0)[0]);
}
key = ParserUtils.normalizeTranslationKey(key, configuration);
key = stripFormatting(key);

val result = configuration.translationSupplier.apply(key, arguments);

Expand All @@ -212,7 +219,9 @@ TranslationResult<SerializedComponent> translateComponent(
return text;
}

private @NotNull SerializedComponent replaceArguments(@NotNull SerializedComponent comp, @NotNull SerializedComponent @NotNull ... arguments) {
@VisibleForTesting
@NotNull
SerializedComponent replaceArguments(@NotNull SerializedComponent comp, @NotNull SerializedComponent @NotNull ... arguments) {
// Replace args in text
String[] args = Arrays.stream(arguments).map(SerializedComponent::getText).toArray(String[]::new);
comp.setText(replaceArguments(comp.getText(), args));
Expand Down Expand Up @@ -244,7 +253,12 @@ TranslationResult<SerializedComponent> translateComponent(
* @param text The text to convert the color code characters from.
* @return The input text with the color codes replaced.
*/
private String translateAlternateColorCodes(String text) {
@Contract("null -> null; !null -> !null")
private @Nullable String translateAlternateColorCodes(@Nullable String text) {
if (text == null) {
return null;
}

char[] chars = text.toCharArray();
for (int i = 0; i < chars.length - 1; i++) {
if (chars[i] == AMPERSAND_CHAR && VALID_COLOR_CODES.indexOf(chars[i + 1]) != -1) {
Expand All @@ -254,6 +268,23 @@ private String translateAlternateColorCodes(String text) {
return new String(chars);
}

/**
* Remove color code and click/hover event formatting in the text of {@link SerializedComponent}.
*
* @param text The text to remove the formatting from.
* @return The text without the associated formatting.
*/
@Contract("null -> null; !null -> !null")
@VisibleForTesting
@Nullable
String stripFormatting(@Nullable String text) {
if (text == null) {
return null;
}

return FORMATTING_STRIP_PATTERN.matcher(text).replaceAll("");
}

/**
* Represents a {@link Component} but as a String and with additional storage for the
* values of click/hover events and translatable components.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.rexcantor64.triton.language.parser;

import com.rexcantor64.triton.api.config.FeatureSyntax;
import com.rexcantor64.triton.language.parser.LegacyParser.SerializedComponent;
import com.rexcantor64.triton.utils.DefaultFeatureSyntax;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
Expand All @@ -11,10 +14,43 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class LegacyParserTest {
private final LegacyParser parser = new LegacyParser();
private final FeatureSyntax defaultSyntax = new DefaultFeatureSyntax();

private final Function<String, SerializedComponent> messageResolver = (key) -> {
switch (key) {
case "without.formatting":
return new SerializedComponent("This is text without formatting");
case "without.formatting.with.args":
return new SerializedComponent("This is text without formatting but with arguments (%1)");
case "with.colors":
return new SerializedComponent("§aThis text is green");
case "with.colors.two.args":
return new SerializedComponent("§dThis text is pink and has two arguments (%1 and %2)");
case "with.colors.repeated.args":
return new SerializedComponent("§dThis text is pink and has three arguments (%1 and %2 and %1)");
case "nested":
return new SerializedComponent("some text [lang]without.formatting[/lang]");
case "with.placeholder.colors":
return new SerializedComponent("§d%1 §ais a very cool guy");
case "change.colors.on.args":
return new SerializedComponent("§cSome text §9%1 more text");
}
return new SerializedComponent("unknown placeholder");
};

private final TranslationConfiguration<SerializedComponent> configuration = new TranslationConfiguration<>(
defaultSyntax,
"disabled.line",
(key, args) -> parser.replaceArguments(messageResolver.apply(key), args)
);

private final Component ALL_IN_ONE_COMPONENT = Component.text("Lorem ")
.append(
Component.text("ipsum dolor ")
Expand Down Expand Up @@ -51,7 +87,7 @@ public class LegacyParserTest {

@Test
public void testSerializingComponent() {
LegacyParser.SerializedComponent serializedComponent = new LegacyParser.SerializedComponent(ALL_IN_ONE_COMPONENT);
SerializedComponent serializedComponent = new SerializedComponent(ALL_IN_ONE_COMPONENT);

assertEquals(1, serializedComponent.getClickEvents().size());
assertEquals(1, serializedComponent.getHoverEvents().size());
Expand All @@ -70,7 +106,7 @@ public void testSerializingComponent() {

@Test
public void testSerializeDeserializeComponent() {
Component result = new LegacyParser.SerializedComponent(ALL_IN_ONE_COMPONENT).toComponent();
Component result = new SerializedComponent(ALL_IN_ONE_COMPONENT).toComponent();

// slightly modify input to equivalent component, due to behaviour of the deserializer
List<Component> expectedChildren = new ArrayList<>(ALL_IN_ONE_COMPONENT.children());
Expand All @@ -89,4 +125,110 @@ public void testSerializeDeserializeComponent() {

assertEquals(expected.compact(), result.compact());
}

@Test
public void testStripFormatting() {
Component component = Component.text()
.content("abc")
.append(
Component.text()
.content("def")
.color(NamedTextColor.AQUA)
)
.append(Component.text("ghi"))
.color(NamedTextColor.GOLD)
.clickEvent(ClickEvent.copyToClipboard("Lorem Ipsum"))
.hoverEvent(HoverEvent.showText(Component.text("Lorem Ipsum")))
.build();

SerializedComponent serializedComponent = new SerializedComponent(component);

String result = parser.stripFormatting(serializedComponent.getText());

assertEquals("abcdefghi", result);
}

@Test
public void testTranslateComponentWithoutPlaceholders() {
SerializedComponent comp = new SerializedComponent("Text without any placeholders whatsoever");

TranslationResult<SerializedComponent> result = parser.translateComponent(comp, configuration);

assertEquals(TranslationResult.ResultState.UNCHANGED, result.getState());
}

@Test
public void testParseComponentWithoutFormatting() {
SerializedComponent comp = new SerializedComponent("Text [lang]without.formatting[/lang] more text");

TranslationResult<Component> result = parser.translateComponent(comp, configuration)
.map(SerializedComponent::toComponent);

Component expected = Component.text("Text This is text without formatting more text");

assertEquals(TranslationResult.ResultState.CHANGED, result.getState());
assertNotNull(result.getResultRaw());
assertEquals(expected.compact(), result.getResultRaw().compact());
}

@Test
public void testParseComponentStripFormattingFromKey() {
SerializedComponent comp = new SerializedComponent("Text [lang]with§bout.formatting[/lang] more text");

TranslationResult<Component> result = parser.translateComponent(comp, configuration)
.map(SerializedComponent::toComponent);

Component expected = Component.text("Text This is text without formatting more text");

assertEquals(TranslationResult.ResultState.CHANGED, result.getState());
assertNotNull(result.getResultRaw());
assertEquals(expected.compact(), result.getResultRaw().compact());
}

@Test
public void testParseComponentStyleSpillFromPlaceholder() {
SerializedComponent comp = new SerializedComponent("Text [lang]with.colors[/lang] lorem ipsum [lang]without.formatting[/lang] more text");

TranslationResult<Component> result = parser.translateComponent(comp, configuration)
.map(SerializedComponent::toComponent);

Component expected = Component.text()
.content("Text ")
.append(
Component.text()
.content("This text is green lorem ipsum This is text without formatting more text")
.color(NamedTextColor.GREEN)
)
.build();

assertEquals(TranslationResult.ResultState.CHANGED, result.getState());
assertNotNull(result.getResultRaw());
assertEquals(expected.compact(), result.getResultRaw().compact());
}

@Test
public void testParseComponentStyleSpillFromArgument() {
SerializedComponent comp = new SerializedComponent("Text §4[lang]without.formatting.with.args[args][arg]§agreen text[/arg][/args][/lang] more text");

TranslationResult<Component> result = parser.translateComponent(comp, configuration)
.map(SerializedComponent::toComponent);

Component expected = Component.text()
.content("Text ")
.append(
Component.text()
.content("This is text without formatting but with arguments (")
.color(NamedTextColor.DARK_RED)
)
.append(
Component.text()
.content("green text) more text")
.color(NamedTextColor.GREEN)
)
.build();

assertEquals(TranslationResult.ResultState.CHANGED, result.getState());
assertNotNull(result.getResultRaw());
assertEquals(expected.compact(), result.getResultRaw().compact());
}
}

0 comments on commit e0a101c

Please sign in to comment.