public void Given_rule_is_configured_via_factory_and_serviceCollection_it_should_add_instance_to_rule_collection() { var configuredRule = new TestValidationRule(); IServiceProvider providedServiceProvider = null; IIbanNetOptionsBuilder returnedBuilder = _builder .WithRule(s => { providedServiceProvider = s; return(configuredRule); }); // Act IServiceProvider services = _serviceCollection.BuildServiceProvider(); // Assert IbanValidatorOptions opts = services.GetService <IOptions <IbanValidatorOptions> >().Value; opts.Should() .HaveRule <IIbanValidationRule>() .And.HaveCount(1) .And.Subject.Single() .Should() .BeSameAs(configuredRule); providedServiceProvider.Should().NotBeNull(); returnedBuilder.Should().BeSameAs(_builder); }
public void Given_rule_is_configured_via_factory_and_serviceCollection_it_should_add_instance_to_rule_collection() { var configuredRule = new TestValidationRule(); IComponentContext providedComponentContext = null; IIbanNetOptionsBuilder returnedBuilder = _builder .WithRule(ctx => { providedComponentContext = ctx; return(configuredRule); }); // Act IContainer container = _containerBuilder.Build(); // Assert IbanValidatorOptions opts = container.Resolve <IbanValidatorOptions>(); opts.Should() .HaveRule <IIbanValidationRule>() .And.HaveCount(1) .And.Subject.Single() .Should() .BeSameAs(configuredRule); providedComponentContext.Should().NotBeNull(); returnedBuilder.Should().BeSameAs(_builder); }
/// <summary> /// Configures the <see cref="IbanValidator" /> to use the specified registry. /// </summary> /// <param name="builder">The builder instance.</param> /// <param name="registry">The registry of IBAN countries.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder UseRegistry(this IIbanNetOptionsBuilder builder, IEnumerable <IbanCountry> registry) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (registry is null) { throw new ArgumentNullException(nameof(registry)); } if (registry is IIbanRegistry ibanRegistry && ibanRegistry.Providers.Count == 0) { throw new ArgumentException(Resources.The_registry_has_no_providers, nameof(registry)); } builder.Configure(options => options.Registry = registry is IIbanRegistry ir ? ir : new IbanRegistry { Providers = { new IbanRegistryListProvider(registry) } } ); return(builder); }
// TODO: maybe add 'experimental' option to opt-in into experimental (still in development/unstable) check digit validators/countries (perhaps specific per country/algo. /// <summary> /// Enables national check digit validation for a subset of IBAN countries. /// </summary> /// <param name="builder">The builder instance.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder"/> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder ValidateNationalCheckDigits(this IIbanNetOptionsBuilder builder) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } return(builder.WithRule(() => new HasValidNationalCheckDigitsRule())); }
public void Given_loose_validation_is_configured_it_should_set_validation_method() { // Act IIbanNetOptionsBuilder returnedBuilder = _builder.UseLooseValidation(); // Assert _builderStub.Should().HaveConfiguredValidationMethod(ValidationMethod.Loose); returnedBuilder.Should().BeSameAs(_builderStub.Object); }
private static IIbanNetOptionsBuilder UseValidationMethod(this IIbanNetOptionsBuilder builder, ValidationMethod validationMethod) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } return(builder.Configure(options => options.Method = validationMethod)); }
protected override void Given() { Fixture.Configure(builder => { IIbanNetOptionsBuilder returnedBuilder = builder.WithRule(typeof(TestValidationRule)); returnedBuilder.Should().BeSameAs(builder); }); }
/// <summary> /// Registers a custom validation rule that is executed after built-in validation has passed. /// </summary> /// <typeparam name="T">The type of the validation rule.</typeparam> /// <param name="builder">The builder instance.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder WithRule <T>(this IIbanNetOptionsBuilder builder) where T : class, IIbanValidationRule { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } return(builder.WithRule(typeof(T))); }
protected override void Given() { Fixture.Configure(builder => { // ReSharper disable once AssignNullToNotNullAttribute IIbanNetOptionsBuilder returnedBuilder = builder.Configure(options => options.Registry = null); returnedBuilder.Should().BeSameAs(builder); }); }
public void Given_multiple_registry_providers_are_configured_it_should_use_providers() { var customProvider = new IbanRegistryListProvider(new[] { new IbanCountry("XX") }); // Act IIbanNetOptionsBuilder returnedBuilder = _builder.UseRegistryProvider(new SwiftRegistryProvider(), customProvider); // Assert _builderStub.Should().HaveConfiguredRegistry(new SwiftRegistryProvider().Concat(customProvider)); returnedBuilder.Should().BeSameAs(_builderStub.Object); }
protected override void Given() { Fixture.Configure(builder => { IIbanNetOptionsBuilder returnedBuilder = builder.UseRegistry( new SwiftRegistryProvider().Where(c => c.TwoLetterISORegionName == "NL" || c.TwoLetterISORegionName == "GB") ); returnedBuilder.Should().BeSameAs(builder); }); }
public void Given_rule_is_configured_via_factory_it_should_add_instance_to_rule_collection() { var configuredRule = new TestValidationRule(); // Act IIbanNetOptionsBuilder returnedBuilder = _builder.WithRule(() => configuredRule); // Assert _builderStub.Should() .HaveConfiguredRule <TestValidationRule>() .And.Contain(configuredRule); returnedBuilder.Should().BeSameAs(_builderStub.Object); }
/// <summary> /// Registers a handler to configure the options when the builder executes. /// </summary> /// <param name="builder">The builder instance.</param> /// <param name="configure">The handler that is called when configuring the options.</param> public static IIbanNetOptionsBuilder Configure(this IIbanNetOptionsBuilder builder, Action <IbanValidatorOptions> configure) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (configure is null) { throw new ArgumentNullException(nameof(configure)); } return(builder.Configure((_, options) => configure(options))); }
public void Given_that_builder_is_null_when_adding_rule_it_should_throw() { IIbanNetOptionsBuilder builder = null; // Act // ReSharper disable once ExpressionIsAlwaysNull Action act = () => builder.ValidateNationalCheckDigits(); // Assert act.Should() .Throw <ArgumentNullException>() .Which.ParamName.Should() .Be(nameof(builder)); }
protected override void Given() { IIbanValidationRule fakeRule = Mock.Of <IIbanValidationRule>(); _fakeRuleType = fakeRule.GetType(); Fixture.Configure(builder => { IIbanNetOptionsBuilder returnedBuilder = builder .WithRule(typeof(TestValidationRule)) .WithRule <AnotherTestValidationRule>() .WithRule(() => fakeRule); returnedBuilder.Should().BeSameAs(builder); }); }
/// <summary> /// Registers a custom validation rule that is executed after built-in validation has passed. /// </summary> /// <param name="builder">The builder instance.</param> /// <param name="implementationType">The type of the validation rule.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder WithRule(this IIbanNetOptionsBuilder builder, Type implementationType) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (implementationType is null) { throw new ArgumentNullException(nameof(implementationType)); } return(builder.Configure( (adapter, options) => options.Rules.Add((IIbanValidationRule)adapter.GetRequiredService(implementationType)) )); }
/// <summary> /// Registers a handler to configure the options when the builder executes. /// </summary> /// <param name="builder">The builder instance.</param> /// <param name="configure">The handler that is called when configuring the options.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder Configure(this IIbanNetOptionsBuilder builder, Action <IComponentContext, IbanValidatorOptions> configure) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (configure is null) { throw new ArgumentNullException(nameof(configure)); } return(builder.Configure( (adapter, options) => configure(adapter.GetRequiredService <IComponentContext>(), options) )); }
/// <summary> /// Registers a custom validation rule that is executed after built-in validation has passed. /// </summary> /// <typeparam name="T">The type of the validation rule.</typeparam> /// <param name="builder">The builder instance.</param> /// <param name="implementationFactory">The factory returning a new instance of the rule.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder WithRule <T>(this IIbanNetOptionsBuilder builder, Func <T> implementationFactory) where T : class, IIbanValidationRule { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (implementationFactory is null) { throw new ArgumentNullException(nameof(implementationFactory)); } return(builder.Configure( options => options.Rules.Add(implementationFactory()) )); }
public void When_registering_rule_it_should_return_builder() { IIbanNetOptionsBuilder returnedBuilder = null; IServiceProvider services = new ServiceCollection() // Register rule .AddIbanNet(builder => { returnedBuilder = builder.ValidateNationalCheckDigits(); returnedBuilder.Should().Be(builder); }) .BuildServiceProvider(); // Act // Resolve to trigger extension method. services.GetRequiredService <IIbanValidator>(); returnedBuilder.Should().NotBeNull(); }
/// <summary> /// Registers a custom validation rule that is executed after built-in validation has passed. /// </summary> /// <typeparam name="T">The type of the validation rule.</typeparam> /// <param name="builder">The builder instance.</param> /// <param name="implementationFactory">The factory returning a new instance of the rule.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder WithRule <T>(this IIbanNetOptionsBuilder builder, Func <IServiceProvider, T> implementationFactory) where T : class, IIbanValidationRule { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (implementationFactory is null) { throw new ArgumentNullException(nameof(implementationFactory)); } return(Configure(builder, (services, options) => options.Rules.Add( implementationFactory(services.GetRequiredService <IServiceProvider>()) ) )); }
public void Given_registry_is_configured_it_should_set_registry() { IEnumerable <IbanCountry> limitedCountries = IbanRegistry.Default .Where((country, i) => i % 2 == 0) .ToList(); // Act IIbanNetOptionsBuilder returnedBuilder = _builder.UseRegistry(new IbanRegistry { Providers = { new IbanRegistryListProvider(limitedCountries) } }); // Assert _builderStub.Should().HaveConfiguredRegistry(limitedCountries); returnedBuilder.Should().BeSameAs(_builderStub.Object); }
/// <summary> /// Configures the <see cref="IbanValidator" /> to use the specified provider(s). /// </summary> /// <param name="builder">The builder instance.</param> /// <param name="registryProviders">One or more registry providers.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder UseRegistryProvider(this IIbanNetOptionsBuilder builder, params IIbanRegistryProvider[] registryProviders) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (registryProviders is null) { throw new ArgumentNullException(nameof(registryProviders)); } if (registryProviders.Length == 0) { throw new ArgumentException(Resources.One_or_more_providers_is_required, nameof(registryProviders)); } builder.UseRegistry(new IbanRegistry { Providers = registryProviders }); return(builder); }
public static IEnumerable <object[]> NullArgumentTestCases() { IIbanNetOptionsBuilder instance = GetBuilderStub(); return(new NullArgumentTestCases { DelegateTestCase.Create <IIbanNetOptionsBuilder, Action <IbanValidatorOptions>, IIbanNetOptionsBuilder>( IbanNetOptionsBuilderExtensions.Configure, instance, _ => { }), DelegateTestCase.Create( IbanNetOptionsBuilderExtensions.WithRule, instance, typeof(TestValidationRule)), DelegateTestCase.Create( IbanNetOptionsBuilderExtensions.UseRegistry, instance, Mock.Of <IIbanRegistry>()), DelegateTestCase.Create( IbanNetOptionsBuilderExtensions.UseRegistryProvider, instance, new IIbanRegistryProvider[0]), DelegateTestCase.Create( IbanNetOptionsBuilderExtensions.WithRule <TestValidationRule>, instance), DelegateTestCase.Create( IbanNetOptionsBuilderExtensions.UseStrictValidation, instance), DelegateTestCase.Create( IbanNetOptionsBuilderExtensions.UseLooseValidation, instance), DelegateTestCase.Create <IIbanNetOptionsBuilder, Func <TestValidationRule>, IIbanNetOptionsBuilder>( IbanNetOptionsBuilderExtensions.WithRule, instance, () => new TestValidationRule()) }.Flatten()); }
/// <summary> /// Configures the <see cref="IbanValidator" /> to use loose validation. /// </summary> /// <param name="builder">The builder instance.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder UseLooseValidation(this IIbanNetOptionsBuilder builder) { return(builder.UseValidationMethod(ValidationMethod.Loose)); }
/// <summary> /// Configures the <see cref="IbanValidator" /> to use strict validation. /// </summary> /// <param name="builder">The builder instance.</param> /// <returns>The <see cref="IIbanNetOptionsBuilder" /> so that additional calls can be chained.</returns> public static IIbanNetOptionsBuilder UseStrictValidation(this IIbanNetOptionsBuilder builder) { return(builder.UseValidationMethod(ValidationMethod.Strict)); }
protected IbanNetOptionsBuilderTests() { _builder = GetBuilderStub(); _builderStub = Mock.Get(_builder); }