TestNullableTypes.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 NullableContainer
  12. {
  13. public int? Field;
  14. public int? Prop { get; set; }
  15. }
  16. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  17. public class TestNullableTypes
  18. {
  19. [Fact]
  20. public void TestNull()
  21. {
  22. var nc = new NullableContainer();
  23. var json = Json.Format(nc);
  24. Console.WriteLine(json);
  25. Assert.Contains("null", json);
  26. var nc2 = Json.Parse<NullableContainer>(json);
  27. Assert.Null(nc2.Field);
  28. Assert.Null(nc2.Prop);
  29. }
  30. [Fact]
  31. public void TestNotNull()
  32. {
  33. var nc = new NullableContainer()
  34. {
  35. Field = 23,
  36. Prop = 24,
  37. };
  38. var json = Json.Format(nc);
  39. Console.WriteLine(json);
  40. Assert.DoesNotContain(json, "null");
  41. var nc2 = Json.Parse<NullableContainer>(json);
  42. Assert.Equal(23, nc2.Field.Value);
  43. Assert.Equal(24, nc2.Prop.Value);
  44. }
  45. }
  46. }