From 9c6bb882b0b97d09750973edf4b93c2892ccb501 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 5 Jun 2015 09:29:42 +0200 Subject: [PATCH] Better test layout and fix a bug. --- .../massivecraft/massivecore/mson/ATest.java | 108 -------------- .../massivecraft/massivecore/mson/Mson.java | 2 +- .../massivecraft/massivecore/mson/Test.java | 139 ++++++++++++++++++ 3 files changed, 140 insertions(+), 109 deletions(-) delete mode 100644 src/com/massivecraft/massivecore/mson/ATest.java create mode 100644 src/com/massivecraft/massivecore/mson/Test.java diff --git a/src/com/massivecraft/massivecore/mson/ATest.java b/src/com/massivecraft/massivecore/mson/ATest.java deleted file mode 100644 index 6307a3a8..00000000 --- a/src/com/massivecraft/massivecore/mson/ATest.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.massivecraft.massivecore.mson; - -import static com.massivecraft.massivecore.mson.Mson.mson; - -import java.util.List; - -import org.bukkit.ChatColor; - -import com.massivecraft.massivecore.collections.MassiveList; -import com.massivecraft.massivecore.util.Txt; - -public class ATest -{ - public static void main(String[] args) - { - try - { - // Test getMsonFrom - Mson ofMson = mson(new Mson().text("hello")); - System.out.println("ofMson:" + ofMson); // Success - - Mson ofString = mson("hello"); - System.out.println("ofString:" + ofString); // Success - - Mson ofCollection = mson(new MassiveList("hello ", "you!")); - System.out.println("ofCollection:" + ofCollection); // Success - - // Test children - Mson child = mson("test").color(ChatColor.BLUE) - .addChild(" test2").link("www.massivecraft.com") - .addChild(" test3 ") - .addChildren("this ", "is only ", "one way to do it!") - .root() - .tooltip("Holy moly!"); - System.out.println("child:" + child.root()); // Success - - // Test siblings - Mson sibling = mson( - "test", - " test2", - " test3", - new Mson().text(" Test4, children: ").addChild("Child1 ") - .addSiblings("Sibling 1 ", "Sibling 2 ", "Sibling 3 ") - .addSibling("Sibling 4").root() - ).tooltip("Holy moly!"); - System.out.println("sibling:" + sibling.root()); // Success - - // Test fromParsedMessage - Mson parsed = Mson.fromParsedMessage(Txt.parse("white info bad green")); - System.out.println("parsed:" + parsed); // Success - - Mson parsed2 = Mson.parse("white info bad green"); - System.out.println("parsed2:" + parsed2.toRaw()); // Success - - Mson parseFormat = Mson.parse("insert here %s whatever you %s could wish for!", "!1337!", "herpi derp"); - System.out.println("parseFormat:" + parseFormat); // Success - - // Test format - Mson format = Mson.format("Just a %s simple string! :)", "very"); - System.out.println("format:" + format); // Success - - // Test split - List split = format.split(Txt.REGEX_WHITESPACE.toString()); - System.out.println("split:" + mson(split)); // Success - - /* - * Test replace - */ - - Mson charr = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replace('1', '0'); - System.out.println("char:" + charr); // Success - - Mson sequence = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replace("1", "0"); - System.out.println("sequence:" + sequence); // Success - - Mson regex = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replaceAll("1", "0"); - System.out.println("regex:" + regex); // Success - - //Mson replaceFirst = Mson.of("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").getRoot().replaceFirst("1", "0"); - //System.out.println("replaceFirst:" + replaceFirst.toRaw()); // Success - - /* - * Test special replaceAll - */ - - // replace string mson - Mson replaceAll1 = mson("1 2 3 4 5 6 1 7 tests").color(ChatColor.BLUE).addChild(" 1+1+1").addChild("herpiderp").root().replaceAll("1", mson("0")); - System.out.println("replaceAll1:" + replaceAll1.root()); // Success - - Mson overload = mson("hello").addChild("hello").addChild("hello").root().replaceAll("hello", mson("lol")); - System.out.println("overload:" + overload.root()); // Success - - Mson toReplace = new Mson().text("hallo"); - - // replace mson mson - Mson replaceAll2 = mson("1 2 3 4 5 6 7 tests").addChild(toReplace).addSibling(" miep").root().replaceAll(toReplace, mson("tests")); - System.out.println("replaceAll2:" + replaceAll2.root()); // Success - - Mson overload2 = mson("1 2 3 4 5 6 7 tests").addChild("hallo").addChild("hallo").addChild("hallo").addSibling(" miep").root().replaceAll(mson("hallo"), mson("tests")); - System.out.println("overload2:" + overload2.root()); // Success - } - catch(Throwable t) - { - t.printStackTrace(); - } - } - -} diff --git a/src/com/massivecraft/massivecore/mson/Mson.java b/src/com/massivecraft/massivecore/mson/Mson.java index c0c6ba74..b90968c3 100644 --- a/src/com/massivecraft/massivecore/mson/Mson.java +++ b/src/com/massivecraft/massivecore/mson/Mson.java @@ -207,7 +207,7 @@ public class Mson implements Serializable if (style == ChatColor.UNDERLINE) return this.underlined(true); if (style == ChatColor.STRIKETHROUGH) return this.striketrhough(true); if (style == ChatColor.MAGIC) return this.obfuscated(true); - if (style.isColor()) return this.color(color); + if (style.isColor()) return this.color(style); throw new UnsupportedOperationException(style.name()); } diff --git a/src/com/massivecraft/massivecore/mson/Test.java b/src/com/massivecraft/massivecore/mson/Test.java new file mode 100644 index 00000000..be7476c6 --- /dev/null +++ b/src/com/massivecraft/massivecore/mson/Test.java @@ -0,0 +1,139 @@ +package com.massivecraft.massivecore.mson; + +import static org.bukkit.ChatColor.*; +import static com.massivecraft.massivecore.mson.Mson.mson; + +import com.massivecraft.massivecore.collections.MassiveList; +import com.massivecraft.massivecore.util.Txt; + +public class Test +{ + public static void main(String[] args) + { + try + { + test(); + } + catch (Throwable t) + { + t.printStackTrace(); + } + } + + public static void test() + { + Mson mson; + + mson = mson("hello"); + test("ofString", mson); + + mson = mson( + mson("hello") + ); + test("ofMson", mson); + + mson = mson( + new MassiveList("hello ", "you!") + ); + test("ofCollection", mson); + + mson = mson("test") + .color(BLUE) + .addChild(" test2").link("www.massivecraft.com") + .addChild(" test3 ") + .addChildren("this ", "is only ", "one way to do it!") + .root() + .tooltip("Holy moly!"); + test("child", mson); + + mson = mson( + "test", + " test2", + " test3", + mson().text(" Test4, children: ").addChild("Child1 ").addSiblings("Sibling 1 ", "Sibling 2 ", "Sibling 3 ").addSibling("Sibling 4").root() + ).tooltip("Holy moly!"); + test("sibling", mson); + + mson = Mson.fromParsedMessage(Txt.parse("white info bad green")); + test("parsed", mson); + + mson = Mson.parse("white info bad green"); + test("parsed2", mson); + + mson = Mson.parse("insert here %s whatever you %s could wish for!", "!1337!", "herpi derp"); + test("parseFormat", mson); + + Mson format = Mson.format("Just a %s simple string! :)", "very"); + test("format", format); + + // Test split + mson = mson(format.split(Txt.REGEX_WHITESPACE.toString())); + test("split", mson); + + // -------------------------------------------- // + // TEST REPLACE + // -------------------------------------------- // + + mson = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replace('1', '0'); + test("charr", mson); + + mson = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replace("1", "0"); + test("sequence", mson); + + mson = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replaceAll("1", "0"); + test("regex", mson); + + // -------------------------------------------- // + // TEST SPECIAL REPLACE ALL + // -------------------------------------------- // + + // replace string mson + mson = mson("1 2 3 4 5 6 1 7 tests").color(BLUE).addChild(" 1+1+1").addChild("herpiderp").root().replaceAll("1", mson("0")); + test("replaceAll1", mson); + + mson = mson("hello").addChild("hello").addChild("hello").root().replaceAll("hello", mson("lol")); + test("overload", mson); + + Mson toReplace = mson("hallo"); + + // replace mson mson + mson = mson("1 2 3 4 5 6 7 tests").addChild(toReplace).addSibling(" miep").root().replaceAll(toReplace, mson("tests")); + test("replaceAll2", mson); + + mson = mson("1 2 3 4 5 6 7 tests").addChild("hallo").addChild("hallo").addChild("hallo").addSibling(" miep").root().replaceAll(mson("hallo"), mson("tests")); + test("overload2", mson); + + // -------------------------------------------- // + // EXAMPLES + // -------------------------------------------- // + + // Example: Would you like to [allow] or [deny]? + mson = mson( + "Would you like to ", + mson("[allow]").style(RED).command("/asfd blah allow"), + " or ", + mson("[deny]").style(GREEN).command("/asfd blah deny"), + "?" + ).style(YELLOW); + test("Would you like to [allow] or [deny]?", mson); + + // -------------------------------------------- // + // SPONGE MIMIC + // -------------------------------------------- // + // https://docs.spongepowered.org/en/plugin/basics/text.html + + // Text multiColoredText = Texts.builder("Sponges are ").color(TextColors.YELLOW).append(Texts.builder("invincible!").color(TextColors.RED).build()).build(); + mson = mson( + mson("Sponges are ").color(YELLOW), + mson("invincible!").color(RED) + ); + test("Sponges are invincible", mson); + + } + + public static void test(String name, Mson mson) + { + System.out.println(name + ": " + mson); + } + +}