TestDictionaryUtils.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using PetaTest;
  6. using PetaJson;
  7. namespace TestCases
  8. {
  9. [TestFixture]
  10. public class TestDictionaryUtils
  11. {
  12. [Test]
  13. public void DictionaryPaths()
  14. {
  15. var dict = new Dictionary<string, object>();
  16. dict.SetPath("settings.subSettings.settingA", 23);
  17. dict.SetPath("settings.subSettings.settingB", 24);
  18. Assert.IsTrue(dict.ContainsKey("settings"));
  19. Assert.IsTrue(((IDictionary<string, object>)dict["settings"]).ContainsKey("subSettings"));
  20. Assert.AreEqual(dict.GetPath<int>("settings.subSettings.settingA"), 23);
  21. Assert.AreEqual(dict.GetPath<int>("settings.subSettings.settingB"), 24);
  22. Assert.IsTrue(dict.PathExists("settings.subSettings"));
  23. Assert.IsTrue(dict.PathExists("settings.subSettings.settingA"));
  24. Assert.IsFalse(dict.PathExists("missing_in_action"));
  25. }
  26. [Test]
  27. public void DictionaryReparseType()
  28. {
  29. // Create and initialize and object then convert it to a dictionary
  30. var o = new DaObject() { id = 101, Name = "#101" };
  31. var oDict = Json.Reparse<IDictionary<string, object>>(o);
  32. // Store that dictionary at a path inside another dictionary
  33. var dict = new Dictionary<string, object>();
  34. dict.SetPath("settings.daObject", oDict);
  35. // Get it back out, but reparse it back into a strongly typed object
  36. var o2 = dict.GetPath<DaObject>("settings.daObject");
  37. Assert.AreEqual(o2.id, o.id);
  38. Assert.AreEqual(o2.Name, o.Name);
  39. }
  40. [Test]
  41. public void ObjectAtPath()
  42. {
  43. // Create and initialize and object then convert it to a dictionary
  44. var o = new DaObject() { id = 101, Name = "#101" };
  45. var oDict = Json.Reparse<IDictionary<string, object>>(o);
  46. // Store that dictionary at a path inside another dictionary
  47. var dict = new Dictionary<string, object>();
  48. dict.SetPath("settings.daObject", oDict);
  49. // Get it back as an object (and update dict to hold an actual DaObject
  50. var o2 = dict.GetObjectAtPath<DaObject>("settings.daObject");
  51. // Modify it
  52. o2.id = 102;
  53. o2.Name = "modified";
  54. // Save the dictionary and make sure we got the change
  55. var json = Json.Format(dict);
  56. Assert.Contains(json, "102");
  57. Assert.Contains(json, "modified");
  58. }
  59. [Test]
  60. public void NewObjectAtPath()
  61. {
  62. // Create a new object at a path
  63. var dict = new Dictionary<string, object>();
  64. var o2 = dict.GetObjectAtPath<DaObject>("settings.daObject");
  65. // Modify it
  66. o2.id = 103;
  67. o2.Name = "new guy";
  68. // Save the dictionary and make sure we got the change
  69. var json = Json.Format(dict);
  70. Assert.Contains(json, "103");
  71. Assert.Contains(json, "new guy");
  72. }
  73. }
  74. }