Add reset() and retain() methods to ComponentBuilder.

This method is simple: it resets the current part to default settings, keeping only the current text. It therefore acts like the old ChatColor.RESET
code. The retain method allows for more control over what is reset.

Add a test to verify proper functioning of reset()/retain().
This commit is contained in:
Tux
2014-12-31 16:56:45 -05:00
committed by Thinkofdeath
parent d1e1ce4cdb
commit 88e71ead05
2 changed files with 121 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import org.junit.Assert;
@@ -80,6 +81,48 @@ public class ComponentsTest
+ "World" + ChatColor.YELLOW + ChatColor.BOLD + "!", BaseComponent.toLegacyText( components ) );
}
@Test
public void testBuilderReset()
{
BaseComponent[] components = new ComponentBuilder( "Hello " ).color(ChatColor.RED)
.append("World").reset().create();
Assert.assertEquals( components[0].getColor(), ChatColor.RED );
Assert.assertEquals( components[1].getColor(), ChatColor.WHITE );
}
@Test
public void testBuilderFormatRetention()
{
BaseComponent[] noneRetention = new ComponentBuilder( "Hello " ).color( ChatColor.RED )
.append( "World", ComponentBuilder.FormatRetention.NONE ).create();
Assert.assertEquals( noneRetention[0].getColor(), ChatColor.RED );
Assert.assertEquals( noneRetention[1].getColor(), ChatColor.WHITE );
HoverEvent testEvent = new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder( "test" ).create() );
BaseComponent[] formattingRetention = new ComponentBuilder( "Hello " ).color( ChatColor.RED )
.event( testEvent ).append( "World", ComponentBuilder.FormatRetention.FORMATTING ).create();
Assert.assertEquals( formattingRetention[0].getColor(), ChatColor.RED );
Assert.assertEquals( formattingRetention[0].getHoverEvent(), testEvent );
Assert.assertEquals( formattingRetention[1].getColor(), ChatColor.RED );
Assert.assertNull( formattingRetention[1].getHoverEvent() );
ClickEvent testClickEvent = new ClickEvent( ClickEvent.Action.OPEN_URL, "http://www.example.com" );
BaseComponent[] eventRetention = new ComponentBuilder( "Hello " ).color( ChatColor.RED )
.event( testEvent ).event(testClickEvent).append("World", ComponentBuilder.FormatRetention.EVENTS).create();
Assert.assertEquals( eventRetention[0].getColor(), ChatColor.RED );
Assert.assertEquals( eventRetention[0].getHoverEvent(), testEvent );
Assert.assertEquals( eventRetention[0].getClickEvent(), testClickEvent );
Assert.assertEquals( eventRetention[1].getColor(), ChatColor.WHITE );
Assert.assertEquals( eventRetention[1].getHoverEvent(), testEvent );
Assert.assertEquals( eventRetention[1].getClickEvent(), testClickEvent );
}
@Test(expected = IllegalArgumentException.class)
public void testLoopSimple()
{