TestExcludeIfEquals.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Topten.JsonKit;
  6. using System.IO;
  7. using System.Reflection;
  8. using Xunit;
  9. namespace TestCases
  10. {
  11. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  12. public class TestExcludeIfEquals
  13. {
  14. enum Fruit
  15. {
  16. Apples,
  17. Pears,
  18. Bananas,
  19. }
  20. class Thing
  21. {
  22. [Json("boolField", ExcludeIfEquals = false)]
  23. public bool boolField;
  24. [Json("intField", ExcludeIfEquals = 0)]
  25. public int intField;
  26. [Json("boolProperty", ExcludeIfEquals = false)]
  27. public bool boolProperty { get; set; }
  28. [Json("intProperty", ExcludeIfEquals = 0)]
  29. public int intProperty { get; set; }
  30. [Json("enumField", ExcludeIfEquals = Fruit.Apples)]
  31. public Fruit enumField;
  32. [Json("enumProperty", ExcludeIfEquals = Fruit.Apples)]
  33. public Fruit enumProperty { get; set; }
  34. [Json("ulongProperty", ExcludeIfEquals = 0UL)]
  35. public ulong ulongProperty { get; set; }
  36. }
  37. public static object GetDefault(Type type)
  38. {
  39. if (type.IsValueType)
  40. {
  41. return Activator.CreateInstance(type);
  42. }
  43. return null;
  44. }
  45. [Fact]
  46. public void TestDoesntWrite()
  47. {
  48. var thing = new Thing()
  49. {
  50. boolField = false,
  51. intField = 0,
  52. boolProperty = false,
  53. intProperty = 0,
  54. ulongProperty = 0,
  55. enumField = Fruit.Apples,
  56. enumProperty = Fruit.Apples,
  57. };
  58. // Save it
  59. var json = Json.Format(thing);
  60. // Check the object kinds were written out
  61. Assert.DoesNotContain("\"boolField\":", json);
  62. Assert.DoesNotContain("\"intField\":", json);
  63. Assert.DoesNotContain("\"boolProperty\":", json);
  64. Assert.DoesNotContain("\"intProperty\":", json);
  65. Assert.DoesNotContain("\"enumField\":", json);
  66. Assert.DoesNotContain("\"ulongProperty\":", json);
  67. Assert.DoesNotContain("\"enumProperty\":", json);
  68. }
  69. [Fact]
  70. public void TestDoesWriteNonNull()
  71. {
  72. var thing = new Thing()
  73. {
  74. boolField = true,
  75. intField = 23,
  76. boolProperty = true,
  77. intProperty = 24,
  78. ulongProperty = 25,
  79. enumField = Fruit.Pears,
  80. enumProperty = Fruit.Bananas,
  81. };
  82. // Save it
  83. var json = Json.Format(thing);
  84. // Check the object kinds were written out
  85. Assert.Contains("\"boolField\":", json);
  86. Assert.Contains("\"intField\":", json);
  87. Assert.Contains("\"boolProperty\":", json);
  88. Assert.Contains("\"intProperty\":", json);
  89. Assert.Contains("\"enumField\":", json);
  90. Assert.Contains("\"enumProperty\":", json);
  91. Assert.Contains("\"ulongProperty\":", json);
  92. Assert.Contains("true", json);
  93. Assert.Contains("23", json);
  94. Assert.Contains("24", json);
  95. Assert.Contains("Pears", json);
  96. Assert.Contains("Bananas", json);
  97. }
  98. }
  99. }