Skip to content

Commit

Permalink
#3634: Improve chat test code style.
Browse files Browse the repository at this point in the history
Stop use of subclass for static method call.
Make test helper methods static.
  • Loading branch information
Janmm14 authored and md-5 committed Mar 30, 2024
1 parent c2f73d3 commit 82684c7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
62 changes: 31 additions & 31 deletions chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public static void testDissembleReassemble(BaseComponent[] components)
{
String json = ComponentSerializer.toString( components );
BaseComponent[] parsed = ComponentSerializer.parse( json );
assertEquals( TextComponent.toLegacyText( parsed ), TextComponent.toLegacyText( components ) );
assertEquals( BaseComponent.toLegacyText( parsed ), BaseComponent.toLegacyText( components ) );
}

public static void testDissembleReassemble(BaseComponent component)
{
String json = ComponentSerializer.toString( component );
BaseComponent[] parsed = ComponentSerializer.parse( json );
assertEquals( TextComponent.toLegacyText( parsed ), TextComponent.toLegacyText( component ) );
assertEquals( BaseComponent.toLegacyText( parsed ), BaseComponent.toLegacyText( component ) );
}

public static void testAssembleDissemble(String json, boolean modern)
Expand Down Expand Up @@ -177,7 +177,7 @@ public void testComponentParting()
public void testToLegacyFromLegacy()
{
String text = "§a§lHello §f§kworld§7!";
assertEquals( text, TextComponent.toLegacyText( TextComponent.fromLegacyText( text ) ) );
assertEquals( text, BaseComponent.toLegacyText( TextComponent.fromLegacyText( text ) ) );
}

@Test
Expand Down Expand Up @@ -337,16 +337,16 @@ private <T> void testHoverEventContents(HoverEvent hoverEvent, Function<String,
@Test
public void testFormatRetentionCopyFormattingCreate()
{
this.testFormatRetentionCopyFormatting( () -> new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder( "Test" ).create() ) );
ComponentsTest.testFormatRetentionCopyFormatting( () -> new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder( "Test" ).create() ) );
}

@Test
public void testFormatRetentionCopyFormattingBuild()
{
this.testFormatRetentionCopyFormatting( () -> new HoverEvent( HoverEvent.Action.SHOW_TEXT, new Text( new ComponentBuilder( "Test" ).build() ) ) );
testFormatRetentionCopyFormatting( () -> new HoverEvent( HoverEvent.Action.SHOW_TEXT, new Text( new ComponentBuilder( "Test" ).build() ) ) );
}

private void testFormatRetentionCopyFormatting(Supplier<HoverEvent> hoverEventSupplier)
private static void testFormatRetentionCopyFormatting(Supplier<HoverEvent> hoverEventSupplier)
{
TextComponent first = new TextComponent( "Hello" );
first.setBold( true );
Expand All @@ -365,16 +365,16 @@ private void testFormatRetentionCopyFormatting(Supplier<HoverEvent> hoverEventSu
@Test
public void testBuilderCloneCreate()
{
this.testBuilderClone( (builder) -> TextComponent.toLegacyText( builder.create() ) );
testBuilderClone( (builder) -> BaseComponent.toLegacyText( builder.create() ) );
}

@Test
public void testBuilderCloneBuild()
{
this.testBuilderClone( (builder) -> TextComponent.toLegacyText( builder.build() ) );
testBuilderClone( (builder) -> BaseComponent.toLegacyText( builder.build() ) );
}

private void testBuilderClone(Function<ComponentBuilder, String> legacyTextFunction)
private static void testBuilderClone(Function<ComponentBuilder, String> legacyTextFunction)
{
ComponentBuilder builder = new ComponentBuilder( "Hello " ).color( ChatColor.RED ).append( "world" ).color( ChatColor.DARK_RED );
ComponentBuilder cloned = new ComponentBuilder( builder );
Expand All @@ -385,7 +385,7 @@ private void testBuilderClone(Function<ComponentBuilder, String> legacyTextFunct
@Test
public void testBuilderAppendCreateMixedComponents()
{
this.testBuilderAppendMixedComponents(
testBuilderAppendMixedComponents(
ComponentBuilder::create,
(components, index) -> components[index]
);
Expand All @@ -394,13 +394,13 @@ public void testBuilderAppendCreateMixedComponents()
@Test
public void testBuilderAppendBuildMixedComponents()
{
this.testBuilderAppendMixedComponents(
testBuilderAppendMixedComponents(
ComponentBuilder::build,
(component, index) -> component.getExtra().get( index )
);
}

private <T> void testBuilderAppendMixedComponents(Function<ComponentBuilder, T> componentBuilder, BiFunction<T, Integer, BaseComponent> extraGetter)
private static <T> void testBuilderAppendMixedComponents(Function<ComponentBuilder, T> componentBuilder, BiFunction<T, Integer, BaseComponent> extraGetter)
{
ComponentBuilder builder = new ComponentBuilder( "Hello " );
TextComponent textComponent = new TextComponent( "world " );
Expand Down Expand Up @@ -443,7 +443,7 @@ public void testStyle()
@Test
public void testBuilderAppendCreate()
{
this.testBuilderAppend(
testBuilderAppend(
() -> new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder( "Hello world" ).create() ),
ComponentBuilder::create,
(components, index) -> components[index],
Expand All @@ -456,7 +456,7 @@ public void testBuilderAppendCreate()
@Test
public void testBuilderAppendBuild()
{
this.testBuilderAppend(
testBuilderAppend(
() -> new HoverEvent( HoverEvent.Action.SHOW_TEXT, new Text( new ComponentBuilder( "Hello world" ).build() ) ),
ComponentBuilder::build,
(component, index) -> component.getExtra().get( index ),
Expand All @@ -467,7 +467,7 @@ public void testBuilderAppendBuild()
);
}

private <T> void testBuilderAppend(Supplier<HoverEvent> hoverEventSupplier, Function<ComponentBuilder, T> componentBuilder, BiFunction<T, Integer, BaseComponent> extraGetter, Function<T, String> toPlainTextFunction, String expectedLegacyText, Function<T, String> toLegacyTextFunction)
private static <T> void testBuilderAppend(Supplier<HoverEvent> hoverEventSupplier, Function<ComponentBuilder, T> componentBuilder, BiFunction<T, Integer, BaseComponent> extraGetter, Function<T, String> toPlainTextFunction, String expectedLegacyText, Function<T, String> toLegacyTextFunction)
{
ClickEvent clickEvent = new ClickEvent( ClickEvent.Action.RUN_COMMAND, "/help " );
HoverEvent hoverEvent = hoverEventSupplier.get();
Expand All @@ -486,7 +486,7 @@ private <T> void testBuilderAppend(Supplier<HoverEvent> hoverEventSupplier, Func
@Test
public void testBuilderAppendLegacyCreate()
{
this.testBuilderAppendLegacy(
testBuilderAppendLegacy(
ComponentBuilder::create,
BaseComponent::toPlainText,
ChatColor.YELLOW + "Hello " + ChatColor.GREEN + "world!",
Expand All @@ -497,7 +497,7 @@ public void testBuilderAppendLegacyCreate()
@Test
public void testBuilderAppendLegacyBuild()
{
this.testBuilderAppendLegacy(
testBuilderAppendLegacy(
ComponentBuilder::build,
(component) -> BaseComponent.toPlainText( component ),
// An extra format code is appended to the beginning because there is an empty TextComponent at the start of every component
Expand All @@ -506,7 +506,7 @@ public void testBuilderAppendLegacyBuild()
);
}

private <T> void testBuilderAppendLegacy(Function<ComponentBuilder, T> componentBuilder, Function<T, String> toPlainTextFunction, String expectedLegacyString, Function<T, String> toLegacyTextFunction)
private static <T> void testBuilderAppendLegacy(Function<ComponentBuilder, T> componentBuilder, Function<T, String> toPlainTextFunction, String expectedLegacyString, Function<T, String> toLegacyTextFunction)
{
ComponentBuilder builder = new ComponentBuilder( "Hello " ).color( ChatColor.YELLOW );
builder.appendLegacy( "§aworld!" );
Expand Down Expand Up @@ -579,7 +579,7 @@ public void testTranslateComponent()
@Test
public void testBuilderCreate()
{
this.testBuilder(
testBuilder(
ComponentBuilder::create,
BaseComponent::toPlainText,
ChatColor.RED + "Hello " + ChatColor.BLUE + ChatColor.BOLD + "World" + ChatColor.YELLOW + ChatColor.BOLD + "!",
Expand All @@ -590,7 +590,7 @@ public void testBuilderCreate()
@Test
public void testBuilderBuild()
{
this.testBuilder(
testBuilder(
ComponentBuilder::build,
(component) -> BaseComponent.toPlainText( component ),
// An extra format code is appended to the beginning because there is an empty TextComponent at the start of every component
Expand All @@ -599,7 +599,7 @@ public void testBuilderBuild()
);
}

private <T> void testBuilder(Function<ComponentBuilder, T> componentBuilder, Function<T, String> toPlainTextFunction, String expectedLegacyString, Function<T, String> toLegacyTextFunction)
private static <T> void testBuilder(Function<ComponentBuilder, T> componentBuilder, Function<T, String> toPlainTextFunction, String expectedLegacyString, Function<T, String> toLegacyTextFunction)
{
T component = componentBuilder.apply( new ComponentBuilder( "Hello " ).color( ChatColor.RED ).
append( "World" ).bold( true ).color( ChatColor.BLUE ).
Expand All @@ -612,7 +612,7 @@ private <T> void testBuilder(Function<ComponentBuilder, T> componentBuilder, Fun
@Test
public void testBuilderCreateReset()
{
this.testBuilderReset(
testBuilderReset(
ComponentBuilder::create,
(components, index) -> components[index]
);
Expand All @@ -621,13 +621,13 @@ public void testBuilderCreateReset()
@Test
public void testBuilderBuildReset()
{
this.testBuilderReset(
testBuilderReset(
ComponentBuilder::build,
(component, index) -> component.getExtra().get( index )
);
}

private <T> void testBuilderReset(Function<ComponentBuilder, T> componentBuilder, BiFunction<T, Integer, BaseComponent> extraGetter)
private static <T> void testBuilderReset(Function<ComponentBuilder, T> componentBuilder, BiFunction<T, Integer, BaseComponent> extraGetter)
{
T component = componentBuilder.apply( new ComponentBuilder( "Hello " ).color( ChatColor.RED )
.append( "World" ).reset() );
Expand All @@ -639,7 +639,7 @@ private <T> void testBuilderReset(Function<ComponentBuilder, T> componentBuilder
@Test
public void testBuilderCreateFormatRetention()
{
this.testBuilderFormatRetention(
testBuilderFormatRetention(
ComponentBuilder::create,
(components, index) -> components[index]
);
Expand All @@ -648,13 +648,13 @@ public void testBuilderCreateFormatRetention()
@Test
public void testBuilderBuildFormatRetention()
{
this.testBuilderFormatRetention(
testBuilderFormatRetention(
ComponentBuilder::build,
(component, index) -> component.getExtra().get( index )
);
}

private <T> void testBuilderFormatRetention(Function<ComponentBuilder, T> componentBuilder, BiFunction<T, Integer, BaseComponent> extraGetter)
private static <T> void testBuilderFormatRetention(Function<ComponentBuilder, T> componentBuilder, BiFunction<T, Integer, BaseComponent> extraGetter)
{
T noneRetention = componentBuilder.apply( new ComponentBuilder( "Hello " ).color( ChatColor.RED )
.append( "World", ComponentBuilder.FormatRetention.NONE ) );
Expand Down Expand Up @@ -800,7 +800,7 @@ public void testNotEquals()
public void testLegacyHack()
{
BaseComponent[] hexColored = new ComponentBuilder().color( ChatColor.of( Color.GRAY ) ).append( "Test" ).create();
String legacy = TextComponent.toLegacyText( hexColored );
String legacy = BaseComponent.toLegacyText( hexColored );

BaseComponent[] reColored = TextComponent.fromLegacyText( legacy );

Expand All @@ -810,7 +810,7 @@ public void testLegacyHack()
@Test
public void testLegacyResetInBuilderCreate()
{
this.testLegacyResetInBuilder(
testLegacyResetInBuilder(
ComponentBuilder::create,
ComponentSerializer::toString
);
Expand All @@ -819,7 +819,7 @@ public void testLegacyResetInBuilderCreate()
@Test
public void testLegacyResetInBuilderBuild()
{
this.testLegacyResetInBuilder(
testLegacyResetInBuilder(
ComponentBuilder::build,
ComponentSerializer::toString
);
Expand Down Expand Up @@ -851,7 +851,7 @@ public void testStyleIsEmpty()
* In legacy chat, colors and reset both reset all formatting.
* Make sure it works in combination with ComponentBuilder.
*/
private <T> void testLegacyResetInBuilder(Function<ComponentBuilder, T> componentBuilder, Function<T, String> componentSerializer)
private static <T> void testLegacyResetInBuilder(Function<ComponentBuilder, T> componentBuilder, Function<T, String> componentSerializer)
{
ComponentBuilder builder = new ComponentBuilder();
BaseComponent[] a = TextComponent.fromLegacyText( "§4§n44444§rdd§6§l6666" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void testJsonSerialisation()
String jsonString = ComponentSerializer.toString( testComponent );
BaseComponent[] baseComponents = ComponentSerializer.parse( jsonString );

assertEquals( "Test string with a placeholder", TextComponent.toPlainText( baseComponents ) );
assertEquals( "§fTest string with §fa§f placeholder", TextComponent.toLegacyText( baseComponents ) );
assertEquals( "Test string with a placeholder", BaseComponent.toPlainText( baseComponents ) );
assertEquals( "§fTest string with §fa§f placeholder", BaseComponent.toLegacyText( baseComponents ) );
}
}

0 comments on commit 82684c7

Please sign in to comment.