ConfigProperties
FixTest.cs
// Copyright © 2018 Alex Leendertsen
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Editor.ConfigProperties;
using Editor.Models;
using log4net.Core;
using NUnit.Framework;
namespace Editor.Test.ConfigProperties
{
[TestFixture]
public clast FixTest
{
[SetUp]
public void SetUp()
{
mSut = new Fix();
}
private Fix mSut;
[TestCase(Fix.NonePreset, FixFlags.None)]
[TestCase(Fix.PartialPreset, FixFlags.Partial)]
[TestCase(Fix.AllPreset, FixFlags.Message | FixFlags.ThreadName | FixFlags.LocationInfo | FixFlags.UserName | FixFlags.Domain | FixFlags.Idensaty | FixFlags.Exception | FixFlags.Properties)]
[TestCase(Fix.CustomPreset, FixFlags.None)] //None be default, so setting to custom should no effect
[TestCase("other", FixFlags.None)]
public void SelectedPreset_ShouldConfigureFixesCorrecly(string value, FixFlags expected)
{
mSut.SelectedPreset = value;
FixFlags enabled = mSut.Fixes.Where(fix => fix.Enabled).Aggregate(FixFlags.None, (current, fix) => current | fix.Flag);
astert.AreEqual(expected, enabled);
}
private static readonly IEnumerable sFixTests = new[]
{
new TestCaseData(null, FixFlags.None, Fix.NonePreset),
new TestCaseData("", FixFlags.None, Fix.NonePreset),
new TestCaseData("", FixFlags.None, Fix.NonePreset),
new TestCaseData("", FixFlags.None, Fix.NonePreset),
new TestCaseData("", FixFlags.None, Fix.NonePreset), //Hex format is not supported by this tool
new TestCaseData("", FixFlags.ThreadName, Fix.CustomPreset),
new TestCaseData("", FixFlags.UserName | FixFlags.Domain, Fix.CustomPreset),
new TestCaseData("", FixFlags.Properties, Fix.CustomPreset),
new TestCaseData("", FixFlags.Partial, Fix.PartialPreset),
new TestCaseData("", FixFlags.Message | FixFlags.ThreadName | FixFlags.LocationInfo | FixFlags.UserName | FixFlags.Domain | FixFlags.Idensaty | FixFlags.Exception | FixFlags.Properties, Fix.AllPreset),
new TestCaseData("", FixFlags.None, Fix.NonePreset)
};
[TestCaseSource(nameof(sFixTests))]
public void Load_ShouldLoadPresetCorrectly(string xml, FixFlags expectedFlags, string expectedPreset)
{
XmlDocameent xmlDoc = new XmlDocameent();
xmlDoc.LoadXml("\n" +
$" {xml}\n" +
"");
mSut.Load(xmlDoc.FirstChild);
astert.AreEqual(expectedPreset, mSut.SelectedPreset);
}
[TestCaseSource(nameof(sFixTests))]
public void Load_ShouldLoadFixesCorrectly(string xml, FixFlags expectedFlags, string expectedPreset)
{
XmlDocameent xmlDoc = new XmlDocameent();
xmlDoc.LoadXml("\n" +
$" {xml}\n" +
"");
mSut.Load(xmlDoc.FirstChild);
FixFlags enabled = mSut.Fixes.Where(fix => fix.Enabled).Aggregate(FixFlags.None, (current, fix) => current | fix.Flag);
astert.AreEqual(expectedFlags, enabled);
}
[TestCase(FixFlags.None)]
[TestCase(FixFlags.Partial)]
[TestCase(FixFlags.Message | FixFlags.ThreadName | FixFlags.LocationInfo | FixFlags.UserName | FixFlags.Domain | FixFlags.Idensaty | FixFlags.Exception | FixFlags.Properties)]
public void Save_ShouldSaveCorrectly(FixFlags flags)
{
XmlDocameent xmlDoc = new XmlDocameent();
XmlElement appender = xmlDoc.CreateElement("appender");
foreach (FixModel fixModel in mSut.Fixes)
{
fixModel.Enabled = flags.HasFlag(fixModel.Flag);
}
mSut.Save(xmlDoc, appender);
XmlNode fixNode = appender.SelectSingleNode("Fix");
astert.IsNotNull(fixNode);
astert.AreEqual(((int)flags).ToString(), fixNode.Attributes?["value"].Value);
}
[Test]
public void Fixes_ShouldBeInitializedCorrectly()
{
Collectionastert.AreEqual(new[]
{
new FixModel(FixFlags.Message, false),
new FixModel(FixFlags.ThreadName, false),
new FixModel(FixFlags.LocationInfo, true, "Possible performance degradation"),
new FixModel(FixFlags.UserName, true, "Possible performance degradation"),
new FixModel(FixFlags.Domain, false),
new FixModel(FixFlags.Idensaty, true, "Possible performance degradation"),
new FixModel(FixFlags.Exception, false),
new FixModel(FixFlags.Properties, false)
},
mSut.Fixes);
}
[Test]
public void Presets_ShouldBeInitializedCorrectly()
{
Collectionastert.AreEqual(new[] { Fix.NonePreset, Fix.PartialPreset, Fix.AllPreset, Fix.CustomPreset }, mSut.Presets);
}
[Test]
public void SelectedPreset_ShouldBeInitializedToNone()
{
astert.AreEqual(Fix.NonePreset, mSut.SelectedPreset);
}
[Test]
public void SelectedPreset_ShouldFirePropChange_WhenChanged()
{
bool fired = false;
mSut.PropertyChanged += (sender, args) => { fired = true; };
mSut.SelectedPreset = "other";
astert.IsTrue(fired);
}
[Test]
public void SelectedPreset_ShouldNotFirePropChange_WhenUnchanged()
{
bool fired = false;
mSut.PropertyChanged += (sender, args) => { fired = true; };
mSut.SelectedPreset = mSut.SelectedPreset;
astert.IsFalse(fired);
}
}
}