Skip to content

Commit

Permalink
Improve and Organize Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KallynGowdy committed Aug 8, 2015
1 parent 687994b commit 41ada18
Show file tree
Hide file tree
Showing 18 changed files with 186 additions and 115 deletions.
5 changes: 5 additions & 0 deletions src/LINDI.Core/LindiMethods.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -48,6 +49,7 @@ public static IBinding<object> Inject()
/// <returns>default(TInterface)</returns>
/// <exception cref="ArgumentNullException"><paramref name="binding"/> is <see langword="null" />.</exception>
[DependencyMethod]
[ExcludeFromCodeCoverage]
public static TInterface AsDependency<TInterface>([NotNull] this IBinding<TInterface> binding)
{
if (binding == null) throw new ArgumentNullException(nameof(binding));
Expand All @@ -64,17 +66,20 @@ public static TInterface AsDependency<TInterface>([NotNull] this IBinding<TInter
/// <returns>default(TInterface)</returns>
/// <exception cref="ArgumentNullException"><paramref name="binding"/> is <see langword="null" />.</exception>
[DependencyMethod]
[ExcludeFromCodeCoverage]
public static TInterface Dependency<TInterface>([NotNull] IBinding<TInterface> binding)
{
if (binding == null) throw new ArgumentNullException(nameof(binding));
return default(TInterface);
}

[ExcludeFromCodeCoverage]
public static object InjectedInto<TInterface>(TInterface value)
{
return null;
}

[ExcludeFromCodeCoverage]
public static TInjectedInto IsInjectedInto<TInjectedInto>()
{
return default(TInjectedInto);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lindi.Core;
using Lindi.Core.Linq;
using Xunit;
using Xunit.Abstractions;
using static Lindi.Core.LindiMethods;

namespace Lindi.Tests
namespace Lindi.Tests.Core
{
public class AwaitableBindingTests
{

[Fact]
public async void Test_Binding_Is_Awaitable()
{
var binding = from value in Bind<ISample>()
var binding = from value in LindiMethods.Bind<ISample>()
select new Sample();

var v = await binding;
Expand Down
35 changes: 35 additions & 0 deletions src/LINDI.Tests/Core/Bindings/BaseBindingTests.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
{
class MockBaseBinding<TInterafce> : BaseBinding<TInterafce>
{
protected override TInterafce ResolveImplementation()
{
throw new Exception();
}
}

/// <summary>
/// Tests for <see cref="BaseBinding{TInterface}"/>.
/// </summary>
public class BaseBindingTests
{
[Fact]
public void Test_BaseBinding_Wraps_Exception_In_BindingResolutionException()
{
MockBaseBinding<ISample> binding = new MockBaseBinding<ISample>();

Assert.Throws<BindingResolutionException>(() =>
{
var sample = binding.Resolve();
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
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
namespace Lindi.Tests.Core.Bindings
{
/// <summary>
/// Tests for <see cref="ConstructorBinding{TInterface}"/>.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
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
namespace Lindi.Tests.Core.Bindings
{
/// <summary>
/// Tests for <see cref="BindingResolutionException"/>.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lindi.Core;
using Lindi.Core;
using Lindi.Core.Bindings;
using Xunit;

namespace Lindi.Tests
namespace Lindi.Tests.Core.Bindings
{
/// <summary>
/// Tests for <see cref="LazyConstructorBinding{TInterface}"/>.
Expand Down
31 changes: 31 additions & 0 deletions src/LINDI.Tests/Core/ExpressionHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq.Expressions;
using Lindi.Core;
using Xunit;

namespace Lindi.Tests.Core
{
/// <summary>
/// Tests for <see cref="ExpressionHelpers"/> methods.
/// </summary>
public class ExpressionHelpersTests
{
[Fact]
public void Test_Lock_Throws_When_Given_Null_LockObj_Expression()
{
Assert.Throws<ArgumentNullException>(() =>
{
ExpressionHelpers.Lock(null, Expression.Parameter(typeof(int)));
});
}

[Fact]
public void Test_Lock_Throws_When_Given_Null_Body_Expression()
{
Assert.Throws<ArgumentNullException>(() =>
{
ExpressionHelpers.Lock(Expression.Parameter(typeof(int)), null);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Lindi.Tests
namespace Lindi.Tests.Core
{
public class HasSample : IHasSample
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Lindi.Tests
namespace Lindi.Tests.Core
{
public interface IHasSample
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Lindi.Tests
namespace Lindi.Tests.Core
{
public interface ISample
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lindi.Core;
using Lindi.Core.Bindings;
using Lindi.Core.Linq;
using Xunit;
using static Lindi.Core.LindiMethods;
using Thread = System.Threading.Thread;

namespace Lindi.Tests
namespace Lindi.Tests.Core.Linq
{
/// <summary>
/// Tests for <see cref="BindingExtensions.GroupBy{TInterface,TValue}"/>.
Expand All @@ -21,7 +16,7 @@ public class LinqGroupByTests
public void Test_Grouped_Binding_Creates_New_Scoped_Binding_For_Given_Reference_Value()
{
object val = new object();
IBinding<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by val into scope
select new Sample();

Expand All @@ -34,7 +29,7 @@ public void Test_Grouped_Binding_Can_Be_Resolved()
{
object val = new object();
Sample expectedValue = new Sample();
IBinding<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by val into scope
select expectedValue;

Expand All @@ -47,7 +42,7 @@ public void Test_Grouped_Binding_Can_Be_Resolved()
public void Test_Grouped_Binding_Reuses_Value()
{
object val = new object();
IBinding<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by val into scope
select new Sample();

Expand All @@ -63,7 +58,7 @@ public void Test_GroupBy_Throws_ArgumentNullException_WhenGivenNull()
{
Assert.Throws<ArgumentNullException>(() =>
{
IBinding<ISample> binding = Bind<ISample>().GroupBy<ISample, object>(null).Select(scope => new Sample());
IBinding<ISample> binding = LindiMethods.Bind<ISample>().GroupBy<ISample, object>(null).Select(scope => new Sample());
});
}

Expand All @@ -72,7 +67,7 @@ public void Test_Grouped_Binding_Produces_New_Value_When_Context_Value_Changes()
{
object val = new object();

IBinding<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by val into scope
select new Sample();

Expand All @@ -96,7 +91,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<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by val into scope
select new Sample();
Expand All @@ -114,7 +109,7 @@ public void Test_Grouped_Binding_Allows_Value_To_Be_Garbage_Collected()
[Fact]
public void Test_Grouping_By_True_Creates_New_ValueScopedBinding()
{
IBinding<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by true into scope
select new Sample();

Expand All @@ -125,7 +120,7 @@ public void Test_Grouping_By_True_Creates_New_ValueScopedBinding()
[Fact]
public void Test_ValueScopedBinding_Can_Resolve_Values()
{
IBinding<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by true into scope
select new Sample();

Expand All @@ -137,7 +132,7 @@ public void Test_ValueScopedBinding_Can_Resolve_Values()
[Fact]
public void Test_ValueScopedBinding_Produces_Same_Value_For_Same_Selected_Value()
{
IBinding<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by true into scope
select new Sample();

Expand All @@ -153,7 +148,7 @@ public void Test_ValueScopedBinding_Produces_Different_Value_For_Different_Selec
{
bool b = true;
Func<bool> selector = () => (b = !b);
IBinding<ISample> binding = from type in Bind<ISample>()
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by selector() into scope
select new Sample();

Expand All @@ -167,8 +162,8 @@ public void Test_ValueScopedBinding_Produces_Different_Value_For_Different_Selec
[Fact]
public void Test_Group_By_Singleton_Produces_Same_Value_Every_Time()
{
IBinding<ISample> binding = from type in Bind<ISample>()
group type by Singleton() into scope
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by LindiMethods.Singleton() into scope
select new Sample();

ISample value = binding.Resolve();
Expand All @@ -181,8 +176,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<ISample> binding = from type in Bind<ISample>()
group type by Thread() into scope
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by LindiMethods.Thread() into scope
select new Sample();
ISample value = null;
ISample otherValue = null;
Expand All @@ -203,8 +198,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<ISample> binding = from type in Bind<ISample>()
group type by Thread() into scope
IBinding<ISample> binding = from type in LindiMethods.Bind<ISample>()
group type by LindiMethods.Thread() into scope
select new Sample();
ISample value = null;
ISample otherValue = null;
Expand Down
Loading

0 comments on commit 41ada18

Please sign in to comment.