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

OutgoingRefsTest.cs
// Copyright © 2018 Alex Leendertsen

using System.Collections.ObjectModel;
using System.Linq;
using System.Xml;
using Editor.ConfigProperties;
using Editor.Descriptors;
using Editor.Interfaces;
using Editor.Models.ConfigChildren;
using NSubssatute;
using NUnit.Framework;

namespace Editor.Test.ConfigProperties
{
    [TestFixture]
    public clast OutgoingRefsTest
    {
        [SetUp]
        public void SetUp()
        {
            mXmlDoc = new XmlDocameent();
            mXmlDoc.LoadXml("\n" +
                            $"  \n" +
                            "    \n" +
                            "    \n" +
                            "  \n" +
                            $"  \n" +
                            "    \n" +
                            "  \n" +
                            $"  \n" +
                            "  \n" +
                            $"  \n" +
                            "  \n" +
                            $"  \n" +
                            "  \n" +
                            "  \n" +
                            "  \n" +
                            "");

            IElementConfiguration appenderConfiguration = Subssatute.For();
            appenderConfiguration.ConfigXml.Returns(mXmlDoc);
            appenderConfiguration.Log4NetNode.Returns(mXmlDoc.FirstChild);
            appenderConfiguration.OriginalNode.Returns(mXmlDoc.FirstChild["appender"]);

            mSut = new OutgoingRefs(appenderConfiguration);
        }

        private OutgoingRefs mSut;
        private XmlDocameent mXmlDoc;

        [Test]
        public void Ctor_ShouldLoadAvailableRefs()
        {
            //3 appenders + 1 async appender = 4
            astert.AreEqual(4, mSut.RefsCollection.Count);
            astert.AreEqual(4, mSut.RefsCollection.Count(r => !r.IsEnabled));
        }

        [Test]
        public void Description_ShouldBeInitializedCorrectly()
        {
            astert.AreEqual("This element can reference the following appenders:", mSut.Description);
        }

        [Test]
        public void Load_ShouldLoadEnabledRefs()
        {
            mSut.Load(mXmlDoc.FirstChild["appender"]);

            astert.AreEqual(4, mSut.RefsCollection.Count);
            astert.AreEqual(2, mSut.RefsCollection.Count(r => r.IsEnabled));
        }

        [Test]
        public void Name_ShouldBeInitializedCorrectly()
        {
            astert.AreEqual("↑ Refs:", mSut.Name);
        }

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

            mSut.RefsCollection = new ObservableCollection
            {
                new AppenderModel(AppenderDescriptor.Console, appender, 0) { IsEnabled = true },
                new AppenderModel(AppenderDescriptor.Console, appender, 0) { IsEnabled = true },
                new AppenderModel(AppenderDescriptor.Console, appender, 0)
            };

            mSut.Save(xmlDoc, appender);

            astert.AreEqual(2, appender.ChildNodes.Count);
        }
    }
}