Skip to content

Commit

Permalink
Add Tests for Better Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
KallynGowdy committed Aug 8, 2015
1 parent 41ada18 commit d671008
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Lindi.Tests.Core.Bindings
/// <summary>
/// Tests for <see cref="ConstructorBinding{TInterface}"/>.
/// </summary>
public class BindToConstructorTests
public class ConstructorBindingTests
{
[Fact]
public void Test_Resolve_Uses_Given_Constructor_Function()
Expand All @@ -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<ArgumentNullException>(() =>
{
ConstructorBinding<object> binding = new ConstructorBinding<object>(null);
});
}
}
}
64 changes: 64 additions & 0 deletions src/LINDI.Tests/Core/Bindings/InjectValuesIntoTests.cs
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 src/LINDI.Tests/Core/Bindings/LazyBindToConstructorTests.cs

This file was deleted.

153 changes: 153 additions & 0 deletions src/LINDI.Tests/Core/Bindings/LazyConstructorBindingTests.cs
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 src/LINDI.Tests/Core/Bindings/ReferenceScopedBindingTests.cs
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);
});
}

}
}
35 changes: 35 additions & 0 deletions src/LINDI.Tests/Core/Bindings/ValueScopedBindingTests.cs
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);
});
}
}
}

0 comments on commit d671008

Please sign in to comment.