Skip to content

Instantly share code, notes, and snippets.

@jackawatts
Last active December 7, 2016 11:10
Show Gist options
  • Save jackawatts/f66ee7e239dc165955f889936ca32dfd to your computer and use it in GitHub Desktop.
Save jackawatts/f66ee7e239dc165955f889936ca32dfd to your computer and use it in GitHub Desktop.
DateTime Conventional
using System;
using System.Linq;
using Conventional;
using Conventional.Conventions;
namespace ConventionTests
{
public class PropertyNamesOfTypeMustEndWith : ConventionSpecification
{
private readonly Type _type;
private readonly string[] _suffixes;
public PropertyNamesOfTypeMustEndWith(Type type, params string[] suffixes)
{
_type = type;
_suffixes = suffixes;
}
public override ConventionResult IsSatisfiedBy(Type type)
{
var failedProperties = type.GetProperties()
.Where(p => p.PropertyType == _type && _suffixes.All(s => !p.Name.EndsWith(s)))
.Select(p => p.Name)
.ToArray();
return failedProperties.Any()
? ConventionResult.NotSatisfied(type.FullName, string.Format(FailureMessage, string.Join(",", failedProperties), string.Join(" OR ", _suffixes)))
: ConventionResult.Satisfied(type.FullName);
}
protected override string FailureMessage => "Properties {0} name does not end with {1}";
}
[TestFixture]
public class ConventionsForDateTime
{
[Test]
public void DateTimePropertiesMustEndWithDateOrUtc()
{
var types = typeof(AccountReservedMessage).Assembly.GetExportedTypes();
types.MustConformTo(new PropertyNamesOfTypeMustEndWith(typeof(DateTime), "Date", "Utc"))
.WithFailureAssertion(Assert.Fail);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment