-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41ada18
commit d671008
Showing
8 changed files
with
321 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Lindi.Core.Bindings; | ||
using Lindi.Core.Linq; | ||
using Lindi.Tests.Core.Linq; | ||
using Xunit; | ||
using static Lindi.Core.LindiMethods; | ||
|
||
namespace Lindi.Tests.Core.Bindings | ||
{ | ||
/// <summary> | ||
/// Tests for <see cref="InjectValuesInto{TInjectedInto}"/>. | ||
/// </summary> | ||
public class InjectValuesIntoTests | ||
{ | ||
[Fact] | ||
public void Test_Inject_Wraps_Thrown_Exceptions_In_BindingResolutionException() | ||
{ | ||
var injection = Inject() | ||
.Where(value => InjectedInto(value) as INeedSample) | ||
.Select(value => | ||
{ | ||
value.Sample = null; | ||
throw new Exception(); | ||
}); | ||
|
||
Assert.Throws<BindingResolutionException>(() => | ||
{ | ||
INeedSample needSample = new NeedSample(); | ||
injection.Inject(needSample); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Test_Inject_Does_Not_ReWrap_BindingResolutionException() | ||
{ | ||
BindingResolutionException ex = new BindingResolutionException(typeof(INeedSample), new Exception()); | ||
bool caught = false; | ||
var injection = Inject() | ||
.Where(value => InjectedInto(value) as INeedSample) | ||
.Select(value => | ||
{ | ||
value.Sample = null; | ||
throw ex; | ||
}); | ||
|
||
try | ||
{ | ||
INeedSample needSample = new NeedSample(); | ||
injection.Inject(needSample); | ||
} | ||
catch (BindingResolutionException e) | ||
{ | ||
Assert.Same(ex, e); | ||
caught = true; | ||
} | ||
|
||
Assert.True(caught); | ||
} | ||
} | ||
} |
72 changes: 0 additions & 72 deletions
72
src/LINDI.Tests/Core/Bindings/LazyBindToConstructorTests.cs
This file was deleted.
Oops, something went wrong.
153 changes: 153 additions & 0 deletions
153
src/LINDI.Tests/Core/Bindings/LazyConstructorBindingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using Lindi.Core; | ||
using Lindi.Core.Bindings; | ||
using Xunit; | ||
|
||
namespace Lindi.Tests.Core.Bindings | ||
{ | ||
/// <summary> | ||
/// Tests for <see cref="LazyConstructorBinding{TInterface}"/>. | ||
/// </summary> | ||
public class LazyConstructorBindingTests | ||
{ | ||
[Fact] | ||
public void Test_Constructor_Throws_When_Given_Null_Dependencies() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
LazyConstructorBinding<object> binding = new LazyConstructorBinding<object>(null, bindings => new object()); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Test_Constructor_Throws_When_Given_Null_Construction_Expression() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
LazyConstructorBinding<object> binding = new LazyConstructorBinding<object>(new IBinding[0], null); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Test_Resolve_Implementation_Builds_Expression() | ||
{ | ||
var lazyBinding = new LazyConstructorBinding<ISample>(new IBinding[0], bindings => new Sample()); | ||
|
||
Assert.False(lazyBinding.IsBuilt); | ||
|
||
ISample sample = lazyBinding.Resolve(); | ||
|
||
Assert.IsType<Sample>(sample); | ||
Assert.True(lazyBinding.IsBuilt); | ||
} | ||
|
||
[Fact] | ||
public void Test_Resolve_Implementation_Flattens_Multiple_Lazy_Bindings() | ||
{ | ||
LazyConstructorBinding<IHasSample> lazyBinding = new LazyConstructorBinding<IHasSample>(new IBinding[] | ||
{ | ||
new LazyConstructorBinding<ISample>(new IBinding[0], bindings => new Sample()) | ||
}, bindings => new HasSample(((LazyConstructorBinding<ISample>)bindings[0]).Resolve())); | ||
|
||
IHasSample sample = lazyBinding.Resolve(); | ||
|
||
Assert.IsType<HasSample>(sample); | ||
Assert.IsType<ConstructorBinding<IHasSample>>(lazyBinding.Constructor); | ||
} | ||
|
||
[Fact] | ||
public void Test_Resolve_Implementation_Handles_Non_Lazy_Bindings_Correctly() | ||
{ | ||
IBinding<ISample> binding = new ConstructorBinding<ISample>(() => new Sample()); | ||
LazyConstructorBinding<IHasSample> lazyBinding = new LazyConstructorBinding<IHasSample>( | ||
new IBinding[0], | ||
bindings => new HasSample(binding.Resolve())); | ||
|
||
IHasSample sample = lazyBinding.Resolve(); | ||
|
||
Assert.IsType<HasSample>(sample); | ||
Assert.IsType<ConstructorBinding<IHasSample>>(lazyBinding.Constructor); | ||
} | ||
|
||
[Fact] | ||
public void Test_Resolve_Implementation_Handles_Binding_Inside_Initializer() | ||
{ | ||
IBinding<ISample> binding = new LazyConstructorBinding<ISample>(new IBinding[] | ||
{ | ||
new LazyConstructorBinding<object>(new IBinding[0], bindings => new Sample()) | ||
}, bindings => new Sample | ||
{ | ||
Obj = ((ILazyConstructorBinding<object>)bindings[0]).Resolve() | ||
}); | ||
|
||
ISample sample = binding.Resolve(); | ||
|
||
Assert.IsType<Sample>(sample); | ||
Assert.IsType<Sample>(((Sample)sample).Obj); | ||
} | ||
|
||
[Fact] | ||
public void Test_Resolve_Throws_InvalidOperationException_When_Dependencies_Have_Not_Been_Set() | ||
{ | ||
DerivedLazyConstructorBinding binding = new DerivedLazyConstructorBinding(); | ||
binding.ConstructionExpression = bindings => new object(); | ||
BindingResolutionException ex = Assert.Throws<BindingResolutionException>(() => | ||
{ | ||
object obj = binding.Resolve(); | ||
}); | ||
|
||
Assert.NotNull(ex.InnerException); | ||
Assert.IsType<InvalidOperationException>(ex.InnerException); | ||
} | ||
|
||
[Fact] | ||
public void Test_Resolve_Throws_InvalidOperationException_When_ConstructionExpression_Has_Not_Been_Set() | ||
{ | ||
DerivedLazyConstructorBinding binding = new DerivedLazyConstructorBinding(); | ||
binding.Dependencies = new IBinding[0]; | ||
BindingResolutionException ex = Assert.Throws<BindingResolutionException>(() => | ||
{ | ||
object obj = binding.Resolve(); | ||
}); | ||
|
||
Assert.NotNull(ex.InnerException); | ||
Assert.IsType<InvalidOperationException>(ex.InnerException); | ||
} | ||
|
||
class DerivedLazyConstructorBinding : LazyConstructorBinding<object> | ||
{ | ||
public new IEnumerable<IBinding> Dependencies | ||
{ | ||
get | ||
{ | ||
return base.Dependencies; | ||
} | ||
set | ||
{ | ||
base.Dependencies = value; | ||
} | ||
} | ||
|
||
public new Expression<Func<IBinding[], object>> ConstructionExpression | ||
{ | ||
get | ||
{ | ||
return base.ConstructionExpression; | ||
} | ||
set | ||
{ | ||
base.ConstructionExpression = value; | ||
} | ||
} | ||
|
||
public DerivedLazyConstructorBinding() : base() | ||
{ | ||
|
||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/LINDI.Tests/Core/Bindings/ReferenceScopedBindingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Lindi.Core.Bindings; | ||
using Xunit; | ||
using static Lindi.Core.LindiMethods; | ||
|
||
namespace Lindi.Tests.Core.Bindings | ||
{ | ||
/// <summary> | ||
/// Tests for <see cref="ReferenceScopedBinding{TInterface,TValue}"/>. | ||
/// </summary> | ||
public class ReferenceScopedBindingTests | ||
{ | ||
[Fact] | ||
public void Test_Constructor_Throws_When_Given_Null_Value_Selector() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
ReferenceScopedBinding<object, object> binding = new ReferenceScopedBinding<object, object>(null); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Test_SetBinding_Throws_When_Given_Null_Binding() | ||
{ | ||
ReferenceScopedBinding<IHasSample, ISample> binding = new ReferenceScopedBinding<IHasSample, ISample>(() => null); | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
binding.SetBinding(null); | ||
}); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Lindi.Core.Bindings; | ||
using Xunit; | ||
|
||
namespace Lindi.Tests.Core.Bindings | ||
{ | ||
/// <summary> | ||
/// Tests for <see cref="ValueScopedBinding{TInterface,TValue}"/>. | ||
/// </summary> | ||
public class ValueScopedBindingTests | ||
{ | ||
[Fact] | ||
public void Test_Constructor_Throws_When_Given_Null_Value_Selector() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
ValueScopedBinding<object, bool> binding = new ValueScopedBinding<object, bool>(null); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Test_SetBinding_Throws_When_Given_Null_Binding() | ||
{ | ||
ValueScopedBinding<IHasSample, bool> binding = new ValueScopedBinding<IHasSample, bool>(() => true); | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
binding.SetBinding(null); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.