From 70bb3ddcceda76fb5c177af26bf3d027af62f3f7 Mon Sep 17 00:00:00 2001 From: Thinkofdeath Date: Sun, 8 Dec 2013 00:48:10 +0000 Subject: [PATCH] Added some unit tests --- .../net/md_5/bungee/chat/ComponentsTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 proxy/src/test/java/net/md_5/bungee/chat/ComponentsTest.java diff --git a/proxy/src/test/java/net/md_5/bungee/chat/ComponentsTest.java b/proxy/src/test/java/net/md_5/bungee/chat/ComponentsTest.java new file mode 100644 index 00000000..e6d7ec39 --- /dev/null +++ b/proxy/src/test/java/net/md_5/bungee/chat/ComponentsTest.java @@ -0,0 +1,60 @@ +package net.md_5.bungee.chat; + +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.TextComponent; +import net.md_5.bungee.api.chat.TranslatableComponent; +import org.junit.Assert; +import org.junit.Test; + +public class ComponentsTest +{ + @Test + public void testBasicComponent() { + TextComponent textComponent = new TextComponent( "Hello world" ); + textComponent.setColor( ChatColor.RED ); + + Assert.assertEquals( "Hello world" , textComponent.toPlainText() ); + Assert.assertEquals( ChatColor.RED + "Hello world" , textComponent.toLegacyText() ); + } + + @Test + public void testLegacyConverter() { + BaseComponent[] test1 = TextComponent.fromLegacyText( ChatColor.AQUA + "Aqua " + ChatColor.RED + ChatColor.BOLD + "RedBold" ); + + Assert.assertEquals( "Aqua RedBold" , BaseComponent.toPlainText( test1 ) ); + Assert.assertEquals( ChatColor.AQUA + "Aqua " + ChatColor.RED + ChatColor.BOLD + "RedBold" , BaseComponent.toLegacyText( test1 ) ); + + BaseComponent[] test2 = TextComponent.fromLegacyText( "Text http://spigotmc.org " + ChatColor.GREEN + "google.com/test" ); + + Assert.assertEquals( "Text http://spigotmc.org google.com/test" , BaseComponent.toPlainText( test2 ) ); + //The extra ChatColor.WHITEs are sometimes inserted when not needed but it doesn't change the result + Assert.assertEquals( ChatColor.WHITE + "Text " + ChatColor.WHITE + "http://spigotmc.org" + ChatColor.WHITE + + " " + ChatColor.GREEN + "google.com/test" , BaseComponent.toLegacyText( test2 ) ); + + ClickEvent url1 = test2[1].getClickEvent(); + Assert.assertNotNull( url1 ); + Assert.assertTrue( url1.getAction() == ClickEvent.Action.OPEN_URL ); + Assert.assertEquals( "http://spigotmc.org" , url1.getValue() ); + + ClickEvent url2 = test2[3].getClickEvent(); + Assert.assertNotNull( url2 ); + Assert.assertTrue( url2.getAction() == ClickEvent.Action.OPEN_URL ); + Assert.assertEquals( "http://google.com/test" , url2.getValue() ); + } + + @Test + public void testTranslateComponent() { + TranslatableComponent item = new TranslatableComponent( "item.swordGold.name" ); + item.setColor( ChatColor.AQUA ); + TranslatableComponent translatableComponent = new TranslatableComponent( "commands.give.success", + item, "5", + "thinkofdeath"); + + Assert.assertEquals( "Given Golden Sword * 5 to thinkofdeath" , translatableComponent.toPlainText() ); + Assert.assertEquals( ChatColor.WHITE + "Given " + ChatColor.AQUA + "Golden Sword" + ChatColor.WHITE + + " * " + ChatColor.WHITE + "5" + ChatColor.WHITE + " to " + ChatColor.WHITE + "thinkofdeath" , + translatableComponent.toLegacyText() ); + } +}