From 6e32d4312b0e08c6c0188e4cff286865eeb4e3f9 Mon Sep 17 00:00:00 2001 From: skwasjer <11424653+skwasjer@users.noreply.github.com> Date: Wed, 9 Oct 2024 04:43:03 +0200 Subject: [PATCH] fix(CA1062): throw when context is null --- src/IbanNet/Validation/Rules/QrIbanRule.cs | 9 ++++-- .../Rules/IsValidQrIbanRuleTests.cs | 32 ++++++++++++++++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/IbanNet/Validation/Rules/QrIbanRule.cs b/src/IbanNet/Validation/Rules/QrIbanRule.cs index 9db8baf..bc15055 100644 --- a/src/IbanNet/Validation/Rules/QrIbanRule.cs +++ b/src/IbanNet/Validation/Rules/QrIbanRule.cs @@ -12,6 +12,11 @@ public sealed class QrIbanRule : IIbanValidationRule /// 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))) { @@ -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; } } diff --git a/test/IbanNet.Tests/Validation/Rules/IsValidQrIbanRuleTests.cs b/test/IbanNet.Tests/Validation/Rules/IsValidQrIbanRuleTests.cs index 8fb97be..db7287b 100644 --- a/test/IbanNet.Tests/Validation/Rules/IsValidQrIbanRuleTests.cs +++ b/test/IbanNet.Tests/Validation/Rules/IsValidQrIbanRuleTests.cs @@ -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; @@ -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(); } @@ -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() + .WithParameterName(nameof(context)); + } }