Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simplify deprecated MethodFilter overloads for SetUpTearDown behavior…

…s by no longer using Invoke objects for those behaviors. Simplify Invoke now that it only needs to work for test methods. CaseBehaviors, InstanceBehaviors, and TypeBehaviors never fail to handle exceptions at their respective levels.
  • Loading branch information...
commit 4535866daa271a84f08df84f3866f81e9c53153d 1 parent 3c82276
@plioi plioi authored
View
19 src/Fixie/Behaviors/ExecuteCases.cs
@@ -1,11 +1,26 @@
-namespace Fixie.Behaviors
+using System;
+
+namespace Fixie.Behaviors
{
public class ExecuteCases : InstanceBehavior
{
public void Execute(Fixture fixture)
{
foreach (var @case in fixture.Cases)
- fixture.CaseExecutionBehavior.Execute(@case, fixture.Instance);
+ {
+ try
+ {
+ fixture.CaseExecutionBehavior.Execute(@case, fixture.Instance);
+ }
+ catch (PreservedException preservedException)
+ {
+ @case.Exceptions.Add(preservedException.OriginalException);
+ }
+ catch (Exception ex)
+ {
+ @case.Exceptions.Add(ex);
+ }
+ }
}
}
}
View
17 src/Fixie/Behaviors/Invoke.cs
@@ -9,31 +9,26 @@ public class Invoke : CaseBehavior
{
public void Execute(Case @case, object instance)
{
- Execute(@case.Method, instance, @case.Exceptions);
- }
-
- public void Execute(MethodInfo method, object instance, ExceptionList exceptions)
- {
try
{
+ var method = @case.Method;
+
bool isDeclaredAsync = method.Async();
if (isDeclaredAsync && method.Void())
ThrowForUnsupportedAsyncVoid();
- bool invokeReturned = false;
- object result = null;
+ object result;
try
{
result = method.Invoke(instance, null);
- invokeReturned = true;
}
catch (TargetInvocationException ex)
{
throw new PreservedException(ex.InnerException);
}
- if (invokeReturned && isDeclaredAsync)
+ if (isDeclaredAsync)
{
var task = (Task)result;
try
@@ -48,11 +43,11 @@ public void Execute(MethodInfo method, object instance, ExceptionList exceptions
}
catch (PreservedException preservedException)
{
- exceptions.Add(preservedException.OriginalException);
+ @case.Exceptions.Add(preservedException.OriginalException);
}
catch (Exception ex)
{
- exceptions.Add(ex);
+ @case.Exceptions.Add(ex);
}
}
View
19 src/Fixie/Conventions/CaseBehaviorBuilder.cs
@@ -1,4 +1,5 @@
using System;
+using System.Reflection;
using Fixie.Behaviors;
namespace Fixie.Conventions
@@ -46,14 +47,16 @@ public CaseBehaviorBuilder SetUpTearDown(MethodFilter setUpMethods, MethodFilter
static void InvokeAll(MethodFilter methodFilter, Case @case, object instance)
{
- var invoke = new Invoke();
- int countBefore = @case.Exceptions.Count;
foreach (var method in methodFilter.Filter(@case.Class))
{
- invoke.Execute(method, instance, @case.Exceptions);
-
- if (@case.Exceptions.Count > countBefore)
- break;
+ try
+ {
+ method.Invoke(instance, null);
+ }
+ catch (TargetInvocationException ex)
+ {
+ throw new PreservedException(ex.InnerException);
+ }
}
}
@@ -74,6 +77,10 @@ public void Execute(Case @case, object instance)
{
outer(@case, instance, () => inner.Execute(@case, instance));
}
+ catch (PreservedException preservedException)
+ {
+ @case.Exceptions.Add(preservedException.OriginalException);
+ }
catch (Exception exception)
{
@case.Exceptions.Add(exception);
View
64 src/Fixie/Conventions/InstanceBehaviorBuilder.cs
@@ -1,4 +1,5 @@
using System;
+using System.Reflection;
using Fixie.Behaviors;
namespace Fixie.Conventions
@@ -32,56 +33,23 @@ public InstanceBehaviorBuilder SetUpTearDown(Action<Fixture> setUp, Action<Fixtu
public InstanceBehaviorBuilder SetUpTearDown(MethodFilter setUpMethods, MethodFilter tearDownMethods)
{
- return SetUpTearDown(fixture =>
- {
- //NOTE: We want to just throw like normal if InvokeAll ever experiences an exception,
- // allowing WrapBehavior's own try/catch to handle it correctly.
- // As is, if the inner behavior doesn't happen to short-cicuit case-running
- // for cases that have already failed, we could run the case even after
- // failed setup.
- //
- // The use of WrappedExceptions is a workaround which only fixes the
- // immediate issue, but suggests that all exception handling should
- // be done by throwing a wrapper exception instead of explicitly
- // passing around ExceptionLists as return values.
-
- var setUpExceptions = InvokeAll(setUpMethods, fixture.TestClass, fixture.Instance);
- if (setUpExceptions.Any())
- throw new WrappedExceptions(setUpExceptions);
- },
- fixture =>
- {
- var tearDownExceptions = InvokeAll(tearDownMethods, fixture.TestClass, fixture.Instance);
- if (tearDownExceptions.Any())
- {
- foreach (var @case in fixture.Cases)
- @case.Exceptions.Add(tearDownExceptions);
- }
- });
- }
-
- class WrappedExceptions : Exception
- {
- public WrappedExceptions(ExceptionList innerExceptions)
- : base("See InnerExceptions.")
- {
- InnerExceptions = innerExceptions;
- }
-
- public ExceptionList InnerExceptions { get; private set; }
+ return SetUpTearDown(fixture => InvokeAll(setUpMethods, fixture.TestClass, fixture.Instance),
+ fixture => InvokeAll(tearDownMethods, fixture.TestClass, fixture.Instance));
}
- static ExceptionList InvokeAll(MethodFilter methodFilter, Type type, object instance)
+ static void InvokeAll(MethodFilter methodFilter, Type type, object instance)
{
- var exceptions = new ExceptionList();
- var invoke = new Invoke();
foreach (var method in methodFilter.Filter(type))
{
- invoke.Execute(method, instance, exceptions);
- if (exceptions.Count > 0)
- break;
+ try
+ {
+ method.Invoke(instance, null);
+ }
+ catch (TargetInvocationException ex)
+ {
+ throw new PreservedException(ex.InnerException);
+ }
}
- return exceptions;
}
class WrapBehavior : InstanceBehavior
@@ -101,19 +69,15 @@ public void Execute(Fixture fixture)
{
outer(fixture, () => inner.Execute(fixture));
}
- catch (WrappedExceptions exceptions)
+ catch (PreservedException preservedException)
{
foreach (var @case in fixture.Cases)
- {
- @case.Exceptions.Add(exceptions.InnerExceptions);
- }
+ @case.Exceptions.Add(preservedException.OriginalException);
}
catch (Exception exception)
{
foreach (var @case in fixture.Cases)
- {
@case.Exceptions.Add(exception);
- }
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.