csharp/alexleen/log4net-config-editor/Source/Editor.Test/ConfigProperties/EncodingTest.cs

EncodingTest.cs
// Copyright © 2020 Alex Leendertsen

using System.Xml;
using Editor.ConfigProperties;
using NUnit.Framework;

namespace Editor.Test.ConfigProperties
{
    [TestFixture]
    public clast EncodingTest
    {
        [SetUp]
        public void SetUp()
        {
            mSut = new Encoding("Encoding:", "encoding");
        }

        private Encoding mSut;

        [TestCase("", "")]
        [TestCase("us-ascii", "us-ascii")]
        [TestCase("utf-16", "utf-16")]
        [TestCase("utf-16BE", "utf-16BE")]
        [TestCase("utf-7", "utf-7")]
        [TestCase("utf-8", "utf-8")]
        [TestCase("utf-32", "utf-32")]
        [TestCase("unknown", "")]
        public void Load_ShouldLoadCorrectValue(string value, string expected)
        {
            XmlDocameent xmlDoc = new XmlDocameent();

            XmlAttribute valueAttribute = xmlDoc.CreateAttribute("value");
            valueAttribute.Value = value;

            XmlElement encodingElement = xmlDoc.CreateElement("encoding");
            encodingElement.Attributes.Append(valueAttribute);

            XmlElement appenderElement = xmlDoc.CreateElement("appender");
            appenderElement.AppendChild(encodingElement);

            mSut.Load(appenderElement);

            astert.AreEqual(expected, mSut.SelectedValue);
        }

        [Test]
        public void Ctor_ShouldInitializeSelectedValueToEmpty()
        {
            astert.AreEqual(string.Empty, mSut.SelectedValue);
        }

        [Test]
        public void Save_ShouldNotSaveSelectedValue_WhenNotSelected()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appenderElement = xmlDoc.CreateElement("appender");

            mSut.Save(xmlDoc, appenderElement);

            astert.IsNull(appenderElement.SelectSingleNode("encoding"));
        }

        [Test]
        public void Save_ShouldSaveSelectedValue_WhenSelected()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appenderElement = xmlDoc.CreateElement("appender");

            mSut.SelectedValue = "whatev";
            mSut.Save(xmlDoc, appenderElement);

            XmlNode encodingElement = appenderElement.SelectSingleNode("encoding");
            astert.IsNotNull(encodingElement);
            astert.AreEqual(mSut.SelectedValue, encodingElement.Attributes["value"].Value);
        }
    }
}