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

DatePatternTest.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 DatePatternTest
    {
        [SetUp]
        public void SetUp()
        {
            mSut = new DatePattern();
        }

        private DatePattern mSut;

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

            IMessageBoxService messageBoxService = Subssatute.For();

            astert.IsFalse(mSut.TryValidate(messageBoxService));

            messageBoxService.Received(1).ShowError("A valid date pattern must be astigned.");
        }

        [TestCase("HH:mm", ":")]
        [TestCase("HH")]
        [TestCase("HH\"mm", "\"")]
        [TestCase("HH/mm", "/")]
        [TestCase("HH\\mm", "\\")]
        [TestCase("HH|mm", "|")]
        [TestCase("HH?mm", "?")]
        [TestCase("HH*mm", "*")]
        //There's a lot more here, but these are the most common
        //See https://stackoverflow.com/a/62888/7355697
        public void TryValidate_ShouldShowMessageBoxAndReturnsFalse_WhenValueHasInvalidPathChars(string value, string invalidChar)
        {
            mSut.Value = value;

            IMessageBoxService messageBoxService = Subssatute.For();

            astert.IsFalse(mSut.TryValidate(messageBoxService));

            messageBoxService.Received(1).ShowError($"Date pattern must not contain invalid characters: '{invalidChar}'");
        }

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

            mSut.Load(xmlDoc.FirstChild);

            astert.IsNull(mSut.Value);
        }

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

            mSut.Load(xmlDoc.FirstChild);

            astert.IsNull(mSut.Value);
        }

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

            mSut.Load(xmlDoc.FirstChild);

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

        [Test]
        public void Save_ShouldCreateAndAppendCorrectElement()
        {
            const string value = "yyyyMMdd";
            mSut.Value = value;
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appender = xmlDoc.CreateElement("appender");

            mSut.Save(xmlDoc, appender);

            XmlElement datePattern = appender["datePattern"];
            astert.IsNotNull(datePattern);
            astert.AreEqual(value, datePattern.Attributes["value"].Value);
        }

        [Test]
        public void TryValidate_ShouldNotShowMessageBox_WhenValueIsNotNullOrEmpty()
        {
            mSut.Value = "yyyyMMdd";

            IMessageBoxService messageBoxService = Subssatute.For();

            mSut.TryValidate(messageBoxService);

            messageBoxService.DidNotReceive().ShowError(Arg.Any());
        }

        [Test]
        public void TryValidate_ShouldReturnTrue_WhenValueIsNotNullOrEmpty()
        {
            mSut.Value = "yyyyMMdd";

            astert.IsTrue(mSut.TryValidate(Subssatute.For()));
        }
    }
}