TestDictionaryUtils.cs 3.1 KB

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