Skip to content

Commit

Permalink
fix(CA1062): throw when context is null (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer authored Oct 9, 2024
1 parent d840a4e commit ab0970b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/IbanNet/Validation/Rules/QrIbanRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public sealed class QrIbanRule : IIbanValidationRule
/// <inheritdoc />
public ValidationRuleResult Validate(ValidationRuleContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

IbanCountry? country = context.Country;
if (country != null && IsValid(country, context.Value.Substring(country.Bank.Position, country.Bank.Length)))
{
Expand All @@ -24,7 +29,7 @@ public ValidationRuleResult Validate(ValidationRuleContext context)
internal static bool IsValid(IbanCountry country, string iidStr)
{
return country.TwoLetterISORegionName is "CH" or "LI"
&& int.TryParse(iidStr, NumberStyles.None, NumberFormatInfo.InvariantInfo, out int iid)
&& iid is >= 30000 and <= 31999;
&& int.TryParse(iidStr, NumberStyles.None, NumberFormatInfo.InvariantInfo, out int iid)
&& iid is >= 30000 and <= 31999;
}
}
32 changes: 27 additions & 5 deletions test/IbanNet.Tests/Validation/Rules/IsValidQrIbanRuleTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using IbanNet.Validation.Results;
using IbanNet.Registry;
using IbanNet.Registry;
using IbanNet.Validation.Results;

namespace IbanNet.Validation.Rules;

public class IsValidQrIbanRuleTests
public sealed class IsValidQrIbanRuleTests
{
private readonly QrIbanRule _sut;

Expand All @@ -25,8 +25,12 @@ public IsValidQrIbanRuleTests()
[Theory]
public void Given_invalid_values_it_should_return_success(string iban)
{
ValidationRuleResult actual = _sut.Validate(new ValidationRuleContext(iban, IbanRegistry.Default[iban.Substring(0, 2)]));
IbanCountry country = IbanRegistry.Default[iban.Substring(0, 2)];

// Act
ValidationRuleResult actual = _sut.Validate(new ValidationRuleContext(iban, country));

// Assert
actual.Should().BeOfType<InvalidQrIbanResult>();
}

Expand All @@ -35,8 +39,26 @@ public void Given_invalid_values_it_should_return_success(string iban)
[Theory]
public void Given_valid_values_it_should_return_success(string iban)
{
ValidationRuleResult actual = _sut.Validate(new ValidationRuleContext(iban, IbanRegistry.Default[iban.Substring(0, 2)]));
IbanCountry country = IbanRegistry.Default[iban.Substring(0, 2)];

// Act
ValidationRuleResult actual = _sut.Validate(new ValidationRuleContext(iban, country));

// Assert
actual.Should().Be(ValidationRuleResult.Success);
}

[Fact]
public void Given_that_context_is_null_when_validating_it_should_throw()
{
ValidationRuleContext? context = null;

// Act
Action act = () => _sut.Validate(context!);

// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithParameterName(nameof(context));
}
}

0 comments on commit ab0970b

Please sign in to comment.