Skip to content

Instantly share code, notes, and snippets.

@RhysC
Created March 5, 2013 10:41
Show Gist options
  • Save RhysC/5089413 to your computer and use it in GitHub Desktop.
Save RhysC/5089413 to your computer and use it in GitHub Desktop.
Simple attribute testing on MVC ViewModel
public class PesonalDetailsFixture
{
[Fact]
public void RequiredPropertiesAreMarkedAsSo()
{
PropertyOn<PersonalData, string>(x => x.Title).Has<RequiredAttribute>();
PropertyOn<PersonalData, string>(x => x.FirstName).Has<RequiredAttribute>();
PropertyOn<PersonalData, string>(x => x.LastName).Has<RequiredAttribute>();
PropertyOn<PersonalData, DateTime>(x => x.DateOfBirth).Has<RequiredAttribute>();
PropertyOn<PersonalData, string>(x => x.Email).Has<RequiredAttribute>();
PropertyOn<PersonalData, string>(x => x.MobilePhone).Has<RequiredAttribute>();
}
[Fact]
public void NonRequiredPropertiesAreNotMarkedAsRequired()
{
//If these are not required, why do we capture them? i.e. what business descision/action will be taken by having this data?
PropertyOn<PersonalData, string>(x => x.MiddleName).DoesNotHave<RequiredAttribute>();
PropertyOn<PersonalData, string>(x => x.ContactPhone).DoesNotHave<RequiredAttribute>();
}
[Fact]
public void DateOfBirthIsDescribedAsADate()
{
PropertyOn<PersonalData, DateTime>(x => x.DateOfBirth)
.Has<DataTypeAttribute>()
.With(d => d.DataType == DataType.Date);//ie not datetime or time
}
[Fact]
public void EmailIsDescribedAsAnEmailField()
{
PropertyOn<PersonalData, string>(x => x.Email)
.Has<DataTypeAttribute>()
.With(d => d.DataType == DataType.EmailAddress);
}
[Fact]
public void PhonenumbersAreDescribedAsPhoneNumbers()
{
PropertyOn<PersonalData, string>(x => x.MobilePhone)
.Has<DataTypeAttribute>()
.With(d => d.DataType == DataType.PhoneNumber);
PropertyOn<PersonalData, string>(x => x.ContactPhone)
.Has<DataTypeAttribute>()
.With(d => d.DataType == DataType.PhoneNumber);
}
public PropertyInfo PropertyOn<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda)
{
var type = typeof(TSource);
var memberExpression = propertyLambda.Body as MemberExpression;
if (memberExpression == null)
throw new ArgumentException(string.Format("Expression '{0}' refers to a method, not a property.", propertyLambda));
var propInfo = memberExpression.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentException(string.Format("Expression '{0}' refers to a field, not a property.", propertyLambda));
if (type != propInfo.ReflectedType &&
!type.IsSubclassOf(propInfo.ReflectedType))
throw new ArgumentException(string.Format("Expresion '{0}' refers to a property that is not from type {1}.", propertyLambda, type));
return propInfo;
}
}
public static class PropertyInfoEx
{
public static T Has<T>(this PropertyInfo propInfo) where T : Attribute
{
//non need to deal with multiple attributations at the moment.
var att = propInfo.GetCustomAttributes(typeof(T), true).Single();
Assert.NotNull(att);
return att as T;
}
public static void DoesNotHave<T>(this PropertyInfo propInfo) where T : Attribute
{
Assert.False(propInfo.GetCustomAttributes(typeof(T), true).Any());
}
public static void With<T>(this T obj, Predicate<T> test)
{
Assert.True(test(obj));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment