Skip to content

Commit

Permalink
Added xml comments to reduce warning count back to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
Scandal-UK committed Feb 2, 2024
1 parent 6b04d58 commit 3ea9af8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ private static string DeriveJsonTypeFromType(Type dataType)
{
"Boolean" => "boolean",
"DateTime" => "date",
"DateTimeOffset" => "date",
"String" => "string",
_ => "object",
};
Expand Down
18 changes: 17 additions & 1 deletion FluentValidationLister.Tests/TestListingMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,97 @@ namespace FluentValidationLister.Tests;
/// <summary> Tests for the error messages generated by the validator rules. </summary>
public class TestListingMessages
{
/// <summary> Test correct message is returned for NotEmpty. </summary>
[Fact(DisplayName = "NotEmpty() returns correct message")]
public void NotEmpty_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).NotEmpty())
.ErrorList[nameof(Person.Forename)]["required"].Should().Be("'Forename' must not be empty.");

/// <summary> Test correct message is returned for LessThan. </summary>
[Fact(DisplayName = "LessThan() returns correct message")]
public void LessThan_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).LessThan(100))
.ErrorList[nameof(Person.NullableInt)]["lessThan"].Should().Be("'Nullable Int' must be less than '100'.");

/// <summary> Test correct message is returned for LessThanOrEqualTo. </summary>
[Fact(DisplayName = "LessThanOrEqualTo() returns correct message")]
public void LessThanOrEqualTo_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).LessThanOrEqualTo(100))
.ErrorList[nameof(Person.NullableInt)]["lessThanOrEqualTo"].Should().Be("'Nullable Int' must be less than or equal to '100'.");

/// <summary> Test correct message is returned for GreaterThan. </summary>
[Fact(DisplayName = "GreaterThan() returns correct message")]
public void GreaterThan_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).GreaterThan(100))
.ErrorList[nameof(Person.NullableInt)]["greaterThan"].Should().Be("'Nullable Int' must be greater than '100'.");

/// <summary> Test correct message is returned for GreaterThanOrEqualTo. </summary>
[Fact(DisplayName = "GreaterThanOrEqualTo() returns correct message")]
public void GreaterThanOrEqualTo_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).GreaterThanOrEqualTo(100))
.ErrorList[nameof(Person.NullableInt)]["greaterThanOrEqualTo"].Should().Be("'Nullable Int' must be greater than or equal to '100'.");

/// <summary> Test correct message is returned for MaxLength. </summary>
[Fact(DisplayName = "MaxLength() returns correct message")]
public void MaxLength_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).MaximumLength(10))
.ErrorList[nameof(Person.Forename)]["maxLength"].Should().Be("The length of 'Forename' must be 10 characters or fewer.");

/// <summary> Test correct message is returned for MinLength. </summary>
[Fact(DisplayName = "MinLength() returns correct message")]
public void MinLength_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).MinimumLength(10))
.ErrorList[nameof(Person.Forename)]["minLength"].Should().Be("The length of 'Forename' must be at least 10 characters.");

/// <summary> Test correct message is returned for Length. </summary>
[Fact(DisplayName = "Length() returns correct message")]
public void Length_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Length(2, 10))
.ErrorList[nameof(Person.Forename)]["length"].Should().Be("'Forename' must be between 2 and 10 characters.");

[Fact(DisplayName = "Exact Length() returns correct message")]
/// <summary> Test correct message is returned for ExactLength. </summary>
[Fact(DisplayName = "ExactLength() returns correct message")]
public void ExactLength_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Length(10))
.ErrorList[nameof(Person.Forename)]["exactLength"].Should().Be("'Forename' must be 10 characters in length.");

/// <summary> Test correct message is returned for Equal. </summary>
[Fact(DisplayName = "Equal() returns correct message for property")]
public void Equal_ReturnsCorrectMessageForProperty() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Equal(x => x.Surname))
.ErrorList[nameof(Person.Forename)]["compare"].Should().Be("'Forename' must be equal to 'Surname'.");

/// <summary> Test correct message is returned for Equal (value). </summary>
[Fact(DisplayName = "Equal() returns correct message for value")]
public void Equal_ReturnsCorrectMessageForValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Equal("Foo bar"))
.ErrorList[nameof(Person.Forename)]["compare"].Should().Be("'Forename' must be equal to 'Foo bar'.");

/// <summary> Test correct message is returned for Matches. </summary>
[Fact(DisplayName = "Matches() returns correct message")]
public void Matches_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Matches("^[A-Za-z]+$"))
.ErrorList[nameof(Person.Forename)]["regex"].Should().Be("'Forename' is not in the correct format.");

/// <summary> Test correct message is returned for EmailAddress. </summary>
[Fact(DisplayName = "EmailAddress() returns correct message")]
public void EmailAddress_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Email).EmailAddress())
.ErrorList[nameof(Person.Email)]["regex"].Should().Be("'Email' is not a valid email address.");

/// <summary> Test correct message is returned for InclusiveBetween. </summary>
[Fact(DisplayName = "InclusiveBetween() returns correct message")]
public void InclusiveBetween_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.AnotherInt).InclusiveBetween(5, 8))
.ErrorList[nameof(Person.AnotherInt)]["range"].Should().Be("'Another Int' must be between 5 and 8.");

/// <summary> Test correct message is returned for ExclusiveBetween. </summary>
[Fact(DisplayName = "ExclusiveBetween() returns correct message")]
public void ExclusiveBetween_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.AnotherInt).ExclusiveBetween(5, 8))
.ErrorList[nameof(Person.AnotherInt)]["exclusiveRange"].Should().Be("'Another Int' must be between 5 and 8 (exclusive).");

/// <summary> Test correct message is returned for child validator. </summary>
[Fact(DisplayName = "Child validator returns correct message")]
public void ChildValidator_ReturnsCorrectMessage() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Address).SetValidator(new AddressValidator()))
Expand Down
26 changes: 25 additions & 1 deletion FluentValidationLister.Tests/TestListingRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,104 +14,126 @@ namespace FluentValidationLister.Tests;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

/// <summary> Suite of tests to ensure that applied rules are returned as expected. </summary>
public class TestListingRules
{
/// <summary> Test that null validator throws ArgumentNullException. </summary>
[Fact(DisplayName = "Null validator throws ArgumentNullException")]
public void NullValidator_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new ValidationLister(validator: null, typeof(Person), new ServiceCollection().BuildServiceProvider()));

/// <summary> Test that null modelType throws ArgumentNullException. </summary>
[Fact(DisplayName = "Null modelType throws ArgumentNullException")]
public void NullModelType_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new ValidationLister(new TestValidator(), modelType: null, new ServiceCollection().BuildServiceProvider()));

/// <summary> Test that null serviceProvider throws ArgumentNullException. </summary>
[Fact(DisplayName = "Null serviceProvider throws ArgumentNullException")]
public void NullServiceProvider_ThrowsArgumentNullException() =>
Assert.Throws<ArgumentNullException>(() => new ValidationLister(new TestValidator(), typeof(Person), serviceProvider: null));

/// <summary> Test that NotEmpty() returns required rule. </summary>
[Fact(DisplayName = "NotEmpty() returns required rule")]
public void NotEmpty_ReturnsRequiredRule() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).NotEmpty())
.ValidatorList[key: nameof(Person.Forename)]["required"].Should().Be(true);

/// <summary> Test that two validators return two rules. </summary>
[Fact(DisplayName = "Two validators return two rules")]
public void TwoValidators_ReturnTwoRulesAndMessages() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).NotNull().LessThan(100))
.ValidatorList[key: nameof(Person.NullableInt)].Count.Should().Be(2);

/// <summary> Test that LessThan() returns correct limit value. </summary>
[Fact(DisplayName = "LessThan() returns correct limit value")]
public void LessThan_ReturnsCorrectLimitValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).LessThan(100))
.ValidatorList[key: nameof(Person.NullableInt)]["lessThan"].Should().Be(100);

/// <summary> Test that LessThanOrEqualTo() returns correct limit value. </summary>
[Fact(DisplayName = "LessThanOrEqualTo() returns correct limit value")]
public void LessThanOrEqualTo_ReturnsCorrectLimitValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).LessThanOrEqualTo(100))
.ValidatorList[key: nameof(Person.NullableInt)]["lessThanOrEqualTo"].Should().Be(100);

/// <summary> Test that GreaterThan() returns correct limit value. </summary>
[Fact(DisplayName = "GreaterThan() returns correct limit value")]
public void GreaterThan_ReturnsCorrectLimitValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).GreaterThan(100))
.ValidatorList[key: nameof(Person.NullableInt)]["greaterThan"].Should().Be(100);

/// <summary> Test that GreaterThanOrEqualTo() returns correct limit value. </summary>
[Fact(DisplayName = "GreaterThanOrEqualTo() returns correct limit value")]
public void GreaterThanOrEqualTo_ReturnsCorrectLimitValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.NullableInt).GreaterThanOrEqualTo(100))
.ValidatorList[key: nameof(Person.NullableInt)]["greaterThanOrEqualTo"].Should().Be(100);

/// <summary> Test that MaxLength() returns correct limit value. </summary>
[Fact(DisplayName = "MaxLength() returns correct limit value")]
public void MaxLength_ReturnsCorrectLimitValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).MaximumLength(10))
.ValidatorList[key: nameof(Person.Forename)]["maxLength"].Should().Be(10);

/// <summary> Test that MinLength() returns correct limit value. </summary>
[Fact(DisplayName = "MinLength() returns correct limit value")]
public void MinLength_ReturnsCorrectLimitValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).MinimumLength(10))
.ValidatorList[key: nameof(Person.Forename)]["minLength"].Should().Be(10);

[Fact(DisplayName = "Exact Length() returns correct value")]
/// <summary> Test that ExactLength() returns correct value. </summary>
[Fact(DisplayName = "ExactLength() returns correct value")]
public void ExactLength_ReturnsCorrectValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Length(10))
.ValidatorList[key: nameof(Person.Forename)]["exactLength"].Should().Be(10);

/// <summary> Test that Equal() returns correct comparison property. </summary>
[Fact(DisplayName = "Equal() returns correct comparison property")]
public void Equal_ReturnsCorrectComparisonProperty() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Equal(x => x.Surname))
.ValidatorList[key: nameof(Person.Forename)]["compare"].Should().Be("Surname");

/// <summary> Test that Equal() returns correct comparison value. </summary>
[Fact(DisplayName = "Equal() returns correct comparison value")]
public void Equal_ReturnsCorrectComparisonValue() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Equal("Foo bar"))
.ValidatorList[key: nameof(Person.Forename)]["compare"].Should().Be("Foo bar");

/// <summary> Test that Matches() returns regular expression. </summary>
[Fact(DisplayName = "Matches() returns regular expression")]
public void Matches_ReturnsRegularExpression() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).Matches("^[A-Za-z]+$"))
.ValidatorList[key: nameof(Person.Forename)]["regex"].Should().Be("^[A-Za-z]+$");

/// <summary> Test that EmailAddress() returns regular expression. </summary>
[Fact(DisplayName = "EmailAddress() returns regular expression")]
public void EmailAddress_ReturnsRegularExpression() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Email).EmailAddress())
.ValidatorList[key: nameof(Person.Email)]["regex"].Should().NotBeNull();

/// <summary> Test that validator includes child validator rules. </summary>
[Fact(DisplayName = "Validator includes child validator rules")]
public void Validator_IncludesChildValidatorRules() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Address).SetValidator(new AddressValidator()))
.ValidatorList["Address.Line1"].Should().NotBeNull();

/// <summary> Test that validator includes grandchild validator rules. </summary>
[Fact(DisplayName = "Validator includes grandchild validator rules")]
public void Validator_IncludesGrandChildValidatorRules() =>
TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Address).SetValidator(new AddressValidator()))
.ValidatorList["Address.Country.Name"].Should().NotBeNull();

/// <summary> Test that validator includes collection rules. </summary>
[Fact(DisplayName = "Validator includes collection rules")]
public void Validator_IncludesCollectionRules() =>
TestListingHelper.GetValidatorRules(v => v.RuleForEach(x => x.Orders).SetValidator(new OrderValidator()))
.ValidatorList["Orders.Amount"].Should().NotBeNull();

/// <summary> Test that when serialised returns correct message. </summary>
[Fact(DisplayName = "When serialised returns correct message")]
public void WhenSerialised_ReturnsCorrectMessage() =>
TestListingHelper.GetDeserialisedValidatorRules(TestListingHelper.GetValidatorRules(v => v.RuleFor(x => x.Forename).NotEmpty()))["errorList"]["forename"]["required"].ToString().Should().Be("'Forename' must not be empty.");

/// <summary> Test that Length() when serialised, returns correct values. </summary>
[Fact(DisplayName = "Length() when serialised, returns correct values")]
public void Length_WhenSerialisedReturnsCorrectValues()
{
Expand All @@ -120,6 +142,7 @@ public void Length_WhenSerialisedReturnsCorrectValues()
forenameRules["max"].ToObject<int>().Should().Be(10);
}

/// <summary> Test that InclusiveBetween() when serialised, returns correct values. </summary>
[Fact(DisplayName = "InclusiveBetween() when serialised, returns correct values")]
public void InclusiveBetween_WhenSerialisedReturnsCorrectValues()
{
Expand All @@ -128,6 +151,7 @@ public void InclusiveBetween_WhenSerialisedReturnsCorrectValues()
anotherIntRules["to"].ToObject<int>().Should().Be(10);
}

/// <summary> Test that ExclusiveBetween() returns correct values. </summary>
[Fact(DisplayName = "ExclusiveBetween() returns correct values")]
public void ExclusiveBetween_ReturnsCorrectValues()
{
Expand Down

0 comments on commit 3ea9af8

Please sign in to comment.