Skip to content

Commit

Permalink
tests complete for #563
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Apr 18, 2023
1 parent b288578 commit 9db74c1
Showing 1 changed file with 69 additions and 3 deletions.
72 changes: 69 additions & 3 deletions test/DryIoc.UnitTests/RequiredPropertiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public int Run()
{
Can_inject_required_properties();
Should_throw_for_unresolved_required_property();
Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties();
return 3;
Should_skip_required_property_injection_when_using_ctor_which_SetsRequiredProperties();
Works_with_constructor_selector();
Works_with_open_generic_registration_and_constructor_selector_which_SetsRequiredProperties();
return 5;
}

[Test]
Expand Down Expand Up @@ -49,7 +51,7 @@ public void Should_throw_for_unresolved_required_property()
}

[Test]
public void Should_skip_required_property_injection_when_using_ctor_with_SetsRequiredProperties()
public void Should_skip_required_property_injection_when_using_ctor_which_SetsRequiredProperties()
{
var c = new Container(Rules.Default.With(propertiesAndFields: PropertiesAndFields.RequiredProperties()));

Expand All @@ -64,6 +66,44 @@ public void Should_skip_required_property_injection_when_using_ctor_with_SetsReq
Assert.NotNull(x.B);
}

[Test]
public void Works_with_constructor_selector()
{
var c = new Container(
Rules.Default.With(
factoryMethod: FactoryMethod.ConstructorWithResolvableArguments,
propertiesAndFields: PropertiesAndFields.RequiredProperties()));

c.Register<SSS>();

c.Register<A>();
c.Register<B>();

var x = c.Resolve<SSS>();

Assert.NotNull(x.A);
Assert.NotNull(x.B);
}

[Test]
public void Works_with_open_generic_registration_and_constructor_selector_which_SetsRequiredProperties()
{
var c = new Container(
Rules.Default.With(
factoryMethod: FactoryMethod.ConstructorWithResolvableArguments,
propertiesAndFields: PropertiesAndFields.RequiredProperties()));

c.Register(typeof(SS<>));
c.Register(typeof(B<>));
c.Register<A>();

var x = c.Resolve<SS<A>>();

Assert.Null(x.A);
Assert.NotNull(x.B);
Assert.IsInstanceOf<A>(x.B.A);
}

public class A {}
public class B {}
public class C {}
Expand All @@ -90,6 +130,32 @@ public class SS
[SetsRequiredMembers]
public SS(B b) => B = b;
}

public class SSS
{
public required A A { get; init; }
public B B { get; private set; }

public SSS(B b) => B = b;

public SSS() => B = null;
}

public class B<T>
{
public required T A { get; init; }
}

public class SS<T>
{
public required A A { get; init; }
public B<T> B { get; private set; }

[SetsRequiredMembers]
public SS(B<T> b) => B = b;

public SS() => B = null;
}
#endif
}
}

0 comments on commit 9db74c1

Please sign in to comment.