using System.Linq; using System.Xml.Linq; using Developpez.Dotnet.Xml; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Xml { [TestFixture] public class XmlExtensionTests { [Test] public void Test_SafeValue_Element_NoDefault() { var root = new XElement("Root", new XElement("Foo", "Hello world")); string expected = "Hello world"; string actual = root.Element("Foo").SafeValue(); Assert.AreEqual(expected, actual); expected = null; actual = root.Element("Bar").SafeValue(); Assert.AreEqual(expected, actual); } [Test] public void Test_SafeValue_Element_WithDefault() { var root = new XElement("Root", new XElement("Foo", "Hello world")); const string defaultValue = "nothing"; string expected = "Hello world"; string actual = root.Element("Foo").SafeValue(defaultValue); Assert.AreEqual(expected, actual); expected = defaultValue; actual = root.Element("Bar").SafeValue(defaultValue); Assert.AreEqual(expected, actual); } [Test] public void Test_SafeValue_Attribute_NoDefault() { var root = new XElement("Root", new XAttribute("Foo", "Hello world")); string expected = "Hello world"; string actual = root.Attribute("Foo").SafeValue(); Assert.AreEqual(expected, actual); expected = null; actual = root.Attribute("Bar").SafeValue(); Assert.AreEqual(expected, actual); } [Test] public void Test_SafeValue_Attribute_WithDefault() { var root = new XElement("Root", new XAttribute("Foo", "Hello world")); const string defaultValue = "nothing"; string expected = "Hello world"; string actual = root.Attribute("Foo").SafeValue(defaultValue); Assert.AreEqual(expected, actual); expected = defaultValue; actual = root.Attribute("Bar").SafeValue(defaultValue); Assert.AreEqual(expected, actual); } [Test] public void Test_SafeElement() { var root = new XElement("Root", new XElement("Foo", new XElement("Baz", "Hello world"))); string expected = "Hello world"; string actual = root.Element("Foo").SafeElement("Baz").SafeValue(); Assert.AreEqual(expected, actual); expected = null; actual = root.Element("Bar").SafeElement("Baz").SafeValue(); Assert.AreEqual(expected, actual); } [Test] public void Test_SafeElements_NoName() { var root = new XElement("Root", new XElement("Foo", new XElement("Baz1", "abc"), new XElement("Baz2", "def"))); var expected = new[] { "abc", "def" }; var actual = root.Element("Foo").SafeElements().Select(e => e.Value); CollectionAssert.AreEqual(expected, actual); expected = new string[0]; actual = root.Element("Bar").SafeElements().Select(e => e.Value); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_SafeElements_WithName() { var root = new XElement("Root", new XElement("Foo", new XElement("Baz1", "abc"), new XElement("Baz2", "def"))); var expected = new[] { "abc" }; var actual = root.Element("Foo").SafeElements("Baz1").Select(e => e.Value); CollectionAssert.AreEqual(expected, actual); expected = new string[0]; actual = root.Element("Bar").SafeElements("Baz1").Select(e => e.Value); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_SafeAttribute() { var root = new XElement("Root", new XElement("Foo", new XAttribute("Baz", "Hello world"))); string expected = "Hello world"; string actual = root.Element("Foo").SafeAttribute("Baz").SafeValue(); Assert.AreEqual(expected, actual); expected = null; actual = root.Element("Bar").SafeAttribute("Baz").SafeValue(); Assert.AreEqual(expected, actual); } [Test] public void Test_SafeAttributes_NoName() { var root = new XElement("Root", new XElement("Foo", new XAttribute("Baz1", "abc"), new XAttribute("Baz2", "def"))); var expected = new[] { "abc", "def" }; var actual = root.Element("Foo").SafeAttributes().Select(e => e.Value); CollectionAssert.AreEqual(expected, actual); expected = new string[0]; actual = root.Element("Bar").SafeAttributes().Select(e => e.Value); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_SafeAttributes_WithName() { var root = new XElement("Root", new XElement("Foo", new XAttribute("Baz1", "abc"), new XAttribute("Baz2", "def"))); var expected = new[] { "abc" }; var actual = root.Element("Foo").SafeAttributes("Baz1").Select(e => e.Value); CollectionAssert.AreEqual(expected, actual); expected = new string[0]; actual = root.Element("Bar").SafeAttributes("Baz1").Select(e => e.Value); CollectionAssert.AreEqual(expected, actual); } } }