public void SaveLiteral() { Dom dom = Dom.Serialise("hello"); string output = dom.Save(); Assert.AreEqual("String \"hello\"", output); }
public void WriteParsableNumberObject() { Dom dom = Dom.Serialise(10); Assert.IsInstanceOfType(dom.Root.GetNode(), typeof(Dom.LiteralNode)); Dom.LiteralNode node = dom.Root.GetNode() as Dom.LiteralNode; Assert.AreEqual(typeof(int), node.Type); Assert.AreEqual("10", node.Value); }
public void WriteParsableCustomObject() { var time = new DateTime(2010, 10, 4); Dom dom = Dom.Serialise(time); Assert.IsInstanceOfType(dom.Root.GetNode(), typeof(Dom.LiteralNode)); Dom.LiteralNode node = dom.Root.GetNode() as Dom.LiteralNode; Assert.AreEqual(typeof(DateTime), node.Type); Assert.AreEqual(string.Format("{0}", time), node.Value); }
public void SaveArray() { int[] value = new int[] { 1, 2, 3, 4 }; Dom dom = Dom.Serialise(value); string output = dom.Save(); string expected = "Int32[] [\n Int32 \"1\",\n Int32 \"2\",\n Int32 \"3\",\n Int32 \"4\"\n]"; Assert.AreEqual(expected, output); }
public void DeserialiseObject() { var expected = new NonParsable() { A = 5, B = 5.0f, C = "hello world" }; Dom dom = Dom.Serialise(expected); var actual = dom.Deserialise <NonParsable>(); Assert.AreEqual(expected, actual); }
public void SaveDictionary() { Dictionary <int, string> dictionary = new Dictionary <int, string>() { { 1, "one" }, { 2, "two" }, { 3, "three" } }; Dom dom = Dom.Serialise(dictionary); string output = dom.Save(); string expected = "Dictionary<Int32,String> [\n Int32 \"1\": String \"one\",\n Int32 \"2\": String \"two\",\n Int32 \"3\": String \"three\"\n]"; Assert.AreEqual(expected, output); }
public void SaveList() { List <int> list = new List <int>() { 1, 2, 3, 4, 5 }; Dom dom = Dom.Serialise(list); string output = dom.Save(); string expected = "List<Int32> [\n Int32 \"1\",\n Int32 \"2\",\n Int32 \"3\",\n Int32 \"4\",\n Int32 \"5\"\n]"; Assert.AreEqual(expected, output); }
public void SaveObject() { NonParsable foo = new NonParsable() { A = 5, B = 5.0f, C = "hello \"world\"!" }; Dom dom = Dom.Serialise(foo); string output = dom.Save(); string expected = "SaveTest+NonParsable {\n A: Int32 \"5\",\n B: Single \"5\",\n C: String \"hello \\\"world\\\"!\",\n D: null\n}"; Assert.AreEqual(expected, output); }
public void SaveSharedResources() { string foo = "Foo"; TwoStrings value = new TwoStrings() { A = foo, B = foo }; Dom dom = Dom.Serialise(value); string output = dom.Save(); string expected = "R[\n 0: String \"Foo\"\n]\nSaveTest+TwoStrings {\n A: #0,\n B: #0\n}"; Assert.AreEqual(expected, output); }
public void SaveCompositeType() { CompositeType value = new CompositeType(); value.Foo = "foobar"; value.Bar.A = 5; value.Bar.B = 10; value.Bar.C = "hello \"world\"!"; Dom dom = Dom.Serialise(value); string output = dom.Save(); string expected = "SaveTest+CompositeType {\n Foo: String \"foobar\",\n Bar: SaveTest+NonParsable {\n A: Int32 \"5\",\n B: Single \"10\",\n C: String \"hello \\\"world\\\"!\",\n D: null\n }\n}"; Assert.AreEqual(expected, output); }
public void WriteArray() { int[] array = new int[] { 1, 2, 3, 4, 5 }; Dom dom = Dom.Serialise(array); Assert.IsInstanceOfType(dom.Root.GetNode(), typeof(Dom.ListNode)); Dom.ListNode node = dom.Root.GetNode() as Dom.ListNode; Assert.AreEqual(typeof(int[]), node.Type); Assert.AreEqual(array.Length, node.Children.Count); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(typeof(int), node.Children[i].GetNode().Type); Assert.AreEqual(string.Format("{0}", array[i]), (node.Children[i].GetNode() as Dom.LiteralNode).Value); } }
public void SaveCompositeTypeDictionary() { CompositeType value = new CompositeType(); value.Foo = "foobar"; value.Bar.A = 5; value.Bar.B = 10; value.Bar.C = "hello \"world\"!"; var dictionary = new Dictionary <CompositeType, CompositeType>() { { value, value } }; Dom dom = Dom.Serialise(dictionary); string output = dom.Save(); string expected = "Dictionary<SaveTest+CompositeType,SaveTest+CompositeType> [\n SaveTest+CompositeType {\n Foo: String \"foobar\",\n Bar: SaveTest+NonParsable {\n A: Int32 \"5\",\n B: Single \"10\",\n C: String \"hello \\\"world\\\"!\",\n D: null\n }\n }: SaveTest+CompositeType {\n Foo: String \"foobar\",\n Bar: SaveTest+NonParsable {\n A: Int32 \"5\",\n B: Single \"10\",\n C: String \"hello \\\"world\\\"!\",\n D: null\n }\n }\n]"; Assert.AreEqual(expected, output); }
public void WriteList() { List <int> list = new List <int>() { 1, 2, 3, 4, 5 }; Dom dom = Dom.Serialise(list); Assert.IsInstanceOfType(dom.Root.GetNode(), typeof(Dom.ListNode)); Dom.ListNode node = dom.Root.GetNode() as Dom.ListNode; Assert.AreEqual(typeof(List <int>), node.Type); Assert.AreEqual(list.Count, node.Children.Count); for (int i = 0; i < list.Count; i++) { Assert.AreEqual(typeof(int), node.Children[i].GetNode().Type); Assert.AreEqual(string.Format("{0}", list[i]), (node.Children[i].GetNode() as Dom.LiteralNode).Value); } }
public void WriteNonParsableObject() { NonParsable foo = new NonParsable() { A = 5, B = 5.0f, C = "hello world" }; Dom dom = Dom.Serialise(foo); Assert.IsInstanceOfType(dom.Root.GetNode(), typeof(Dom.ObjectNode)); Dom.ObjectNode node = dom.Root.GetNode() as Dom.ObjectNode; Assert.AreEqual(typeof(NonParsable), node.Type); Assert.AreEqual(typeof(int), node.Children["A"].GetNode().Type); Assert.AreEqual("5", (node.Children["A"].GetNode() as Dom.LiteralNode).Value); Assert.AreEqual(typeof(float), node.Children["B"].GetNode().Type); Assert.AreEqual("5", (node.Children["B"].GetNode() as Dom.LiteralNode).Value); Assert.AreEqual(typeof(string), node.Children["C"].GetNode().Type); Assert.AreEqual("hello world", (node.Children["C"].GetNode() as Dom.LiteralNode).Value); }
public void WriteDictionary() { Dictionary <string, int> dictionary = new Dictionary <string, int>() { { "foo", 1 }, { "bar", 2 } }; Dom dom = Dom.Serialise(dictionary); Assert.IsInstanceOfType(dom.Root.GetNode(), typeof(Dom.DictionaryNode)); Dom.DictionaryNode node = dom.Root.GetNode() as Dom.DictionaryNode; Assert.AreEqual(typeof(Dictionary <string, int>), node.Type); Assert.AreEqual(dictionary.Count, node.Children.Count); foreach (var key in dictionary.Keys) { var matches = node.Children.Where(kvp => (kvp.Key.GetNode() as Dom.LiteralNode).Value == string.Format("{0}", key) && (kvp.Value.GetNode() as Dom.LiteralNode).Value == string.Format("{0}", dictionary[key])); Assert.AreEqual(1, matches.Count()); } }