Better test layout and fix a bug.

This commit is contained in:
Olof Larsson 2015-06-05 09:29:42 +02:00
parent 7f45486600
commit 9c6bb882b0
3 changed files with 140 additions and 109 deletions

View File

@ -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<String>("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 <i>in<em>fo <b><em><bold>bad <lime>green"));
System.out.println("parsed:" + parsed); // Success
Mson parsed2 = Mson.parse("white <i>in<em>fo <b><bold>bad <lime>green");
System.out.println("parsed2:" + parsed2.toRaw()); // Success
Mson parseFormat = Mson.parse("<i>insert here %s whatever <g> you <b>%s <g>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<Mson> 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();
}
}
}

View File

@ -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());
}

View File

@ -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<String>("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 <i>in<em>fo <b><em><bold>bad <lime>green"));
test("parsed", mson);
mson = Mson.parse("white <i>in<em>fo <b><bold>bad <lime>green");
test("parsed2", mson);
mson = Mson.parse("<i>insert here %s whatever <g> you <b>%s <g>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);
}
}