public void CreateReturnsValuesFromCorrectSetForRequestsWithDifferentTypesAndSameLimits( Type primaryRequestType, Type otherRequestType) { // Arrange int minimum = 0, maximum = 2; int rangeCount = maximum - minimum + 1; var primaryRequest = new RangedNumberRequest(primaryRequestType, minimum, maximum); var otherRequest = new RangedNumberRequest(otherRequestType, minimum, maximum); var dummyContext = new DelegatingSpecimenContext(); var primaryResults = new List <IComparable>(); var sut = new RandomRangedNumberGenerator(); // Act primaryResults.Add((IComparable)sut.Create(primaryRequest, dummyContext)); primaryResults.Add((IComparable)sut.Create(primaryRequest, dummyContext)); var otherResult = ((IComparable)sut.Create(otherRequest, dummyContext)); primaryResults.Add((IComparable)sut.Create(primaryRequest, dummyContext)); // Verify Assert.True(primaryResults.Distinct().Count() == 3); Assert.InRange(otherResult, minimum, maximum); Assert.True(Enumerable.Range(minimum, rangeCount) .Select(a => Convert.ChangeType(a, primaryRequestType)) .Cast <IComparable>() .All(a => primaryResults.Any(b => b.CompareTo(a) == 0))); }
public void CreateWithPropertyDecoratedWithRangeAttributeReturnsCorrectResult( string name, object attributeMinimum, object attributeMaximum) { // Fixture setup var request = typeof(RangeValidatedType).GetProperty(name); Type target = Nullable.GetUnderlyingType(request.PropertyType) ?? request.PropertyType; var expectedRequest = new RangedNumberRequest( target, Convert.ChangeType(RangeValidatedType.Minimum, target, CultureInfo.CurrentCulture), Convert.ChangeType(RangeValidatedType.Maximum, target, CultureInfo.CurrentCulture) ); var expectedResult = new object(); var context = new DelegatingSpecimenContext { #pragma warning disable 618 OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r) #pragma warning restore 618 }; var sut = new RangeAttributeRelay(); // Exercise system var result = sut.Create(request, context); // Verify outcome Assert.Equal(expectedResult, result); // Teardown }
/// <summary> /// Choose the RandomNumericSequenceGenerator to fulfill the request. Will add the request as a new key /// to generatorMap if the request does not already have a generator for it. Throws ArgumentException /// if either of the limits in the request are non-numeric. /// </summary> /// <param name="request"></param> /// <returns></returns> private RandomNumericSequenceGenerator SelectGenerator(RangedNumberRequest request) { return this.generatorMap.GetOrAdd(request, _ => { return new RandomNumericSequenceGenerator(ConvertLimits(request.Minimum, request.Maximum)); }); }
public void CreateWithFieldDecoratedWithRangeAttributeReturnsCorrectResult( Type attributeType, object attributeMinimum, object attributeMaximum) { // Fixture setup var request = typeof(RangeValidatedType).GetField("Field"); Type target = request.FieldType; var expectedRequest = new RangedNumberRequest( target, Convert.ChangeType(RangeValidatedType.Minimum, target, CultureInfo.CurrentCulture), Convert.ChangeType(RangeValidatedType.Maximum, target, CultureInfo.CurrentCulture) ); var expectedResult = new object(); var context = new DelegatingSpecimenContext { OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r) }; var sut = new RangeAttributeRelay(); // Exercise system var result = sut.Create(request, context); // Verify outcome Assert.Equal(expectedResult, result); // Teardown }
public void CreateWithRangeAttributeRequestReturnsCorrectResult(Type type, object minimum, object maximum) { // Fixture setup var rangeAttribute = new RangeAttribute(type, minimum.ToString(), maximum.ToString()); var providedAttribute = new ProvidedAttribute(rangeAttribute, true); ICustomAttributeProvider request = new FakeCustomAttributeProvider(providedAttribute); Type conversionType = rangeAttribute.OperandType; var expectedRequest = new RangedNumberRequest( conversionType, Convert.ChangeType(rangeAttribute.Minimum, conversionType, CultureInfo.CurrentCulture), Convert.ChangeType(rangeAttribute.Maximum, conversionType, CultureInfo.CurrentCulture) ); var expectedResult = new object(); var context = new DelegatingSpecimenContext { OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r) }; var sut = new RangeAttributeRelay(); // Exercise system var result = sut.Create(request, context); // Verify outcome Assert.Equal(expectedResult, result); // Teardown }
public void ShouldCorrectPassMinimumAndMaximum() { // Arrange var sut = new EnumRangedRequestRelay(); int minimum = 5; int maximum = 10; var request = new RangedRequest(typeof(EnumType), typeof(int), minimum, maximum); RangedNumberRequest capturedNumericRequest = null; var context = new DelegatingSpecimenContext { OnResolve = r => { capturedNumericRequest = (RangedNumberRequest)r; return(new NoSpecimen()); } }; // Act sut.Create(request, context); // Assert Assert.NotNull(capturedNumericRequest); Assert.Equal(minimum, capturedNumericRequest.Minimum); Assert.Equal(maximum, capturedNumericRequest.Maximum); }
public void ShouldRespectUnderlyingEnumType(Type enumType, Type underlyingType) { // Arrange var sut = new EnumRangedRequestRelay(); var request = new RangedRequest(memberType: enumType, operandType: typeof(int), minimum: 1, maximum: 2); RangedNumberRequest capturedNumericRequest = null; var context = new DelegatingSpecimenContext { OnResolve = r => { capturedNumericRequest = (RangedNumberRequest)r; return(new NoSpecimen()); } }; // Act sut.Create(request, context); // Assert Assert.NotNull(capturedNumericRequest); Assert.Equal(underlyingType, capturedNumericRequest.OperandType); Assert.IsType(underlyingType, capturedNumericRequest.Minimum); Assert.IsType(underlyingType, capturedNumericRequest.Maximum); }
/// <summary> /// Choose the RandomNumericSequenceGenerator to fulfill the request. Will add the request as a new key /// to generatorMap if the request does not already have a generator for it. Throws ArgumentException /// if either of the limits in the request are non-numeric. /// </summary> /// <param name="request"></param> /// <returns></returns> private RandomNumericSequenceGenerator SelectGenerator(RangedNumberRequest request) { return(this.generatorMap.GetOrAdd(request, _ => { return new RandomNumericSequenceGenerator(ConvertLimits(request.Minimum, request.Maximum)); })); }
public void ShouldCorrectPassMinimumAndMaximumAsTicks() { // Arrange var sut = new DateTimeRangedRequestRelay(); var request = new RangedRequest(typeof(DateTime), typeof(DateTime), "2020-01-01 00:00:00", "2020-12-31 23:59:59"); RangedNumberRequest capturedNumericRequest = null; var context = new DelegatingSpecimenContext { OnResolve = r => { capturedNumericRequest = (RangedNumberRequest)r; return(new NoSpecimen()); } }; // Act sut.Create(request, context); // Assert Assert.NotNull(capturedNumericRequest); Assert.Equal(new DateTime(2020, 1, 1, 0, 0, 0).Ticks, capturedNumericRequest.Minimum); Assert.Equal(new DateTime(2020, 12, 31, 23, 59, 59).Ticks, capturedNumericRequest.Maximum); }
public void ShouldEmitRandomNumberRequestWithCorrectBoundaries() { // Arrange var min = 10; var max = 20; var rsr = new RangedSequenceRequest(typeof(object), min, max); RangedNumberRequest capturedNumberRequest = null; var context = new DelegatingSpecimenContext { OnResolve = r => { if (r is RangedNumberRequest rnr) { capturedNumberRequest = rnr; } return(new NoSpecimen()); } }; var sut = new RangedSequenceRelay(); // Act sut.Create(rsr, context); // Assert Assert.NotNull(capturedNumberRequest); Assert.Equal(min, capturedNumberRequest.Minimum); Assert.Equal(max, capturedNumberRequest.Maximum); }
/// <inheritdoc /> public object Create(object request, ISpecimenContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var rangedRequest = request as RangedRequest; if (rangedRequest == null) { return(new NoSpecimen()); } if (!rangedRequest.MemberType.IsNumberType()) { return(new NoSpecimen()); } var convertedMinimum = rangedRequest.GetConvertedMinimum(rangedRequest.MemberType); var convertedMaximum = rangedRequest.GetConvertedMaximum(rangedRequest.MemberType); var rangedNumberRequest = new RangedNumberRequest( rangedRequest.MemberType, convertedMinimum, convertedMaximum); return(context.Resolve(rangedNumberRequest)); }
public void ShouldCorrectPassMinimumAndMaximumAsMilliseconds() { // Arrange var sut = new TimeSpanRangedRequestRelay(); var request = new RangedRequest(typeof(TimeSpan), typeof(TimeSpan), "00:00:00", "12:00:00"); RangedNumberRequest capturedNumericRequest = null; var context = new DelegatingSpecimenContext { OnResolve = r => { capturedNumericRequest = (RangedNumberRequest)r; return(new NoSpecimen()); } }; // Act sut.Create(request, context); // Assert Assert.NotNull(capturedNumericRequest); Assert.Equal(0.0, capturedNumericRequest.Minimum); Assert.Equal(12 * 60 * 60 * 1000.0, capturedNumericRequest.Maximum); }
public void CreateReturnsCorrectResultWithMinimumMaximumOnMultipleCall( double minimum, double maximum) { // Fixture setup var numbers = new Random(); var request = new RangedNumberRequest( typeof(double), minimum, maximum); var context = new DelegatingSpecimenContext { OnResolve = r => { Assert.Equal(request.OperandType, r); return((double)numbers.Next()); } }; var sut = new RangedNumberGenerator(); // Exercise system var result = Enumerable .Range(0, 33) .Select(x => sut.Create(request, context)) .Cast <double>(); // Verify outcome Assert.True( result.All(x => x >= minimum && x <= maximum)); // Teardown }
public void SutIsEquatable() { // Arrange // Act var sut = new RangedNumberRequest(typeof(int), 1, 3); // Assert Assert.IsAssignableFrom <IEquatable <RangedNumberRequest> >(sut); }
public void SutIsEquatable() { // Fixture setup // Exercise system var sut = new RangedNumberRequest(typeof(int), 1, 3); // Verify outcome Assert.IsAssignableFrom <IEquatable <RangedNumberRequest> >(sut); // Teardown }
public void CreationOnFullRangeShouldNotFail(Type type, object minimum, object maximum) { // Arrange var request = new RangedNumberRequest(type, minimum, maximum); var sut = new RandomRangedNumberGenerator(); var dummyContext = new DelegatingSpecimenContext(); // Act & Assert Assert.Null(Record.Exception(() => sut.Create(request, dummyContext))); }
public void OperandTypeIsCorrect() { // Fixture setup var expectedOperandType = typeof(int); var sut = new RangedNumberRequest(expectedOperandType, 1, 3); // Exercise system var result = sut.OperandType; // Verify outcome Assert.Equal(expectedOperandType, result); // Teardown }
public void MaximumIsCorrect() { // Arrange var expectedMaximum = 3; var sut = new RangedNumberRequest(typeof(int), 1, expectedMaximum); // Act var result = sut.Maximum; // Assert Assert.Equal(expectedMaximum, result); }
public void SutDoesNotEqualNullSut() { // Arrange var sut = new RangedNumberRequest(typeof(int), 1, 3); RangedNumberRequest other = null; // Act var result = sut.Equals(other); // Assert Assert.False(result, "Equals"); }
public void OperandTypeIsCorrect() { // Arrange var expectedOperandType = typeof(int); var sut = new RangedNumberRequest(expectedOperandType, 1, 3); // Act var result = sut.OperandType; // Assert Assert.Equal(expectedOperandType, result); }
public void SutDoesNotEqualAnonymousObject() { // Arrange var sut = new RangedNumberRequest(typeof(int), 1, 3); object anonymousObject = new ConcreteType(); // Act var result = sut.Equals(anonymousObject); // Assert Assert.False(result, "Equals"); }
public void SutDoesNotEqualOtherObjectWhenOperandTypesDiffer() { // Arrange var sut = new RangedNumberRequest(typeof(int), 1, 3); object other = new RangedNumberRequest(typeof(double), 1, 3); // Act var result = sut.Equals(other); // Assert Assert.False(result, "Equals"); }
public void SutDoesNotEqualOtherSutWhenMaximumsDiffer() { // Arrange var sut = new RangedNumberRequest(typeof(int), 1, 3); var other = new RangedNumberRequest(typeof(int), 1, 4); // Act var result = sut.Equals(other); // Assert Assert.False(result, "Equals"); }
public void MaximumIsCorrect() { // Fixture setup var expectedMaximum = 3; var sut = new RangedNumberRequest(typeof(int), 1, expectedMaximum); // Exercise system var result = sut.Maximum; // Verify outcome Assert.Equal(expectedMaximum, result); // Teardown }
public void CreateWithNonnumericLimitsReturnsNoSpecimen(Type operandType, object minimum, object maximum) { // Arrange var sut = new RandomRangedNumberGenerator(); var dummyContext = new DelegatingSpecimenContext(); var request = new RangedNumberRequest(operandType, minimum, maximum); // Act var result = sut.Create(request, dummyContext); // Verify Assert.Equal(new NoSpecimen(), result); }
public void SutDoesNotEqualAnonymousObject() { // Fixture setup var sut = new RangedNumberRequest(typeof(int), 1, 3); object anonymousObject = new FileStyleUriParser(); // Exercise system var result = sut.Equals(anonymousObject); // Verify outcome Assert.False(result, "Equals"); // Teardown }
public void MaximumIsCorrect() { // Fixture setup var expectedMaximum = 3; var sut = new RangedNumberRequest(typeof(int), 1, expectedMaximum); // Exercise system var result = sut.Maximum; // Verify outcome Assert.Equal(expectedMaximum, result); // Teardown }
public void CreationOnFullRangeShouldNotFail(Type type, object minimum, object maximum) { // Fixture setup var request = new RangedNumberRequest(type, minimum, maximum); var sut = new RandomRangedNumberGenerator(); var dummyContext = new DelegatingSpecimenContext(); // Excercise System And Verify Assert.Null(Record.Exception(() => sut.Create(request, dummyContext))); // Teardown }
public void OperandTypeIsCorrect() { // Fixture setup var expectedOperandType = typeof(int); var sut = new RangedNumberRequest(expectedOperandType, 1, 3); // Exercise system var result = sut.OperandType; // Verify outcome Assert.Equal(expectedOperandType, result); // Teardown }
public void SutDoesNotEqualOtherObjectWhenOperandTypesDiffer() { // Fixture setup var sut = new RangedNumberRequest(typeof(int), 1, 3); object other = new RangedNumberRequest(typeof(double), 1, 3); // Exercise system var result = sut.Equals(other); // Verify outcome Assert.False(result, "Equals"); // Teardown }
public void SutDoesNotEqualNullSut() { // Fixture setup var sut = new RangedNumberRequest(typeof(int), 1, 3); RangedNumberRequest other = null; // Exercise system var result = sut.Equals(other); // Verify outcome Assert.False(result, "Equals"); // Teardown }
public void SutDoesNotEqualOtherSutWhenMaximumsDiffer() { // Fixture setup var sut = new RangedNumberRequest(typeof(int), 1, 3); var other = new RangedNumberRequest(typeof(int), 1, 4); // Exercise system var result = sut.Equals(other); // Verify outcome Assert.False(result, "Equals"); // Teardown }
public void ToStringShouldBeOverridden() { // Arrange var sut = new RangedNumberRequest(typeof(long), 42, 100); // Act var stringResult = sut.ToString(); // Assert Assert.Contains("Int64", stringResult); Assert.Contains("42", stringResult); Assert.Contains("100", stringResult); }
private void CreateAnonymous(RangedNumberRequest range, IComparable value) { lock (this.syncRoot) { var minimum = (IComparable)range.Minimum; var maximum = (IComparable)range.Maximum; if (this.rangedValue != null) { this.rangedValue = Convert.ChangeType(this.rangedValue, range.OperandType, CultureInfo.CurrentCulture); } if (this.rangedValue != null && (minimum.CompareTo(this.rangedValue) <= 0 && maximum.CompareTo(this.rangedValue) > 0)) { this.rangedValue = RangedNumberGenerator.Add(this.rangedValue, Convert.ChangeType(1, range.OperandType, CultureInfo.CurrentCulture)); } else if (minimum.CompareTo(value) == 0) { this.rangedValue = minimum; } else if (maximum.CompareTo(value) == 0) { this.rangedValue = maximum; } else if (minimum.CompareTo(value) <= 0 && maximum.CompareTo(value) <= 0) { this.rangedValue = minimum; } else if (minimum.CompareTo(this.rangedValue) <= 0 && maximum.CompareTo(this.rangedValue) <= 0) { this.rangedValue = minimum; } else { this.rangedValue = RangedNumberGenerator.Add(minimum, value); } this.rangedValue = Convert.ChangeType(this.rangedValue, range.OperandType, CultureInfo.CurrentCulture); } }
public void SutDoesNotEqualAnonymousObject() { // Fixture setup var sut = new RangedNumberRequest(typeof(int), 1, 3); object anonymousObject = new FileStyleUriParser(); // Exercise system var result = sut.Equals(anonymousObject); // Verify outcome Assert.False(result, "Equals"); // Teardown }
public void SutDoesNotEqualNullSut() { // Fixture setup var sut = new RangedNumberRequest(typeof(int), 1, 3); RangedNumberRequest other = null; // Exercise system var result = sut.Equals(other); // Verify outcome Assert.False(result, "Equals"); // Teardown }
public void SutIsEquatable() { // Fixture setup // Exercise system var sut = new RangedNumberRequest(typeof(int), 1, 3); // Verify outcome Assert.IsAssignableFrom<IEquatable<RangedNumberRequest>>(sut); // Teardown }
public void SutDoesNotEqualOtherSutWhenMaximumsDiffer() { // Fixture setup var sut = new RangedNumberRequest(typeof(int), 1, 3); var other = new RangedNumberRequest(typeof(int), 1, 4); // Exercise system var result = sut.Equals(other); // Verify outcome Assert.False(result, "Equals"); // Teardown }
public void CreateWithPropertyDecoratedWithRangeAttributeReturnsCorrectResult( Type attributeType, object attributeMinimum, object attributeMaximum) { // Fixture setup var request = typeof(RangeValidatedType).GetProperty("Property"); Type target = request.PropertyType; var expectedRequest = new RangedNumberRequest( target, Convert.ChangeType(RangeValidatedType.Minimum, target, CultureInfo.CurrentCulture), Convert.ChangeType(RangeValidatedType.Maximum, target, CultureInfo.CurrentCulture) ); var expectedResult = new object(); var context = new DelegatingSpecimenContext { OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r) }; var sut = new RangeAttributeRelay(); // Exercise system var result = sut.Create(request, context); // Verify outcome Assert.Equal(expectedResult, result); // Teardown }
public void GetHashCodeWillReturnCorrectResult() { // Fixture setup Type operandType = typeof(int); object minimum = 1; object maximum = 3; var sut = new RangedNumberRequest(operandType, minimum, maximum); var expectedHashCode = operandType.GetHashCode() ^ minimum.GetHashCode() ^ maximum.GetHashCode(); // Exercise system var result = sut.GetHashCode(); // Verify outcome Assert.Equal(expectedHashCode, result); // Teardown }
public void CreateWithFieldDecoratedWithRangeAttributeReturnsCorrectResult( string name, object attributeMinimum, object attributeMaximum) { // Fixture setup var request = typeof(RangeValidatedType).GetField(name); Type target = Nullable.GetUnderlyingType(request.FieldType) ?? request.FieldType; var expectedRequest = new RangedNumberRequest( target, Convert.ChangeType(RangeValidatedType.Minimum, target, CultureInfo.CurrentCulture), Convert.ChangeType(RangeValidatedType.Maximum, target, CultureInfo.CurrentCulture) ); var expectedResult = new object(); var context = new DelegatingSpecimenContext { #pragma warning disable 618 OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r) #pragma warning restore 618 }; var sut = new RangeAttributeRelay(); // Exercise system var result = sut.Create(request, context); // Verify outcome Assert.Equal(expectedResult, result); // Teardown }
public void SutEqualsOtherSutWhenConstructorParametersEquals() { // Fixture setup Type operandType = typeof(int); object minimum = 1; object maximum = 3; var sut = new RangedNumberRequest(operandType, minimum, maximum); var other = new RangedNumberRequest(operandType, minimum, maximum); // Exercise system var result = sut.Equals(other); // Verify outcome Assert.True(result, "Equals"); // Teardown }
public void CreateWithRangeAttributeRequestReturnsCorrectResult(Type type, object minimum, object maximum) { // Fixture setup var rangeAttribute = new RangeAttribute(type, minimum.ToString(), maximum.ToString()); var providedAttribute = new ProvidedAttribute(rangeAttribute, true); ICustomAttributeProvider request = new FakeCustomAttributeProvider(providedAttribute); Type conversionType = rangeAttribute.OperandType; var expectedRequest = new RangedNumberRequest( conversionType, Convert.ChangeType(rangeAttribute.Minimum, conversionType, CultureInfo.CurrentCulture), Convert.ChangeType(rangeAttribute.Maximum, conversionType, CultureInfo.CurrentCulture) ); var expectedResult = new object(); var context = new DelegatingSpecimenContext { #pragma warning disable 618 OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r) #pragma warning restore 618 }; var sut = new RangeAttributeRelay(); // Exercise system var result = sut.Create(request, context); // Verify outcome Assert.Equal(expectedResult, result); // Teardown }
public void SutDoesNotEqualOtherObjectWhenOperandTypesDiffer() { // Fixture setup var sut = new RangedNumberRequest(typeof(int), 1, 3); object other = new RangedNumberRequest(typeof(double), 1, 3); // Exercise system var result = sut.Equals(other); // Verify outcome Assert.False(result, "Equals"); // Teardown }
private void CreateAnonymous(RangedNumberRequest range, IComparable value) { lock (this.syncRoot) { var minimum = (IComparable)range.Minimum; var maximum = (IComparable)range.Maximum; if (this.rangedValue != null) { object target; if (range.OperandType == typeof(byte) && Convert.ToInt32( this.rangedValue, CultureInfo.CurrentCulture) > byte.MaxValue || range.OperandType == typeof(short) && Convert.ToInt32( this.rangedValue, CultureInfo.CurrentCulture) > short.MaxValue) target = minimum; else target = this.rangedValue; this.rangedValue = Convert.ChangeType(target, range.OperandType, CultureInfo.CurrentCulture); } if (this.rangedValue != null && (minimum.CompareTo(this.rangedValue) <= 0 && maximum.CompareTo(this.rangedValue) > 0)) { this.rangedValue = Convert.ChangeType( RangedNumberGenerator.Add( this.rangedValue, Convert.ChangeType( 1, range.OperandType, CultureInfo.CurrentCulture)), range.OperandType, CultureInfo.CurrentCulture); if (maximum.CompareTo(this.rangedValue) < 0) { this.rangedValue = Convert.ChangeType( maximum, range.OperandType, CultureInfo.CurrentCulture); } } else if (minimum.CompareTo(value) == 0) { this.rangedValue = minimum; } else if (maximum.CompareTo(value) == 0) { this.rangedValue = maximum; } else if (minimum.CompareTo(value) <= 0 && maximum.CompareTo(value) <= 0) { this.rangedValue = minimum; } else if (minimum.CompareTo(this.rangedValue) <= 0 && maximum.CompareTo(this.rangedValue) <= 0) { this.rangedValue = minimum; } else if (minimum.CompareTo(value) < 0) { this.rangedValue = value; } else { this.rangedValue = RangedNumberGenerator.Add(minimum, value); if (minimum.CompareTo(this.rangedValue) > 0 || maximum.CompareTo(this.rangedValue) < 0) { this.rangedValue = minimum; } } this.rangedValue = Convert.ChangeType(this.rangedValue, range.OperandType, CultureInfo.CurrentCulture); } }