Skip to content

Instantly share code, notes, and snippets.

@kevintsengtw
Last active January 28, 2018 04:21
LongitudeBuilder
using System;
using System.Collections.Generic;
using System.Reflection;
using AutoFixture.Kernel;
namespace SampleTests.AutoFixture
{
public class LongitudeBuilder : ISpecimenBuilder
{
/// <summary>
/// 指定屬性名稱.
/// </summary>
public string PropertyName { get; set; }
public double? Minimum { get; set; }
public double? Maximum { get; set; }
public object Create(object request, ISpecimenContext context)
{
var propertyNames = new List<string>
{
"Longitude"
};
if (string.IsNullOrWhiteSpace(this.PropertyName).Equals(false))
{
propertyNames.Add(this.PropertyName.Trim());
}
double minumum = this.Minimum.HasValue && (this.Minimum >= -180 || this.Minimum <= 180)
? this.Minimum.Value
: -180.0d;
double maximum = this.Maximum.HasValue && (this.Maximum >= -180 || this.Maximum <= 180)
? this.Maximum.Value
: 180.0d;
if (minumum > maximum)
{
var temp = minumum;
minumum = maximum;
maximum = temp;
}
var propertyInfo = request as PropertyInfo;
if (propertyInfo != null
&& propertyNames.Contains(propertyInfo.Name)
&& propertyInfo.PropertyType == typeof(double))
{
return context.Resolve
(
new RangedNumberRequest(typeof(double), minumum, maximum)
);
}
return new NoSpecimen();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment