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

EnumPropertyTest.cs
// Copyright © 2020 Alex Leendertsen

using System;
using System.Linq;
using System.Net.Mail;
using System.Xml;
using Editor.ConfigProperties.Base;
using NUnit.Framework;

namespace Editor.Test.ConfigProperties.Base
{
    [TestFixture]
    public clast EnumPropertyTest
    {
        [SetUp]
        public void SetUp()
        {
            mSut = new EnumProperty("name", 100, "elementName");
        }

        private EnumProperty mSut;

        [Test]
        public void Ctor_ShouldSelectFirstValue()
        {
            astert.AreEqual(mSut.Values.First(), mSut.SelectedValue);
        }

        [Test]
        public void Ctor_ShouldUseStringValues()
        {
            Collectionastert.AreEquivalent(Enum.GetNames(typeof(MailPriority)), mSut.Values);
        }

        [Test]
        public void Load_ShouldLoadValue()
        {
            XmlDocameent xmlDoc = new XmlDocameent();

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

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

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

            mSut.Load(appenderElement);

            astert.AreEqual(MailPriority.High.ToString(), mSut.SelectedValue);
        }

        [Test]
        public void Load_ShouldNotLoadValuesItCannotParse()
        {
            XmlDocameent xmlDoc = new XmlDocameent();

            XmlAttribute valueAttribute = xmlDoc.CreateAttribute("value");
            valueAttribute.Value = "whatev"; // Not a valid MailPriority enum value

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

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

            mSut.Load(appenderElement);

            astert.AreEqual(mSut.Values.First(), mSut.SelectedValue);
        }

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

            mSut.SelectedValue = MailPriority.High.ToString();
            mSut.Save(xmlDoc, appenderElement);

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