TestsGeneral.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Topten.JsonKit;
  6. using System.Reflection;
  7. using Xunit;
  8. namespace TestCases
  9. {
  10. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  11. public class TestsGeneral
  12. {
  13. [Fact]
  14. public void Format_Null()
  15. {
  16. Assert.Equal("null", Json.Format(null));
  17. }
  18. [Fact]
  19. public void Format_Boolean()
  20. {
  21. Assert.Equal("true", Json.Format(true));
  22. Assert.Equal("false", Json.Format(false));
  23. }
  24. [Fact]
  25. public void Format_String()
  26. {
  27. Assert.Equal("\"Hello World\"", Json.Format("Hello World"));
  28. Assert.Equal("\" \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u0000 \u1234\"", Json.Format(" \" \\ / \b \f \n \r \t \0 \u1234"));
  29. }
  30. [Fact]
  31. public void Format_Numbers()
  32. {
  33. Assert.Equal("123", Json.Format(123));
  34. Assert.Equal("-123", Json.Format(-123));
  35. Assert.Equal("123", Json.Format(123.0));
  36. Assert.Equal("123.4", Json.Format(123.4));
  37. Assert.Equal("-123.4", Json.Format(-123.4));
  38. Assert.Equal("-1.2345E-65", Json.Format(-123.45E-67));
  39. Assert.Equal("123", Json.Format(123U));
  40. Assert.Equal("255", Json.Format(0xFF));
  41. Assert.Equal("255", Json.Format(0xFFU));
  42. Assert.Equal("18446744073709551615", Json.Format(0xFFFFFFFFFFFFFFFFL));
  43. }
  44. [Fact]
  45. public void Format_Empty_Array()
  46. {
  47. Assert.Equal("[]", Json.Format(new int[] { }));
  48. }
  49. [Fact]
  50. public void Format_Simple_Array()
  51. {
  52. Assert.Equal("[\n\t1,\n\t2,\n\t3\n]", Json.Format(new int[] { 1, 2, 3 }));
  53. }
  54. [Fact]
  55. public void Format_Empty_Dictionary()
  56. {
  57. Assert.Equal("{}", Json.Format(new Dictionary<int, int>() { }));
  58. }
  59. [Fact]
  60. public void Format_Simple_Dictionary()
  61. {
  62. var result = Json.Format(new Dictionary<string, int>() { {"Apples", 1}, {"Pears", 2} , {"Bananas", 3 } });
  63. Assert.Equal("{\n\t\"Apples\": 1,\n\t\"Pears\": 2,\n\t\"Bananas\": 3\n}", result);
  64. }
  65. [Fact]
  66. public void Format_Date()
  67. {
  68. Assert.Equal("1293840000000", Json.Format(new DateTime(2011, 1, 1, 0, 0, 0, DateTimeKind.Utc)));
  69. }
  70. [Fact]
  71. public void Format_Poco()
  72. {
  73. var result = Json.Format(new { Apples=1, Pears=2, Bananas=3});
  74. Assert.Equal("{\n\t\"apples\": 1,\n\t\"pears\": 2,\n\t\"bananas\": 3\n}", result);
  75. }
  76. [Fact]
  77. public void Parse_Null()
  78. {
  79. Assert.Null(Json.Parse<object>("null"));
  80. }
  81. [Fact]
  82. public void Parse_Boolean()
  83. {
  84. Assert.True(Json.Parse<bool>("true"));
  85. Assert.False(Json.Parse<bool>("false"));
  86. }
  87. [Fact]
  88. public void Parse_String()
  89. {
  90. var s = Json.Parse<string>("\"Hello\\r\\n\\t\\u0000 World\"");
  91. Assert.Equal("Hello\r\n\t\0 World", (string)s);
  92. }
  93. [Fact]
  94. public void Parse_Numbers()
  95. {
  96. Assert.Equal(0, Json.Parse<int>("0"));
  97. Assert.Equal(123, Json.Parse<int>("123"));
  98. Assert.Equal(123.45, Json.Parse<double>("123.45"));
  99. Assert.Equal(123e45, Json.Parse<double>("123e45"));
  100. Assert.Equal(123.0e45, Json.Parse<double>("123.0e45"));
  101. Assert.Equal(123e45, Json.Parse<double>("123e+45"));
  102. Assert.Equal(123.0e45, Json.Parse<double>("123.0e+45"));
  103. Assert.Equal(123e-45, Json.Parse<double>("123e-45"));
  104. Assert.Equal(123.0e-45, Json.Parse<double>("123.0e-45"));
  105. Assert.Equal(123E45, Json.Parse<double>("123E45"));
  106. Assert.Equal(-123e45, Json.Parse<double>("-123e45"));
  107. }
  108. [Fact]
  109. public void Parse_Empty_Array()
  110. {
  111. var d = Json.Parse<object[]>("[]");
  112. Assert.Equal(new object[] { }, d as object[]);
  113. }
  114. [Fact]
  115. public void Parse_simple_Array()
  116. {
  117. var d = Json.Parse<int[]>("[1,2,3]");
  118. Assert.Equal(new int[] { 1, 2, 3} , d);
  119. }
  120. [Fact]
  121. public void Parse_Date()
  122. {
  123. var d1 = new DateTime(2011, 1, 1, 10, 10, 10, DateTimeKind.Utc);
  124. var d2 = Json.Parse<DateTime>(Json.Format(d1));
  125. Assert.Equal(d2, d1);
  126. }
  127. [Fact]
  128. public void DynamicTest()
  129. {
  130. var d = Json.Parse<IDictionary<string, object>>("{\"apples\":1, \"pears\":2, \"bananas\":3}") ;
  131. Assert.Equal(new string[] { "apples", "pears", "bananas" }, d.Keys);
  132. Assert.Equal(new object[] { 1UL, 2UL, 3UL }, d.Values);
  133. }
  134. [Fact]
  135. public void Invalid_Numbers()
  136. {
  137. Assert.Throws<JsonParseException>(() => Json.Parse<object>("123ee0"));
  138. Assert.Throws<JsonParseException>(() => Json.Parse<object>("+123"));
  139. Assert.Throws<JsonParseException>(() => Json.Parse<object>("--123"));
  140. Assert.Throws<JsonParseException>(() => Json.Parse<object>("--123..0"));
  141. Assert.Throws<JsonParseException>(() => Json.Parse<object>("--123ex"));
  142. Assert.Throws<JsonParseException>(() => Json.Parse<object>("123x"));
  143. Assert.Throws<JsonParseException>(() => Json.Parse<object>("0x123", JsonOptions.StrictParser));
  144. }
  145. [Fact]
  146. public void Invalid_Trailing_Characters()
  147. {
  148. Assert.Throws<JsonParseException>(()=> Json.Parse<object>("\"Hello\" , 123"));
  149. }
  150. [Fact]
  151. public void Invalid_Identifier()
  152. {
  153. Assert.Throws<JsonParseException>(() => Json.Parse<object>("identifier"));
  154. }
  155. [Fact]
  156. public void Invalid_Character()
  157. {
  158. Assert.Throws<JsonParseException>(() => Json.Parse<object>("~"));
  159. }
  160. [Fact]
  161. public void Invalid_StringEscape()
  162. {
  163. Assert.Throws<JsonParseException>(() => Json.Parse<object>("\"\\q\""));
  164. }
  165. [Fact]
  166. public void ErrorLocation()
  167. {
  168. var strJson="{\r\n \r\n \n\r \r \n \t \"key:\": zzz";
  169. try
  170. {
  171. Json.Parse<object>(strJson);
  172. }
  173. catch (JsonParseException x)
  174. {
  175. Assert.Equal(5, x.Position.Line);
  176. Assert.Equal(13, x.Position.Offset);
  177. }
  178. }
  179. }
  180. }