using System; using Prism; using UnitTestSharp; namespace Konis.UnitTests { public class DefsTests : TestFixture { public static class GoodDefs { public static readonly GradientDef First = new GradientDef { Gradient = Gradient.Constant(Colors.Red) }; public static readonly GradientDef Second = new GradientDef { Gradient = Gradient.Constant(Colors.Blue) }; } public static class NoDefs { public static readonly int Count = 3; public static readonly string Label = "not a def"; } public static class PrivateDef { private static readonly GradientDef Hidden = new GradientDef(); } public static class NullDef { public static readonly GradientDef Missing = null; } public static class BrokenDef { public static readonly GradientDef Fine = new GradientDef { Gradient = Gradient.Constant(Colors.Red) }; public static readonly GradientDef Broken = new GradientDef(); } public class InstanceDef { public GradientDef Broken = new GradientDef(); } public class BaseDefs { public static readonly GradientDef Broken = new GradientDef(); } public class DerivedDefs : BaseDefs { public static readonly GradientDef Fine = new GradientDef { Gradient = Gradient.Constant(Colors.Red) }; } public static InvalidOperationException ThrownBy(Type owner) { try { Defs.ValidateStatics(owner); } catch (InvalidOperationException exception) { return exception; } return null; } public class ValidateStaticsTests : TestFixture { public void CountsEveryDefField() { CheckEqual(2, Defs.ValidateStatics(typeof(GoodDefs))); } public void IgnoresFieldsThatAreNotDefs() { CheckEqual(0, Defs.ValidateStatics(typeof(NoDefs))); } public void IgnoresInstanceFields() { CheckEqual(0, Defs.ValidateStatics(typeof(InstanceDef))); } public void IgnoresFieldsDeclaredOnABase() { CheckEqual(1, Defs.ValidateStatics(typeof(DerivedDefs))); } public void ValidatesAPrivateField() { CheckNotNull(ThrownBy(typeof(PrivateDef))); } public void AFailingDef_ThrowsNamingTheField() { CheckTrue(ThrownBy(typeof(BrokenDef)).Message.Contains("BrokenDef.Broken")); } public void AFailingDef_KeepsTheDefsOwnExceptionAsTheInnerOne() { CheckEqual("A gradient def needs a gradient.", ThrownBy(typeof(BrokenDef)).InnerException.Message); } public void ANullDef_ThrowsNamingTheField() { CheckTrue(ThrownBy(typeof(NullDef)).Message.Contains("NullDef.Missing")); } public void ANullOwner_Throws() { CheckThrow(typeof(ArgumentNullException)); Defs.ValidateStatics(null); } } } }