From d67100831be6f2ccdcfcbbb98f7db411e87e0aab Mon Sep 17 00:00:00 2001 From: Kallyn Gowdy Date: Sat, 8 Aug 2015 18:49:01 -0400 Subject: [PATCH] Add Tests for Better Coverage --- ...torTests.cs => ConstructorBindingTests.cs} | 11 +- .../Core/Bindings/InjectValuesIntoTests.cs | 64 ++++++++ .../Bindings/LazyBindToConstructorTests.cs | 72 --------- .../Bindings/LazyConstructorBindingTests.cs | 153 ++++++++++++++++++ .../Bindings/ReferenceScopedBindingTests.cs | 37 +++++ .../Core/Bindings/ValueScopedBindingTests.cs | 35 ++++ src/LINDI.Tests/Core/Linq/LinqGroupByTests.cs | 33 ++-- src/LINDI.Tests/LINDI.Tests.csproj | 7 +- 8 files changed, 321 insertions(+), 91 deletions(-) rename src/LINDI.Tests/Core/Bindings/{BindToConstructorTests.cs => ConstructorBindingTests.cs} (66%) create mode 100644 src/LINDI.Tests/Core/Bindings/InjectValuesIntoTests.cs delete mode 100644 src/LINDI.Tests/Core/Bindings/LazyBindToConstructorTests.cs create mode 100644 src/LINDI.Tests/Core/Bindings/LazyConstructorBindingTests.cs create mode 100644 src/LINDI.Tests/Core/Bindings/ReferenceScopedBindingTests.cs create mode 100644 src/LINDI.Tests/Core/Bindings/ValueScopedBindingTests.cs diff --git a/src/LINDI.Tests/Core/Bindings/BindToConstructorTests.cs b/src/LINDI.Tests/Core/Bindings/ConstructorBindingTests.cs similarity index 66% rename from src/LINDI.Tests/Core/Bindings/BindToConstructorTests.cs rename to src/LINDI.Tests/Core/Bindings/ConstructorBindingTests.cs index 02cbdf0..6851f69 100644 --- a/src/LINDI.Tests/Core/Bindings/BindToConstructorTests.cs +++ b/src/LINDI.Tests/Core/Bindings/ConstructorBindingTests.cs @@ -7,7 +7,7 @@ namespace Lindi.Tests.Core.Bindings /// /// Tests for . /// - public class BindToConstructorTests + public class ConstructorBindingTests { [Fact] public void Test_Resolve_Uses_Given_Constructor_Function() @@ -23,5 +23,14 @@ public void Test_Resolve_Uses_Given_Constructor_Function() Assert.Same(value, obj); } + + [Fact] + public void Test_Constructor_Throws_When_Given_Null_Function() + { + Assert.Throws(() => + { + ConstructorBinding binding = new ConstructorBinding(null); + }); + } } } diff --git a/src/LINDI.Tests/Core/Bindings/InjectValuesIntoTests.cs b/src/LINDI.Tests/Core/Bindings/InjectValuesIntoTests.cs new file mode 100644 index 0000000..9b194eb --- /dev/null +++ b/src/LINDI.Tests/Core/Bindings/InjectValuesIntoTests.cs @@ -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 +{ + /// + /// Tests for . + /// + 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(() => + { + 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); + } + } +} diff --git a/src/LINDI.Tests/Core/Bindings/LazyBindToConstructorTests.cs b/src/LINDI.Tests/Core/Bindings/LazyBindToConstructorTests.cs deleted file mode 100644 index 756f601..0000000 --- a/src/LINDI.Tests/Core/Bindings/LazyBindToConstructorTests.cs +++ /dev/null @@ -1,72 +0,0 @@ -using Lindi.Core; -using Lindi.Core.Bindings; -using Xunit; - -namespace Lindi.Tests.Core.Bindings -{ - /// - /// Tests for . - /// - public class LazyBindToConstructorTests - { - - [Fact] - public void Test_Resolve_Implementation_Builds_Expression() - { - var lazyBinding = new LazyConstructorBinding(new IBinding[0], bindings => new Sample()); - - Assert.False(lazyBinding.IsBuilt); - - ISample sample = lazyBinding.Resolve(); - - Assert.IsType(sample); - Assert.True(lazyBinding.IsBuilt); - } - - [Fact] - public void Test_Resolve_Implementation_Flattens_Multiple_Lazy_Bindings() - { - LazyConstructorBinding lazyBinding = new LazyConstructorBinding(new IBinding[] - { - new LazyConstructorBinding(new IBinding[0], bindings => new Sample()) - }, bindings => new HasSample(((LazyConstructorBinding)bindings[0]).Resolve())); - - IHasSample sample = lazyBinding.Resolve(); - - Assert.IsType(sample); - Assert.IsType>(lazyBinding.Constructor); - } - - [Fact] - public void Test_Resolve_Implementation_Handles_Non_Lazy_Bindings_Correctly() - { - IBinding binding = new ConstructorBinding(() => new Sample()); - LazyConstructorBinding lazyBinding = new LazyConstructorBinding( - new IBinding[0], - bindings => new HasSample(binding.Resolve())); - - IHasSample sample = lazyBinding.Resolve(); - - Assert.IsType(sample); - Assert.IsType>(lazyBinding.Constructor); - } - - [Fact] - public void Test_Resolve_Implementation_Handles_Binding_Inside_Initializer() - { - IBinding binding = new LazyConstructorBinding(new IBinding[] - { - new LazyConstructorBinding(new IBinding[0], bindings => new Sample()) - }, bindings => new Sample - { - Obj = ((ILazyConstructorBinding)bindings[0]).Resolve() - }); - - ISample sample = binding.Resolve(); - - Assert.IsType(sample); - Assert.IsType(((Sample)sample).Obj); - } - - } -} diff --git a/src/LINDI.Tests/Core/Bindings/LazyConstructorBindingTests.cs b/src/LINDI.Tests/Core/Bindings/LazyConstructorBindingTests.cs new file mode 100644 index 0000000..0d20c63 --- /dev/null +++ b/src/LINDI.Tests/Core/Bindings/LazyConstructorBindingTests.cs @@ -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 +{ + /// + /// Tests for . + /// + public class LazyConstructorBindingTests + { + [Fact] + public void Test_Constructor_Throws_When_Given_Null_Dependencies() + { + Assert.Throws(() => + { + LazyConstructorBinding binding = new LazyConstructorBinding(null, bindings => new object()); + }); + } + + [Fact] + public void Test_Constructor_Throws_When_Given_Null_Construction_Expression() + { + Assert.Throws(() => + { + LazyConstructorBinding binding = new LazyConstructorBinding(new IBinding[0], null); + }); + } + + [Fact] + public void Test_Resolve_Implementation_Builds_Expression() + { + var lazyBinding = new LazyConstructorBinding(new IBinding[0], bindings => new Sample()); + + Assert.False(lazyBinding.IsBuilt); + + ISample sample = lazyBinding.Resolve(); + + Assert.IsType(sample); + Assert.True(lazyBinding.IsBuilt); + } + + [Fact] + public void Test_Resolve_Implementation_Flattens_Multiple_Lazy_Bindings() + { + LazyConstructorBinding lazyBinding = new LazyConstructorBinding(new IBinding[] + { + new LazyConstructorBinding(new IBinding[0], bindings => new Sample()) + }, bindings => new HasSample(((LazyConstructorBinding)bindings[0]).Resolve())); + + IHasSample sample = lazyBinding.Resolve(); + + Assert.IsType(sample); + Assert.IsType>(lazyBinding.Constructor); + } + + [Fact] + public void Test_Resolve_Implementation_Handles_Non_Lazy_Bindings_Correctly() + { + IBinding binding = new ConstructorBinding(() => new Sample()); + LazyConstructorBinding lazyBinding = new LazyConstructorBinding( + new IBinding[0], + bindings => new HasSample(binding.Resolve())); + + IHasSample sample = lazyBinding.Resolve(); + + Assert.IsType(sample); + Assert.IsType>(lazyBinding.Constructor); + } + + [Fact] + public void Test_Resolve_Implementation_Handles_Binding_Inside_Initializer() + { + IBinding binding = new LazyConstructorBinding(new IBinding[] + { + new LazyConstructorBinding(new IBinding[0], bindings => new Sample()) + }, bindings => new Sample + { + Obj = ((ILazyConstructorBinding)bindings[0]).Resolve() + }); + + ISample sample = binding.Resolve(); + + Assert.IsType(sample); + Assert.IsType(((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(() => + { + object obj = binding.Resolve(); + }); + + Assert.NotNull(ex.InnerException); + Assert.IsType(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(() => + { + object obj = binding.Resolve(); + }); + + Assert.NotNull(ex.InnerException); + Assert.IsType(ex.InnerException); + } + + class DerivedLazyConstructorBinding : LazyConstructorBinding + { + public new IEnumerable Dependencies + { + get + { + return base.Dependencies; + } + set + { + base.Dependencies = value; + } + } + + public new Expression> ConstructionExpression + { + get + { + return base.ConstructionExpression; + } + set + { + base.ConstructionExpression = value; + } + } + + public DerivedLazyConstructorBinding() : base() + { + + } + } + } +} diff --git a/src/LINDI.Tests/Core/Bindings/ReferenceScopedBindingTests.cs b/src/LINDI.Tests/Core/Bindings/ReferenceScopedBindingTests.cs new file mode 100644 index 0000000..cac23d5 --- /dev/null +++ b/src/LINDI.Tests/Core/Bindings/ReferenceScopedBindingTests.cs @@ -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 +{ + /// + /// Tests for . + /// + public class ReferenceScopedBindingTests + { + [Fact] + public void Test_Constructor_Throws_When_Given_Null_Value_Selector() + { + Assert.Throws(() => + { + ReferenceScopedBinding binding = new ReferenceScopedBinding(null); + }); + } + + [Fact] + public void Test_SetBinding_Throws_When_Given_Null_Binding() + { + ReferenceScopedBinding binding = new ReferenceScopedBinding(() => null); + Assert.Throws(() => + { + binding.SetBinding(null); + }); + } + + } +} diff --git a/src/LINDI.Tests/Core/Bindings/ValueScopedBindingTests.cs b/src/LINDI.Tests/Core/Bindings/ValueScopedBindingTests.cs new file mode 100644 index 0000000..6243234 --- /dev/null +++ b/src/LINDI.Tests/Core/Bindings/ValueScopedBindingTests.cs @@ -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 +{ + /// + /// Tests for . + /// + public class ValueScopedBindingTests + { + [Fact] + public void Test_Constructor_Throws_When_Given_Null_Value_Selector() + { + Assert.Throws(() => + { + ValueScopedBinding binding = new ValueScopedBinding(null); + }); + } + + [Fact] + public void Test_SetBinding_Throws_When_Given_Null_Binding() + { + ValueScopedBinding binding = new ValueScopedBinding(() => true); + Assert.Throws(() => + { + binding.SetBinding(null); + }); + } + } +} diff --git a/src/LINDI.Tests/Core/Linq/LinqGroupByTests.cs b/src/LINDI.Tests/Core/Linq/LinqGroupByTests.cs index 0f73355..5475b19 100644 --- a/src/LINDI.Tests/Core/Linq/LinqGroupByTests.cs +++ b/src/LINDI.Tests/Core/Linq/LinqGroupByTests.cs @@ -3,6 +3,7 @@ using Lindi.Core.Bindings; using Lindi.Core.Linq; using Xunit; +using static Lindi.Core.LindiMethods; using Thread = System.Threading.Thread; namespace Lindi.Tests.Core.Linq @@ -16,7 +17,7 @@ public class LinqGroupByTests public void Test_Grouped_Binding_Creates_New_Scoped_Binding_For_Given_Reference_Value() { object val = new object(); - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by val into scope select new Sample(); @@ -29,7 +30,7 @@ public void Test_Grouped_Binding_Can_Be_Resolved() { object val = new object(); Sample expectedValue = new Sample(); - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by val into scope select expectedValue; @@ -42,7 +43,7 @@ public void Test_Grouped_Binding_Can_Be_Resolved() public void Test_Grouped_Binding_Reuses_Value() { object val = new object(); - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by val into scope select new Sample(); @@ -58,7 +59,7 @@ public void Test_GroupBy_Throws_ArgumentNullException_WhenGivenNull() { Assert.Throws(() => { - IBinding binding = LindiMethods.Bind().GroupBy(null).Select(scope => new Sample()); + IBinding binding = Bind().GroupBy(null).Select(scope => new Sample()); }); } @@ -67,7 +68,7 @@ public void Test_Grouped_Binding_Produces_New_Value_When_Context_Value_Changes() { object val = new object(); - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by val into scope select new Sample(); @@ -91,7 +92,7 @@ public void Test_Grouped_Binding_Allows_Value_To_Be_Garbage_Collected() { object val = new object(); // Use value, this could be anything (HttpContext, Thread, etc.) - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by val into scope select new Sample(); @@ -109,7 +110,7 @@ public void Test_Grouped_Binding_Allows_Value_To_Be_Garbage_Collected() [Fact] public void Test_Grouping_By_True_Creates_New_ValueScopedBinding() { - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by true into scope select new Sample(); @@ -120,7 +121,7 @@ public void Test_Grouping_By_True_Creates_New_ValueScopedBinding() [Fact] public void Test_ValueScopedBinding_Can_Resolve_Values() { - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by true into scope select new Sample(); @@ -132,7 +133,7 @@ public void Test_ValueScopedBinding_Can_Resolve_Values() [Fact] public void Test_ValueScopedBinding_Produces_Same_Value_For_Same_Selected_Value() { - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by true into scope select new Sample(); @@ -148,7 +149,7 @@ public void Test_ValueScopedBinding_Produces_Different_Value_For_Different_Selec { bool b = true; Func selector = () => (b = !b); - IBinding binding = from type in LindiMethods.Bind() + IBinding binding = from type in Bind() group type by selector() into scope select new Sample(); @@ -162,8 +163,8 @@ public void Test_ValueScopedBinding_Produces_Different_Value_For_Different_Selec [Fact] public void Test_Group_By_Singleton_Produces_Same_Value_Every_Time() { - IBinding binding = from type in LindiMethods.Bind() - group type by LindiMethods.Singleton() into scope + IBinding binding = from type in Bind() + group type by Singleton() into scope select new Sample(); ISample value = binding.Resolve(); @@ -176,8 +177,8 @@ public void Test_Group_By_Singleton_Produces_Same_Value_Every_Time() [Fact] public void Test_Group_By_Thread_Produces_Same_Value_Inside_Same_Thread() { - IBinding binding = from type in LindiMethods.Bind() - group type by LindiMethods.Thread() into scope + IBinding binding = from type in Bind() + group type by Thread() into scope select new Sample(); ISample value = null; ISample otherValue = null; @@ -198,8 +199,8 @@ public void Test_Group_By_Thread_Produces_Same_Value_Inside_Same_Thread() [Fact] public void Test_Group_By_Thread_Produces_Different_Value_On_Different_Threads() { - IBinding binding = from type in LindiMethods.Bind() - group type by LindiMethods.Thread() into scope + IBinding binding = from type in Bind() + group type by Thread() into scope select new Sample(); ISample value = null; ISample otherValue = null; diff --git a/src/LINDI.Tests/LINDI.Tests.csproj b/src/LINDI.Tests/LINDI.Tests.csproj index 52c2b9c..5166b94 100644 --- a/src/LINDI.Tests/LINDI.Tests.csproj +++ b/src/LINDI.Tests/LINDI.Tests.csproj @@ -62,12 +62,15 @@ - + + + + - +