TestsReflection.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. class ModelNotDecorated
  12. {
  13. public string Field1;
  14. public string Field2;
  15. public string Prop1 { get; set; }
  16. public string Prop2 { get; set; }
  17. }
  18. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  19. class ModelInclude
  20. {
  21. [Json] public string Field1;
  22. public string Field2;
  23. [Json] public string Prop1 { get; set; }
  24. public string Prop2 { get; set; }
  25. }
  26. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  27. class ModelExclude
  28. {
  29. public string Field1;
  30. public string Field2;
  31. public string Prop1 { get; set; }
  32. public string Prop2 { get; set; }
  33. [JsonExclude]
  34. public string Field3;
  35. [JsonExclude]
  36. public string Prop3 { get; set; }
  37. }
  38. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  39. class ModelRenamedMembers
  40. {
  41. [Json("Field1")] public string Field1;
  42. public string Field2;
  43. [Json("Prop1")] public string Prop1 { get; set; }
  44. public string Prop2 { get; set; }
  45. }
  46. [Json]
  47. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  48. class InstanceObject
  49. {
  50. public int IntVal1;
  51. [JsonExclude] public int IntVal2;
  52. }
  53. [Json]
  54. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  55. class ModelKeepInstance
  56. {
  57. [Json(KeepInstance=true)]
  58. public InstanceObject InstObj;
  59. }
  60. [Json]
  61. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  62. class ModelWithInstance
  63. {
  64. [Json]
  65. public InstanceObject InstObj;
  66. }
  67. [Json]
  68. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  69. struct ModelStruct
  70. {
  71. public int IntField;
  72. public int IntProp { get; set; }
  73. }
  74. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  75. public class TestsReflection
  76. {
  77. [Fact]
  78. public void ExcludeAttribute()
  79. {
  80. var m = new ModelExclude()
  81. {
  82. Field1 = "f1",
  83. Field2 = "f2",
  84. Field3 = "f3",
  85. Prop1 = "p1",
  86. Prop2 = "p2",
  87. Prop3 = "p3",
  88. };
  89. var json = Json.Format(m);
  90. Assert.Contains("field1", json);
  91. Assert.Contains("field2", json);
  92. Assert.DoesNotContain("field3", json);
  93. Assert.Contains("prop1", json);
  94. Assert.Contains("prop2", json);
  95. Assert.DoesNotContain("prop3", json);
  96. }
  97. [Fact]
  98. public void NonDecorated()
  99. {
  100. var m = new ModelNotDecorated()
  101. {
  102. Field1 = "f1",
  103. Field2 = "f2",
  104. Prop1 = "p1",
  105. Prop2 = "p2",
  106. };
  107. var json = Json.Format(m);
  108. Assert.Contains("field1", json);
  109. Assert.Contains("field2", json);
  110. Assert.Contains("prop1", json);
  111. Assert.Contains("prop2", json);
  112. }
  113. [Fact]
  114. public void Include()
  115. {
  116. var m = new ModelInclude()
  117. {
  118. Field1 = "f1",
  119. Field2 = "f2",
  120. Prop1 = "p1",
  121. Prop2 = "p2",
  122. };
  123. var json = Json.Format(m);
  124. Assert.Contains("field1", json);
  125. Assert.DoesNotContain("field2", json);
  126. Assert.Contains("prop1", json);
  127. Assert.DoesNotContain("prop2", json);
  128. }
  129. [Fact]
  130. public void RenamedMembers()
  131. {
  132. var m = new ModelRenamedMembers()
  133. {
  134. Field1 = "f1",
  135. Field2 = "f2",
  136. Prop1 = "p1",
  137. Prop2 = "p2",
  138. };
  139. var json = Json.Format(m);
  140. Assert.Contains("Field1", json);
  141. Assert.DoesNotContain("field2", json);
  142. Assert.Contains("Prop1", json);
  143. Assert.DoesNotContain("prop2", json);
  144. }
  145. [Fact]
  146. public void KeepInstanceTest1()
  147. {
  148. // Create model and save it
  149. var ki = new ModelKeepInstance();
  150. ki.InstObj = new InstanceObject();
  151. ki.InstObj.IntVal1 = 1;
  152. ki.InstObj.IntVal2 = 2;
  153. var json = Json.Format(ki);
  154. // Update the kept instance object
  155. ki.InstObj.IntVal1 = 11;
  156. ki.InstObj.IntVal2 = 12;
  157. // Reload
  158. var oldInst = ki.InstObj;
  159. Json.ParseInto(json, ki);
  160. // Check object instance kept
  161. Assert.Same(oldInst, ki.InstObj);
  162. // Check json properties updated, others not
  163. Assert.Equal(1, ki.InstObj.IntVal1);
  164. Assert.Equal(12, ki.InstObj.IntVal2);
  165. }
  166. [Fact]
  167. public void KeepInstanceTest2()
  168. {
  169. // Create model and save it
  170. var ki = new ModelKeepInstance();
  171. ki.InstObj = new InstanceObject();
  172. ki.InstObj.IntVal1 = 1;
  173. ki.InstObj.IntVal2 = 2;
  174. var json = Json.Format(ki);
  175. // Update the kept instance object
  176. ki.InstObj = null;
  177. // Reload
  178. Json.ParseInto(json, ki);
  179. // Check object instance kept
  180. Assert.NotNull(ki.InstObj);
  181. // Check json properties updated, others not
  182. Assert.Equal(1, ki.InstObj.IntVal1);
  183. Assert.Equal(0, ki.InstObj.IntVal2);
  184. }
  185. [Fact]
  186. public void StructTest()
  187. {
  188. var o = new ModelStruct();
  189. o.IntField = 23;
  190. o.IntProp = 24;
  191. var json = Json.Format(o);
  192. Assert.Contains("23", json);
  193. Assert.Contains("24", json);
  194. var o2 = Json.Parse<ModelStruct>(json);
  195. Assert.Equal(23, o2.IntField);
  196. Assert.Equal(24, o2.IntProp);
  197. // Test parseInto on a value type not supported
  198. var o3 = new ModelStruct();
  199. Assert.Throws<InvalidOperationException>(() => Json.ParseInto(json, o3));
  200. }
  201. [Fact]
  202. public void NullClassMember()
  203. {
  204. var m = new ModelWithInstance();
  205. var json = Json.Format(m);
  206. Assert.Contains("null", json);
  207. m.InstObj = new InstanceObject();
  208. Json.ParseInto(json, m);
  209. Assert.Null(m.InstObj);
  210. }
  211. [Fact]
  212. public void NullClass()
  213. {
  214. // Save null
  215. var json = Json.Format(null);
  216. Assert.Equal("null", json);
  217. // Load null
  218. var m = Json.Parse<ModelWithInstance>("null");
  219. Assert.Null(m);
  220. // Should fail to parse null into an existing instance
  221. m = new ModelWithInstance();
  222. Assert.Throws<JsonParseException>(() => Json.ParseInto("null", m));
  223. }
  224. }
  225. }