Skip to content

Commit

Permalink
Added member type validation for stringlength attribute relay (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
aivascu authored Feb 20, 2023
1 parent 6153ad1 commit 42ccdd5
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 59 deletions.
5 changes: 2 additions & 3 deletions Src/AutoFixture/DataAnnotations/NumericRangedRequestRelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ public class NumericRangedRequestRelay : ISpecimenBuilder
/// <inheritdoc />
public object Create(object request, ISpecimenContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (context is null) throw new ArgumentNullException(nameof(context));

var rangedRequest = request as RangedRequest;
if (rangedRequest == null)
if (request is not RangedRequest rangedRequest)
return new NoSpecimen();

if (!rangedRequest.MemberType.IsNumberType())
Expand Down
16 changes: 7 additions & 9 deletions Src/AutoFixture/DataAnnotations/RangeAttributeRelay.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using AutoFixture.Kernel;

namespace AutoFixture.DataAnnotations
Expand Down Expand Up @@ -33,21 +32,20 @@ public IRequestMemberTypeResolver RequestMemberTypeResolver
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (context is null) throw new ArgumentNullException(nameof(context));

var rangeAttribute = TypeEnvy.GetAttribute<RangeAttribute>(request);
if (rangeAttribute == null)
if (rangeAttribute is null)
{
return new NoSpecimen();
}

var memberType = this.GetMemberType(rangeAttribute, request);
var rangedRequest =
new RangedRequest(
memberType,
rangeAttribute.OperandType,
rangeAttribute.Minimum,
rangeAttribute.Maximum);
var rangedRequest = new RangedRequest(
memberType,
rangeAttribute.OperandType,
rangeAttribute.Minimum,
rangeAttribute.Maximum);

return context.Resolve(rangedRequest);
}
Expand Down
36 changes: 29 additions & 7 deletions Src/AutoFixture/DataAnnotations/StringLengthAttributeRelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ namespace AutoFixture.DataAnnotations
/// </summary>
public class StringLengthAttributeRelay : ISpecimenBuilder
{
private IRequestMemberTypeResolver requestMemberTypeResolver = new RequestMemberTypeResolver();

/// <summary>
/// Gets or sets the current IRequestMemberTypeResolver.
/// </summary>
public IRequestMemberTypeResolver RequestMemberTypeResolver
{
get => this.requestMemberTypeResolver;
set => this.requestMemberTypeResolver = value ?? throw new ArgumentNullException(nameof(value));
}

/// <summary>
/// Creates a new specimen based on a specified length of characters that are allowed.
/// </summary>
Expand All @@ -21,23 +32,34 @@ public class StringLengthAttributeRelay : ISpecimenBuilder
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (request == null)
if (context is null) throw new ArgumentNullException(nameof(context));

if (request is null)
{
return new NoSpecimen();
}

if (context == null)
var attribute = TypeEnvy.GetAttribute<StringLengthAttribute>(request);
if (attribute is null)
{
throw new ArgumentNullException(nameof(context));
return new NoSpecimen();
}

if (!this.RequestMemberTypeResolver.TryGetMemberType(request, out var memberType))
{
return new NoSpecimen();
}

var stringLengthAttribute = TypeEnvy.GetAttribute<StringLengthAttribute>(request);
if (stringLengthAttribute == null)
if (memberType != typeof(string))
{
return new NoSpecimen();
}

return context.Resolve(new ConstrainedStringRequest(stringLengthAttribute.MinimumLength, stringLengthAttribute.MaximumLength));
var stringRequest = new ConstrainedStringRequest(
attribute.MinimumLength,
attribute.MaximumLength);

return context.Resolve(stringRequest);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ public void FixtureCorrectlyCreatesShortText()
{
// Arrange
var fixture = new Fixture();

// Act
var actual = fixture.Create<ClassWithShortStringLengthConstrainedConstructorArgument>();

// Assert
Assert.True(
actual.ShortText.Length <= ClassWithShortStringLengthConstrainedConstructorArgument.ShortTextMaximumLength,
actual.ShortText.Length <=
ClassWithShortStringLengthConstrainedConstructorArgument.ShortTextMaximumLength,
"AutoFixture should respect [StringLength] attribute on constructor arguments.");
}

Expand All @@ -24,8 +27,10 @@ public void FixtureCorrectlyCreatesLongText()
{
// Arrange
var fixture = new Fixture();

// Act
var actual = fixture.Create<ClassWithLongStringLengthConstrainedConstructorArgument>();

// Assert
Assert.Equal(
ClassWithLongStringLengthConstrainedConstructorArgument.LongTextLength,
Expand All @@ -38,7 +43,7 @@ private class ClassWithShortStringLengthConstrainedConstructorArgument
public readonly string ShortText;

public ClassWithShortStringLengthConstrainedConstructorArgument(
[StringLength(ShortTextMaximumLength)]string shortText)
[StringLength(ShortTextMaximumLength)] string shortText)
{
this.ShortText = shortText;
}
Expand All @@ -50,10 +55,10 @@ private class ClassWithLongStringLengthConstrainedConstructorArgument
public readonly string LongText;

public ClassWithLongStringLengthConstrainedConstructorArgument(
[StringLength(LongTextLength, MinimumLength = LongTextLength)]string longText)
[StringLength(LongTextLength, MinimumLength = LongTextLength)] string longText)
{
this.LongText = longText;
}
}
}
}
}
Loading

0 comments on commit 42ccdd5

Please sign in to comment.