Skip to content

Commit

Permalink
api: Test detection for legacy formatting warning
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Apr 8, 2021
1 parent 0006539 commit d92fcb2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
17 changes: 14 additions & 3 deletions api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
import net.kyori.examination.ExaminableProperty;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.VisibleForTesting;

import static java.util.Objects.requireNonNull;

final class TextComponentImpl extends AbstractComponent implements TextComponent {
private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = Boolean.getBoolean(String.join(".", "net", "kyori", "adventure", "text", "warnWhenLegacyFormattingDetected"));
private static final char SECTION_CHAR = '§';
@VisibleForTesting
static final char SECTION_CHAR = '§';

static final TextComponent EMPTY = createDirect("");
static final TextComponent NEWLINE = createDirect("\n");
Expand All @@ -54,12 +56,21 @@ final class TextComponentImpl extends AbstractComponent implements TextComponent
this.content = content;

if(WARN_WHEN_LEGACY_FORMATTING_DETECTED) {
if(content.indexOf(SECTION_CHAR) != -1) {
Nag.print(new LegacyFormattingDetected(this));
final LegacyFormattingDetected nag = this.warnWhenLegacyFormattingDetected();
if(nag != null) {
Nag.print(nag);
}
}
}

@VisibleForTesting
final @Nullable LegacyFormattingDetected warnWhenLegacyFormattingDetected() {
if(this.content.indexOf(SECTION_CHAR) != -1) {
return new LegacyFormattingDetected(this);
}
return null;
}

@Override
public @NonNull String content() {
return this.content;
Expand Down
18 changes: 18 additions & 0 deletions api/src/test/java/net/kyori/adventure/text/TextComponentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static net.kyori.adventure.text.TextAssertions.assertDecorations;
import static net.kyori.test.WeirdAssertions.assertAllEqualToEachOther;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -45,6 +46,23 @@ TextComponent.Builder builder() {
return Component.text().content("foo");
}

@Test
void testNoWarningThrownWhenNoLegacyFormattingDetected() {
final TextComponent component = Component.text("This is a test.");
assertNull(((TextComponentImpl) component).warnWhenLegacyFormattingDetected());
}

@Test
void testWarningThrownWhenLegacyFormattingDetected() {
final TextComponent component = Component.text(legacy('3') + "This is a test.");
assertNotNull(((TextComponentImpl) component).warnWhenLegacyFormattingDetected());
((TextComponentImpl) component).warnWhenLegacyFormattingDetected().printStackTrace();
}

private static String legacy(final char character) {
return TextComponentImpl.SECTION_CHAR + String.valueOf(character);
}

@Test
void testOfChildren() {
assertSame(Component.empty(), TextComponent.ofChildren()); // empty array
Expand Down

0 comments on commit d92fcb2

Please sign in to comment.