Skip to content
This repository has been archived by the owner on Jun 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #136 from adamralph/25
Browse files Browse the repository at this point in the history
#25 added step overloads with a step context parameter
  • Loading branch information
adamralph committed Mar 3, 2014
2 parents 30f1f5b + eaa3d93 commit 91c3d32
Show file tree
Hide file tree
Showing 18 changed files with 618 additions and 112 deletions.
9 changes: 6 additions & 3 deletions src/Xbehave.Net35/Xbehave.Net35.csproj
Expand Up @@ -66,9 +66,15 @@
<Compile Include="..\Xbehave.Net40\ContinueOnFailureAfterAttribute.cs">
<Link>ContinueOnFailureAfterAttribute.cs</Link>
</Compile>
<Compile Include="..\Xbehave.Net40\IStepContext.cs">
<Link>IStepContext.cs</Link>
</Compile>
<Compile Include="..\Xbehave.Net40\OmitArgumentsFromScenarioNamesAttribute.cs">
<Link>OmitArgumentsFromScenarioNamesAttribute.cs</Link>
</Compile>
<Compile Include="..\Xbehave.Net40\StepContext.cs">
<Link>StepContext.cs</Link>
</Compile>
<Compile Include="..\Xbehave.Net40\StepType.cs">
<Link>StepType.cs</Link>
</Compile>
Expand All @@ -91,9 +97,6 @@
<Compile Include="..\Xbehave.Net40\Fluent\Step.cs">
<Link>Fluent\Step.cs</Link>
</Compile>
<Compile Include="..\Xbehave.Net40\Helper.cs">
<Link>Helper.cs</Link>
</Compile>
<Compile Include="..\Xbehave.Net40\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
Expand Down
16 changes: 16 additions & 0 deletions src/Xbehave.Net40/DisposableExtensions.cs
Expand Up @@ -18,6 +18,7 @@ public static class DisposableExtensions
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="obj">The object to be disposed.</param>
/// <returns>The object.</returns>
[Obsolete("Use Using(IStep) instead. This deprecated version of the method will fail to register objects for disposal in async steps, in steps with timeouts, or when called from a thread other than the scenario execution thread.")]
public static T Using<T>(this T obj) where T : IDisposable
{
if (obj != null)
Expand All @@ -27,5 +28,20 @@ public static class DisposableExtensions

return obj;
}

/// <summary>
/// Immediately registers the <see cref="IDisposable"/> object for disposal after all steps in the current scenario have been executed.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="obj">The object to be disposed.</param>
/// <param name="stepContext">The execution context for the current step.</param>
/// <returns>The object.</returns>
public static T Using<T>(this T obj, IStepContext stepContext) where T : IDisposable
{
Guard.AgainstNullArgument("stepContext", stepContext);

stepContext.Using(obj);
return obj;
}
}
}
29 changes: 27 additions & 2 deletions src/Xbehave.Net40/Fluent/Step.cs
Expand Up @@ -5,16 +5,41 @@
namespace Xbehave.Fluent
{
using System;
#if NET45
using System.Threading.Tasks;
#endif
using Xbehave.Sdk;

internal partial class Step : IStep
{
private readonly Sdk.Step step;

public Step(Sdk.Step step)
public Step(string text, Action body, StepType stepType)
{
this.step = step;
this.step = CurrentScenario.AddStep(text, body, stepType);
}

public Step(string text, Action<IStepContext> body, StepType stepType)
{
var context = new StepContext();
this.step = CurrentScenario.AddStep(text, () => body(context), stepType);
context.Assign(this.step);
}

#if NET45
public Step(string text, Func<Task> body, StepType stepType)
{
this.step = CurrentScenario.AddStep(text, body, stepType);
}

public Step(string text, Func<IStepContext, Task> body, StepType stepType)
{
var context = new StepContext();
this.step = CurrentScenario.AddStep(text, () => body(context), stepType);
context.Assign(this.step);
}

#endif
public IStep And()
{
return this;
Expand Down
17 changes: 0 additions & 17 deletions src/Xbehave.Net40/Helper.cs

This file was deleted.

23 changes: 23 additions & 0 deletions src/Xbehave.Net40/IStepContext.cs
@@ -0,0 +1,23 @@
// <copyright file="IStepContext.cs" company="xBehave.net contributors">
// Copyright (c) xBehave.net contributors. All rights reserved.
// </copyright>

namespace Xbehave
{
using System;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// A scenario step context.
/// </summary>
public interface IStepContext
{
/// <summary>
/// Immediately registers the <see cref="IDisposable"/> object for disposal after all steps in the current scenario have been executed.
/// </summary>
/// <param name="disposable">The object to be disposed.</param>
/// <returns>The current <see cref="IStepContext"/>.</returns>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Using", Justification = "Makes sense here.")]
IStepContext Using(IDisposable disposable);
}
}
26 changes: 26 additions & 0 deletions src/Xbehave.Net40/StepContext.cs
@@ -0,0 +1,26 @@
// <copyright file="StepContext.cs" company="xBehave.net contributors">
// Copyright (c) xBehave.net contributors. All rights reserved.
// </copyright>

namespace Xbehave
{
using System;

internal partial class StepContext : IStepContext
{
private Sdk.Step step;

public void Assign(Sdk.Step step)
{
Guard.AgainstNullArgument("step", step);

this.step = step;
}

public IStepContext Using(IDisposable disposable)
{
this.step.AddDisposable(disposable);
return this;
}
}
}
99 changes: 88 additions & 11 deletions src/Xbehave.Net40/StepExtensions.cs
Expand Up @@ -6,9 +6,6 @@ namespace Xbehave
{
using System;
using System.Diagnostics.CodeAnalysis;

using Sdk;

using Xbehave.Fluent;

/// <summary>
Expand All @@ -29,11 +26,11 @@ public static partial class StepExtensions
[SuppressMessage(
"Microsoft.Globalization",
"CA1303:Do not pass literals as localized parameters",
MessageId = "Xbehave.Helper.AddStep(System.String,System.Action)",
MessageId = "Xbehave.new Step(System.String,System.Action)",
Justification = "Text must match method name.")]
public static IStep When(this IStep stepDefinition, string text, Action body)
{
return Helper.AddStep("When " + text, body, StepType.When);
return new Step("When " + text, body, StepType.When);
}

/// <summary>
Expand All @@ -49,11 +46,11 @@ public static IStep When(this IStep stepDefinition, string text, Action body)
[SuppressMessage(
"Microsoft.Globalization",
"CA1303:Do not pass literals as localized parameters",
MessageId = "Xbehave.Helper.AddStep(System.String,System.Action)",
MessageId = "Xbehave.new Step(System.String,System.Action)",
Justification = "Text must match method name.")]
public static IStep Then(this IStep stepDefinition, string text, Action body)
{
return Helper.AddStep("Then " + text, body, StepType.Then);
return new Step("Then " + text, body, StepType.Then);
}

/// <summary>
Expand All @@ -69,11 +66,11 @@ public static IStep Then(this IStep stepDefinition, string text, Action body)
[SuppressMessage(
"Microsoft.Globalization",
"CA1303:Do not pass literals as localized parameters",
MessageId = "Xbehave.Helper.AddStep(System.String,System.Action)",
MessageId = "Xbehave.new Step(System.String,System.Action)",
Justification = "Text must match method name.")]
public static IStep And(this IStep stepDefinition, string text, Action body)
{
return Helper.AddStep("And " + text, body, StepType.And);
return new Step("And " + text, body, StepType.And);
}

/// <summary>
Expand All @@ -89,11 +86,91 @@ public static IStep And(this IStep stepDefinition, string text, Action body)
[SuppressMessage(
"Microsoft.Globalization",
"CA1303:Do not pass literals as localized parameters",
MessageId = "Xbehave.Helper.AddStep(System.String,System.Action)",
MessageId = "Xbehave.new Step(System.String,System.Action)",
Justification = "Text must match method name.")]
public static IStep But(this IStep stepDefinition, string text, Action body)
{
return Helper.AddStep("But " + text, body, StepType.But);
return new Step("But " + text, body, StepType.But);
}

/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="stepDefinition">The step definition.</param>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "stepDefinition", Justification = "Part of fluent API.")]
[SuppressMessage(
"Microsoft.Globalization",
"CA1303:Do not pass literals as localized parameters",
MessageId = "Xbehave.new Step(System.String,System.Action)",
Justification = "Text must match method name.")]
public static IStep When(this IStep stepDefinition, string text, Action<IStepContext> body)
{
return new Step("When " + text, body, StepType.When);
}

/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="stepDefinition">The step definition.</param>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "stepDefinition", Justification = "Part of fluent API.")]
[SuppressMessage(
"Microsoft.Globalization",
"CA1303:Do not pass literals as localized parameters",
MessageId = "Xbehave.new Step(System.String,System.Action)",
Justification = "Text must match method name.")]
public static IStep Then(this IStep stepDefinition, string text, Action<IStepContext> body)
{
return new Step("Then " + text, body, StepType.Then);
}

/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="stepDefinition">The step definition.</param>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "stepDefinition", Justification = "Part of fluent API.")]
[SuppressMessage(
"Microsoft.Globalization",
"CA1303:Do not pass literals as localized parameters",
MessageId = "Xbehave.new Step(System.String,System.Action)",
Justification = "Text must match method name.")]
public static IStep And(this IStep stepDefinition, string text, Action<IStepContext> body)
{
return new Step("And " + text, body, StepType.And);
}

/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="stepDefinition">The step definition.</param>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "stepDefinition", Justification = "Part of fluent API.")]
[SuppressMessage(
"Microsoft.Globalization",
"CA1303:Do not pass literals as localized parameters",
MessageId = "Xbehave.new Step(System.String,System.Action)",
Justification = "Text must match method name.")]
public static IStep But(this IStep stepDefinition, string text, Action<IStepContext> body)
{
return new Step("But " + text, body, StepType.But);
}
}
}

0 comments on commit 91c3d32

Please sign in to comment.