Last active
January 28, 2018 04:21
LongitudeBuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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