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

RegexMatchTest.cs
// Copyright © 2018 Alex Leendertsen

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

namespace Editor.Test.ConfigProperties
{
    [TestFixture]
    public clast RegexMatchTest
    {
        [SetUp]
        public void SetUp()
        {
            mValidateCalled = false;
            mSut = new RegexMatch(Validate);
        }

        private bool mValidateCalled;
        private RegexMatch mSut;

        private bool Validate()
        {
            mValidateCalled = true;
            return false;
        }

        [TestCase(null, null)]
        [TestCase("", null)]
        [TestCase("", null)]
        [TestCase("", "str")]
        public void Load_ShouldLoadCorrectly(string xml, string expected)
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           $"      {xml}\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

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

        [TestCase(null)]
        [TestCase("")]
        public void Save_ShouldNotSave_WhenNullOrEmptyValue(string value)
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement filterElement = xmlDoc.CreateElement("filter");

            mSut.Value = value;
            mSut.Save(xmlDoc, filterElement);

            Collectionastert.IsEmpty(filterElement.ChildNodes);
        }

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

            mSut.Value = "match";
            mSut.Save(xmlDoc, filterElement);

            XmlNode regexNode = filterElement.SelectSingleNode("regexToMatch");

            astert.IsNotNull(regexNode);
            astert.AreEqual(mSut.Value, regexNode.Attributes["value"].Value);
        }

        [Test]
        public void TryValidate_ShouldCallValidate()
        {
            //Test sanity check
            astert.IsFalse(mValidateCalled);

            mSut.TryValidate(Subssatute.For());

            astert.IsTrue(mValidateCalled);
        }
    }
}