1
0

ChatTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using Xunit;
  6. namespace MineCase.UnitTest
  7. {
  8. public class ChatTest
  9. {
  10. /// <summary>
  11. /// Tests Chat.ToJObject.
  12. /// </summary>
  13. [Fact]
  14. public void Test1()
  15. {
  16. var chat = new Chat(new StringComponent
  17. {
  18. Text = "hello case!",
  19. Bold = true,
  20. Itatic = false,
  21. Color = ColorType.Blue,
  22. ClickEvent = new ChatClickEvent(ClickEventType.OpenUrl, @"http://case.orz")
  23. });
  24. var o = chat.ToJObject();
  25. var o2 = JObject.Parse(chat.ToString());
  26. Assert.Equal("hello case!", o.GetValue("text"));
  27. Assert.True(o.GetValue("bold").Value<bool>());
  28. Assert.Equal("blue", o.GetValue("color"));
  29. Assert.Equal("open_url", (string)o.SelectToken("clickEvent.action"));
  30. Assert.Equal(@"http://case.orz", (string)o.SelectToken("clickEvent.value"));
  31. Assert.True(JToken.DeepEquals(o, o2));
  32. }
  33. /// <summary>
  34. /// Tests Chat.ToString.
  35. /// </summary>
  36. [Fact]
  37. public void Test2()
  38. {
  39. var stringComponent = new StringComponent
  40. {
  41. Text = "hello",
  42. Bold = true,
  43. Itatic = false,
  44. Color = ColorType.Green,
  45. ClickEvent = new ChatClickEvent
  46. {
  47. Action = ClickEventType.ChangePage,
  48. Value = 1
  49. }
  50. };
  51. var chat = new Chat(stringComponent);
  52. Assert.Equal("{\"bold\":true,\"itatic\":false,\"color\":\"green\",\"clickEvent\":{\"action\":\"change_page\",\"value\":1},\"text\":\"hello\"}", chat.ToString());
  53. }
  54. /// <summary>
  55. /// Tests Chat.Parse.
  56. /// </summary>
  57. [Fact]
  58. public void Test3()
  59. {
  60. const string json = @"{
  61. ""text"":""case"",
  62. ""bold"":true,
  63. ""itatic"":true,
  64. ""color"":""red"",
  65. ""clickEvent"":{
  66. ""action"":""change_page"",
  67. ""value"":1
  68. },
  69. ""extra"":[
  70. { ""text"":""foo"",""bold"":false },
  71. { ""text"":""bar"" }
  72. ]
  73. }";
  74. var chat = Chat.Parse(json);
  75. var jObject = JObject.Parse(json);
  76. var sc = (StringComponent)chat.Component;
  77. Assert.Equal("case", sc.Text);
  78. Assert.True(sc.Bold);
  79. Assert.True(sc.Itatic);
  80. Assert.Equal(ColorType.Red, sc.Color);
  81. Assert.Equal(ClickEventType.ChangePage, sc.ClickEvent.Action);
  82. Assert.Equal(1, sc.ClickEvent.Value.Value<int>());
  83. Assert.False(jObject.SelectToken("extra[0].bold").Value<bool>());
  84. Assert.Equal("foo", jObject.SelectToken("extra[0].text"));
  85. Assert.Equal("bar", jObject.SelectToken("extra[1].text"));
  86. }
  87. /// <summary>
  88. /// More test cases.
  89. /// </summary>
  90. [Fact]
  91. public void Test4()
  92. {
  93. var chat = new Chat("text")
  94. {
  95. Component =
  96. {
  97. Bold = true,
  98. Itatic = false,
  99. Insertion = "insert",
  100. Color = ColorType.Red,
  101. ClickEvent = new ChatClickEvent(ClickEventType.ChangePage, 1),
  102. HoverEvent = new ChatHoverEvent(HoverEventType.ShowText, "show")
  103. }
  104. };
  105. var keybind = new KeybindComponent
  106. {
  107. Keybind = KeyBindType.Back,
  108. Bold = false,
  109. Itatic = true,
  110. Color = ColorType.Green
  111. };
  112. chat.Component.Extra = new List<ChatComponent> { keybind };
  113. var json = chat.ToJObject();
  114. Assert.Null(json.SelectToken("underlined"));
  115. Assert.Null(json.SelectToken("strikethrough"));
  116. Assert.Null(json.SelectToken("obfuscated"));
  117. Assert.True(json.SelectToken("bold").Value<bool>());
  118. Assert.False(json.SelectToken("extra[0].bold").Value<bool>());
  119. Assert.False(json.SelectToken("itatic").Value<bool>());
  120. Assert.True(json.SelectToken("extra[0].itatic").Value<bool>());
  121. Assert.Equal("red", json.SelectToken("color"));
  122. Assert.Equal("green", json.SelectToken("extra[0].color"));
  123. Assert.Equal("key.back", json.SelectToken("extra[0].keybind"));
  124. Assert.Equal("insert", json.SelectToken("insertion"));
  125. Assert.Equal("change_page", json.SelectToken("clickEvent.action"));
  126. Assert.Equal(1, json.SelectToken("clickEvent.value").Value<int>());
  127. Assert.Equal("show_text", json.SelectToken("hoverEvent.action"));
  128. Assert.Equal("show", json.SelectToken("hoverEvent.value"));
  129. }
  130. /// <summary>
  131. /// Tests TranslationComponent.
  132. /// </summary>
  133. [Fact]
  134. public void Test5()
  135. {
  136. var sc = new StringComponent("text")
  137. {
  138. Bold = true,
  139. ClickEvent = new ChatClickEvent(ClickEventType.RunCommand, "/msg a")
  140. };
  141. var list = new List<ChatComponent> { sc, new StringComponent("nothing") };
  142. var chat = new Chat(new TranslationComponent("chat.type.text", list));
  143. var jObject = chat.ToJObject();
  144. Assert.Equal("chat.type.text", jObject.SelectToken("translate"));
  145. Assert.True(jObject.SelectToken("with[0].bold").Value<bool>());
  146. Assert.Equal("run_command", jObject.SelectToken("with[0].clickEvent.action"));
  147. Assert.Equal("/msg a", jObject.SelectToken("with[0].clickEvent.value"));
  148. Assert.Equal("text", jObject.SelectToken("with[0].text"));
  149. Assert.Equal("nothing", jObject.SelectToken("with[1].text"));
  150. string json = @"{
  151. ""translate"":""chat.type.text"",
  152. ""with"":[
  153. {
  154. ""bold"":true,
  155. ""text"":""text"",
  156. ""clickEvent"":{""action"":""run_command"",""value"":""/msg a""}
  157. },
  158. {
  159. ""text"":""nothing""
  160. }
  161. ]
  162. }";
  163. var chat2 = Chat.Parse(json);
  164. Assert.True(JToken.DeepEquals(jObject, chat2.ToJObject()));
  165. }
  166. /// <summary>
  167. /// Tests KeybindComponent.
  168. /// </summary>
  169. [Fact]
  170. public void Test6()
  171. {
  172. var keybind = new KeybindComponent(KeyBindType.Attack)
  173. {
  174. Extra = new List<ChatComponent>
  175. {
  176. new StringComponent("text1"),
  177. new StringComponent("text2")
  178. }
  179. };
  180. var chat = new Chat(keybind);
  181. var j = chat.ToJObject();
  182. Assert.Equal("key.attack", j.SelectToken("keybind"));
  183. Assert.Equal("text1", j.SelectToken("extra[0].text"));
  184. Assert.Equal("text2", j.SelectToken("extra[1].text"));
  185. const string json = @"{
  186. ""keybind"":""key.attack"",
  187. ""extra"":[
  188. { ""text"":""text1"" },
  189. { ""text"":""text2"" }
  190. ]
  191. }";
  192. var chat2 = Chat.Parse(json);
  193. Assert.True(JToken.DeepEquals(j, chat2.ToJObject()));
  194. }
  195. /// <summary>
  196. /// Tests ScoreComponent.
  197. /// </summary>
  198. [Fact]
  199. public void Test7()
  200. {
  201. var score = new ScoreComponent(new ChatScore("case", "ball", 100))
  202. {
  203. Extra = new List<ChatComponent>
  204. {
  205. new StringComponent("text1"),
  206. new StringComponent("text2")
  207. }
  208. };
  209. var chat = new Chat(score);
  210. var j = chat.ToJObject();
  211. Assert.Equal("case", j.SelectToken("score.name"));
  212. Assert.Equal("ball", j.SelectToken("score.objective"));
  213. Assert.Equal(100, j.SelectToken("score.value").Value<int>());
  214. Assert.Equal("text1", j.SelectToken("extra[0].text"));
  215. Assert.Equal("text2", j.SelectToken("extra[1].text"));
  216. const string json = @"{
  217. ""score"":{
  218. ""name"":""case"",
  219. ""objective"":""ball"",
  220. ""value"":100
  221. },
  222. ""extra"":[
  223. { ""text"":""text1"" },
  224. { ""text"":""text2"" }
  225. ]
  226. }";
  227. var chat2 = Chat.Parse(json);
  228. Assert.True(JToken.DeepEquals(j, chat2.ToJObject()));
  229. }
  230. /// <summary>
  231. /// Tests SelectorComponent.
  232. /// </summary>
  233. [Fact]
  234. public void Test8()
  235. {
  236. var chat = new Chat(new SelectorComponent("@p"))
  237. {
  238. Component =
  239. {
  240. Extra = new List<ChatComponent>
  241. {
  242. new StringComponent("text1"),
  243. new StringComponent("text2")
  244. }
  245. }
  246. };
  247. var j = chat.ToJObject();
  248. Assert.Equal("@p", j.SelectToken("selector"));
  249. Assert.Equal("text1", j.SelectToken("extra[0].text"));
  250. Assert.Equal("text2", j.SelectToken("extra[1].text"));
  251. const string json = @"{
  252. ""selector"":""@p"",
  253. ""extra"":[
  254. { ""text"":""text1"" },
  255. { ""text"":""text2"" }
  256. ]
  257. }";
  258. var chat2 = Chat.Parse(json);
  259. Assert.True(JToken.DeepEquals(j, chat2.ToJObject()));
  260. }
  261. /// <summary>
  262. /// Tests exceptions.
  263. /// </summary>
  264. [Fact]
  265. public void Test9()
  266. {
  267. // parse exception
  268. const string json = @"{""text"":}";
  269. Assert.Throws<JsonException>(() => Chat.Parse(json));
  270. // no key exception
  271. Chat chat = new Chat(); // no compoent
  272. Assert.Throws<InvalidOperationException>(() => chat.ToJObject());
  273. // no key exception
  274. var comp = new StringComponent();
  275. Chat chat2 = new Chat(comp); // compoent no specified key
  276. Assert.Throws<InvalidOperationException>(() => chat2.ToJObject());
  277. // no key exception
  278. Chat chat3 = new Chat(
  279. new ScoreComponent(
  280. new ChatScore
  281. {
  282. Name = "case" // no name key or objective key
  283. }));
  284. Assert.Throws<InvalidOperationException>(() => chat3.ToJObject());
  285. }
  286. }
  287. }