using System.Xml; using ICD.Common.Utils.Xml; using NUnit.Framework; namespace ICD.Common.Utils.Tests.Xml { [TestFixture] public sealed class GenericXmlConverterTest { [XmlConverter(typeof(TestClassConverter))] private sealed class TestClass { public string A { get; set; } public int B { get; set; } } private sealed class TestClassConverter : AbstractGenericXmlConverter { private const string ELEMENT_A = "A"; private const string ATTRIBUTE_B = "b"; /// /// Override to handle the current attribute. /// /// /// protected override void ReadAttribute(IcdXmlReader reader, TestClass instance) { switch (reader.Name) { case ATTRIBUTE_B: instance.B = int.Parse(reader.Value); break; default: base.ReadAttribute(reader, instance); break; } } /// /// Override to handle the current element. /// /// /// protected override void ReadElement(IcdXmlReader reader, TestClass instance) { switch (reader.Name) { case ELEMENT_A: instance.A = reader.ReadElementContentAsString(); break; default: base.ReadElement(reader, instance); break; } } /// /// Override to write attributes to the root element. /// /// /// protected override void WriteAttributes(IcdXmlTextWriter writer, TestClass value) { base.WriteAttributes(writer, value); writer.WriteAttributeString(ATTRIBUTE_B, value.B.ToString()); } /// /// Override to write elements to the writer. /// /// /// protected override void WriteElements(IcdXmlTextWriter writer, TestClass value) { base.WriteElements(writer, value); writer.WriteElementString(ELEMENT_A, value.A); } } [Test] public void TestElement() { const string xml = @" Test "; using (IcdXmlReader reader = new IcdXmlReader(xml)) { // Read into the Instances node reader.Read(); Assert.AreEqual(XmlNodeType.Element, reader.NodeType); Assert.AreEqual("Instances", reader.Name); // Read into the Instance node reader.Read(); reader.SkipInsignificantWhitespace(); Assert.AreEqual(XmlNodeType.Element, reader.NodeType); Assert.AreEqual("Instance", reader.Name); // Deserialize TestClass instance = IcdXmlConvert.DeserializeObject(reader); Assert.IsNotNull(instance); Assert.AreEqual("Test", instance.A); Assert.AreEqual(1, instance.B); // Deserialization should land on the following node reader.SkipInsignificantWhitespace(); Assert.AreEqual(XmlNodeType.EndElement, reader.NodeType); Assert.AreEqual("Instances", reader.Name); } } [Test] public void TestEmptyElement() { const string xml = @" "; using (IcdXmlReader reader = new IcdXmlReader(xml)) { // Read into the Instances node reader.Read(); Assert.AreEqual(XmlNodeType.Element, reader.NodeType); Assert.AreEqual("Instances", reader.Name); // Read into the Instance node reader.Read(); reader.SkipInsignificantWhitespace(); Assert.AreEqual(XmlNodeType.Element, reader.NodeType); Assert.AreEqual("Instance", reader.Name); // Deserialize TestClass instance = IcdXmlConvert.DeserializeObject(reader); Assert.IsNotNull(instance); Assert.IsNull(instance.A); Assert.AreEqual(1, instance.B); // Deserialization should land on the following node reader.SkipInsignificantWhitespace(); Assert.AreEqual(XmlNodeType.EndElement, reader.NodeType); Assert.AreEqual("Instances", reader.Name); } } } }