TestNullableTypes.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using PetaTest;
  6. using PetaJson;
  7. using System.Reflection;
  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. [TestFixture]
  17. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  18. public class TestNullableTypes
  19. {
  20. [Test]
  21. public void TestNull()
  22. {
  23. var nc = new NullableContainer();
  24. var json = Json.Format(nc);
  25. Console.WriteLine(json);
  26. Assert.Contains(json, "null");
  27. var nc2 = Json.Parse<NullableContainer>(json);
  28. Assert.IsNull(nc2.Field);
  29. Assert.IsNull(nc2.Prop);
  30. }
  31. [Test]
  32. public void TestNotNull()
  33. {
  34. var nc = new NullableContainer()
  35. {
  36. Field = 23,
  37. Prop = 24,
  38. };
  39. var json = Json.Format(nc);
  40. Console.WriteLine(json);
  41. Assert.DoesNotContain(json, "null");
  42. var nc2 = Json.Parse<NullableContainer>(json);
  43. Assert.AreEqual(nc2.Field.Value, 23);
  44. Assert.AreEqual(nc2.Prop.Value, 24);
  45. }
  46. }
  47. }