From 82684c7b6b9d38836e7526c6cabead50689ccaf9 Mon Sep 17 00:00:00 2001 From: Janmm14 Date: Sun, 31 Mar 2024 09:38:18 +1100 Subject: [PATCH] #3634: Improve chat test code style. Stop use of subclass for static method call. Make test helper methods static. --- .../md_5/bungee/api/chat/ComponentsTest.java | 62 +++++++++---------- .../api/chat/TranslatableComponentTest.java | 4 +- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java index 0c36662f0..8b41182ab 100644 --- a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java +++ b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java @@ -20,14 +20,14 @@ public class ComponentsTest { 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) @@ -177,7 +177,7 @@ public class ComponentsTest 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 @@ -337,16 +337,16 @@ public class ComponentsTest @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 hoverEventSupplier) + private static void testFormatRetentionCopyFormatting(Supplier hoverEventSupplier) { TextComponent first = new TextComponent( "Hello" ); first.setBold( true ); @@ -365,16 +365,16 @@ public class ComponentsTest @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 legacyTextFunction) + private static void testBuilderClone(Function legacyTextFunction) { ComponentBuilder builder = new ComponentBuilder( "Hello " ).color( ChatColor.RED ).append( "world" ).color( ChatColor.DARK_RED ); ComponentBuilder cloned = new ComponentBuilder( builder ); @@ -385,7 +385,7 @@ public class ComponentsTest @Test public void testBuilderAppendCreateMixedComponents() { - this.testBuilderAppendMixedComponents( + testBuilderAppendMixedComponents( ComponentBuilder::create, (components, index) -> components[index] ); @@ -394,13 +394,13 @@ public class ComponentsTest @Test public void testBuilderAppendBuildMixedComponents() { - this.testBuilderAppendMixedComponents( + testBuilderAppendMixedComponents( ComponentBuilder::build, (component, index) -> component.getExtra().get( index ) ); } - private void testBuilderAppendMixedComponents(Function componentBuilder, BiFunction extraGetter) + private static void testBuilderAppendMixedComponents(Function componentBuilder, BiFunction extraGetter) { ComponentBuilder builder = new ComponentBuilder( "Hello " ); TextComponent textComponent = new TextComponent( "world " ); @@ -443,7 +443,7 @@ public class ComponentsTest @Test public void testBuilderAppendCreate() { - this.testBuilderAppend( + testBuilderAppend( () -> new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder( "Hello world" ).create() ), ComponentBuilder::create, (components, index) -> components[index], @@ -456,7 +456,7 @@ public class ComponentsTest @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 ), @@ -467,7 +467,7 @@ public class ComponentsTest ); } - private void testBuilderAppend(Supplier hoverEventSupplier, Function componentBuilder, BiFunction extraGetter, Function toPlainTextFunction, String expectedLegacyText, Function toLegacyTextFunction) + private static void testBuilderAppend(Supplier hoverEventSupplier, Function componentBuilder, BiFunction extraGetter, Function toPlainTextFunction, String expectedLegacyText, Function toLegacyTextFunction) { ClickEvent clickEvent = new ClickEvent( ClickEvent.Action.RUN_COMMAND, "/help " ); HoverEvent hoverEvent = hoverEventSupplier.get(); @@ -486,7 +486,7 @@ public class ComponentsTest @Test public void testBuilderAppendLegacyCreate() { - this.testBuilderAppendLegacy( + testBuilderAppendLegacy( ComponentBuilder::create, BaseComponent::toPlainText, ChatColor.YELLOW + "Hello " + ChatColor.GREEN + "world!", @@ -497,7 +497,7 @@ public class ComponentsTest @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 @@ -506,7 +506,7 @@ public class ComponentsTest ); } - private void testBuilderAppendLegacy(Function componentBuilder, Function toPlainTextFunction, String expectedLegacyString, Function toLegacyTextFunction) + private static void testBuilderAppendLegacy(Function componentBuilder, Function toPlainTextFunction, String expectedLegacyString, Function toLegacyTextFunction) { ComponentBuilder builder = new ComponentBuilder( "Hello " ).color( ChatColor.YELLOW ); builder.appendLegacy( "§aworld!" ); @@ -579,7 +579,7 @@ public class ComponentsTest @Test public void testBuilderCreate() { - this.testBuilder( + testBuilder( ComponentBuilder::create, BaseComponent::toPlainText, ChatColor.RED + "Hello " + ChatColor.BLUE + ChatColor.BOLD + "World" + ChatColor.YELLOW + ChatColor.BOLD + "!", @@ -590,7 +590,7 @@ public class ComponentsTest @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 @@ -599,7 +599,7 @@ public class ComponentsTest ); } - private void testBuilder(Function componentBuilder, Function toPlainTextFunction, String expectedLegacyString, Function toLegacyTextFunction) + private static void testBuilder(Function componentBuilder, Function toPlainTextFunction, String expectedLegacyString, Function toLegacyTextFunction) { T component = componentBuilder.apply( new ComponentBuilder( "Hello " ).color( ChatColor.RED ). append( "World" ).bold( true ).color( ChatColor.BLUE ). @@ -612,7 +612,7 @@ public class ComponentsTest @Test public void testBuilderCreateReset() { - this.testBuilderReset( + testBuilderReset( ComponentBuilder::create, (components, index) -> components[index] ); @@ -621,13 +621,13 @@ public class ComponentsTest @Test public void testBuilderBuildReset() { - this.testBuilderReset( + testBuilderReset( ComponentBuilder::build, (component, index) -> component.getExtra().get( index ) ); } - private void testBuilderReset(Function componentBuilder, BiFunction extraGetter) + private static void testBuilderReset(Function componentBuilder, BiFunction extraGetter) { T component = componentBuilder.apply( new ComponentBuilder( "Hello " ).color( ChatColor.RED ) .append( "World" ).reset() ); @@ -639,7 +639,7 @@ public class ComponentsTest @Test public void testBuilderCreateFormatRetention() { - this.testBuilderFormatRetention( + testBuilderFormatRetention( ComponentBuilder::create, (components, index) -> components[index] ); @@ -648,13 +648,13 @@ public class ComponentsTest @Test public void testBuilderBuildFormatRetention() { - this.testBuilderFormatRetention( + testBuilderFormatRetention( ComponentBuilder::build, (component, index) -> component.getExtra().get( index ) ); } - private void testBuilderFormatRetention(Function componentBuilder, BiFunction extraGetter) + private static void testBuilderFormatRetention(Function componentBuilder, BiFunction extraGetter) { T noneRetention = componentBuilder.apply( new ComponentBuilder( "Hello " ).color( ChatColor.RED ) .append( "World", ComponentBuilder.FormatRetention.NONE ) ); @@ -800,7 +800,7 @@ public class ComponentsTest 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 ); @@ -810,7 +810,7 @@ public class ComponentsTest @Test public void testLegacyResetInBuilderCreate() { - this.testLegacyResetInBuilder( + testLegacyResetInBuilder( ComponentBuilder::create, ComponentSerializer::toString ); @@ -819,7 +819,7 @@ public class ComponentsTest @Test public void testLegacyResetInBuilderBuild() { - this.testLegacyResetInBuilder( + testLegacyResetInBuilder( ComponentBuilder::build, ComponentSerializer::toString ); @@ -851,7 +851,7 @@ public class ComponentsTest * In legacy chat, colors and reset both reset all formatting. * Make sure it works in combination with ComponentBuilder. */ - private void testLegacyResetInBuilder(Function componentBuilder, Function componentSerializer) + private static void testLegacyResetInBuilder(Function componentBuilder, Function componentSerializer) { ComponentBuilder builder = new ComponentBuilder(); BaseComponent[] a = TextComponent.fromLegacyText( "§4§n44444§rdd§6§l6666" ); diff --git a/chat/src/test/java/net/md_5/bungee/api/chat/TranslatableComponentTest.java b/chat/src/test/java/net/md_5/bungee/api/chat/TranslatableComponentTest.java index 9ec5dcb1a..47c06baaf 100644 --- a/chat/src/test/java/net/md_5/bungee/api/chat/TranslatableComponentTest.java +++ b/chat/src/test/java/net/md_5/bungee/api/chat/TranslatableComponentTest.java @@ -22,7 +22,7 @@ public class TranslatableComponentTest 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 ) ); } }