Skip to content

Commit

Permalink
added example of metadata based Validate filtering for #577
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed May 26, 2023
1 parent 8859960 commit 49315e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace DryIoc.IssuesTests
Expand All @@ -12,8 +13,9 @@ public int Run()
Test_factory_extension_method_for_the_factory_with_the_service_key();

Test_factory_extension_method_GHIssue577();
Test_factory_extension_method_GHIssue577_exclude_from_Validate_via_metadata();

return 3;
return 4;
}

[Test]
Expand Down Expand Up @@ -93,6 +95,47 @@ public void Test_factory_extension_method_GHIssue577()
Assert.AreEqual(typeof(Foo).FullName, ((SerilogLogger)foo.Logger).TypeName);
}

[Test]
public void Test_factory_extension_method_GHIssue577_exclude_from_Validate_via_metadata()
{
var container = new Container();

var loggerFactory = new SerilogLoggerFactory();

container.RegisterInstance<ILoggerFactory>(loggerFactory);

container.Register(
Made.Of(_ => ServiceInfo.Of<ILoggerFactory>(),
f => f.CreateLogger(null)),
setup: Setup.With(condition: r => r.Parent.ImplementationType == null));

var excludeFromValidation = "exclude-from-validation";
container.Register(
Made.Of(_ => ServiceInfo.Of<ILoggerFactory>(),
f => f.CreateLogger(Arg.Index<Type>(0)),
r => r.Parent.ImplementationType),
setup: Setup.With(condition: r => r.Parent.ImplementationType != null,
metadataOrFuncOfMetadata: excludeFromValidation));

container.Register<Foo>();

var errors = container.Validate(reg =>
{
var metadata = reg.Factory.Setup.Metadata;
return metadata == null
|| metadata is IDictionary<string, object> d && !d.ContainsKey(excludeFromValidation)
|| !Equals(metadata, excludeFromValidation);
});

Assert.IsEmpty(errors);

var log = container.Resolve<ILogger>();
Assert.IsNull(((SerilogLogger)log).TypeName);

var foo = container.Resolve<Foo>();
Assert.AreEqual(typeof(Foo).FullName, ((SerilogLogger)foo.Logger).TypeName);
}

public sealed class Foo
{
public readonly ILogger Logger;
Expand Down
4 changes: 2 additions & 2 deletions test/DryIoc.TestRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class Program
{
public static void Main()
{
RunAllTests();
// RunAllTests();

// new GHIssue576_Extension_methods_not_being_handled_correctly_in_MadeOf_service_returning_expression().Run();
new GHIssue576_Extension_methods_not_being_handled_correctly_in_MadeOf_service_returning_expression().Run();

// new GHIssue116_ReOpened_DryIoc_Resolve_with_decorators_goes_wrong_for_parallel_execution().Run();
// new RequiredPropertiesTests().Run();
Expand Down

0 comments on commit 49315e7

Please sign in to comment.