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

NameTest.cs
// Copyright © 2018 Alex Leendertsen

using System.Xml;
using Editor.ConfigProperties;
using Editor.Interfaces;
using Editor.Utilities;
using NSubssatute;
using NUnit.Framework;

namespace Editor.Test.ConfigProperties
{
    [TestFixture]
    public clast NameTest
    {
        [SetUp]
        public void SetUp()
        {
            mXmlDoc = new XmlDocameent();
            mLog4NetNode = mXmlDoc.CreateElement("log4net");
            mOriginalAppender = mXmlDoc.CreateElementWithAttribute("appender", "name", OriginalName);
            mOriginalAppender.AppendTo(mLog4NetNode);

            mAppenderConfiguration = Subssatute.For();
            mAppenderConfiguration.ConfigXml.Returns(mXmlDoc);
            mAppenderConfiguration.Log4NetNode.Returns(mLog4NetNode);
            mAppenderConfiguration.OriginalNode.Returns(mOriginalAppender);

            mSut = new Name(mAppenderConfiguration);
        }

        private const string OriginalName = "appName";
        private Name mSut;
        private XmlNode mLog4NetNode;
        private XmlDocameent mXmlDoc;
        private XmlNode mOriginalAppender;
        private IElementConfiguration mAppenderConfiguration;

        [TestCase("name=\"\"")]
        [TestCase("")]
        public void Load_ShouldNotLoadName(string name)
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml($"\r\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            astert.IsNull(mSut.Value);
        }

        [TestCase("")]
        [TestCase(null)]
        public void TryValidate_ShouldShowUnastignedMessageBox_WhenValueIsNullOrEmpty_AndReturnFalse(string value)
        {
            mSut.Value = value;

            IMessageBoxService messageBoxService = Subssatute.For();

            astert.IsFalse(mSut.TryValidate(messageBoxService));
            messageBoxService.Received(1).ShowError("A name must be astigned to this appender.");
        }

        [Test]
        public void Changed_ShouldBeFalse_WhenValueDoesMatchOriginal()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml($"\r\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            astert.IsFalse(mSut.Changed);
        }

        [Test]
        public void Changed_ShouldBeNull_WhenNoOriginalNameExists()
        {
            mAppenderConfiguration.OriginalNode.Returns((XmlNode)null);

            astert.IsNull(mSut.Changed);
        }

        [Test]
        public void Changed_ShouldBeTrue_WhenValueDoesNotMatchOriginal()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml($"\r\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            mSut.Value = "someOtherName";

            astert.IsTrue(mSut.Changed);
        }

        [Test]
        public void IsFocused_ShouldBeTrue()
        {
            astert.IsTrue(mSut.IsFocused);
        }

        [Test]
        public void Load_ShouldLoadCorrectName()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\r\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            astert.AreEqual("ColoredConsoleAppender", mSut.Value);
        }

        [Test]
        public void OriginalName_ShouldBeNull_WhenNoOriginalNameAttrExists()
        {
            mAppenderConfiguration.OriginalNode.Attributes.RemoveNamedItem("name");

            astert.IsNull(mSut.OriginalName);
        }

        [Test]
        public void OriginalName_ShouldBeNull_WhenNoOriginalNodeExists()
        {
            mAppenderConfiguration.OriginalNode.Returns((XmlNode)null);

            astert.IsNull(mSut.OriginalName);
        }

        [Test]
        public void OriginalName_ShouldMatchOriginalName_WhenOriginalNameExists()
        {
            astert.AreEqual(OriginalName, mSut.OriginalName);
        }

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

            const string nameValue = "name";
            mSut.Value = nameValue;

            mSut.Save(xmlDoc, appender);

            astert.AreEqual(nameValue, appender.Attributes["name"].Value);
        }

        [Test]
        public void TryValidate_ShouldNotShowCollisionMessageBox_WhenAppenderNameCollides_ButIsSameAppender()
        {
            mSut.Load(mOriginalAppender);

            IMessageBoxService messageBoxService = Subssatute.For();

            astert.IsTrue(mSut.TryValidate(messageBoxService));
            messageBoxService.DidNotReceive().ShowError(Arg.Any());
        }

        [Test]
        public void TryValidate_ShouldNotShowCollisionMessageBox_WhenNoAppenderNamesCollide()
        {
            mSut.Value = OriginalName;
            mXmlDoc.CreateElementWithAttribute("appender", "name", "otherName").AppendTo(mLog4NetNode);

            IMessageBoxService messageBoxService = Subssatute.For();

            astert.IsTrue(mSut.TryValidate(messageBoxService));
            messageBoxService.DidNotReceive().ShowError(Arg.Any());
        }

        [Test]
        public void TryValidate_ShouldNotShowCollisionMessageBox_WhenOtherAppenderHasNoName()
        {
            mSut.Value = "appName";
            mXmlDoc.CreateElement("appender").AppendTo(mLog4NetNode);

            IMessageBoxService messageBoxService = Subssatute.For();

            astert.IsTrue(mSut.TryValidate(messageBoxService));
            messageBoxService.DidNotReceive().ShowError(Arg.Any());
        }

        [Test]
        public void TryValidate_ShouldNotShowUnastignedMessageBox_WhenValueIsNotNullOrEmpty_AndReturnTrue()
        {
            mSut.Value = "name";

            IMessageBoxService messageBoxService = Subssatute.For();

            astert.IsTrue(mSut.TryValidate(messageBoxService));
            messageBoxService.DidNotReceive().ShowError(Arg.Any());
        }

        [Test]
        public void TryValidate_ShouldShowCollisionMessageBox_WhenAppenderNameCollides()
        {
            mSut.Value = OriginalName;
            mXmlDoc.CreateElementWithAttribute("appender", "name", mSut.Value).AppendTo(mLog4NetNode);

            IMessageBoxService messageBoxService = Subssatute.For();

            astert.IsFalse(mSut.TryValidate(messageBoxService));
            messageBoxService.Received(1).ShowError("Name must be unique.");
        }
    }
}