Skip to content

Commit

Permalink
Added more test cases and nuget package
Browse files Browse the repository at this point in the history
  • Loading branch information
harmann-labs committed Apr 4, 2017
1 parent 57dee4b commit ff15c1f
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,5 @@ ModelManifest.xml

# FAKE - F# Make
.fake/
/src/SpecFlow.AdvanceSteps.SpecFlowPlugin/push.ps1
/src/SpecFlow.AdvanceSteps.SpecFlowPlugin/push.ps1
15 changes: 15 additions & 0 deletions src/SpecFlow.AdvanceSteps.SpecFlowPlugin/AdvanceStepsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpecFlow.AdvanceSteps
{
public class AdvanceStepsException : Exception
{
public AdvanceStepsException(string message) : base(message)
{
}
}
}
8 changes: 8 additions & 0 deletions src/SpecFlow.AdvanceSteps.SpecFlowPlugin/App.config.transform
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<specFlow>
<plugins>
<add name="SpecFlow.PeekSteps" type="Runtime" />
</plugins>
</specFlow>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SpecFlow.AdvanceSteps.SpecFlowPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("Provide access to SpecFlow steps through extension methods in ScenarioContext and SceanrioInfo. Also allows performing regression using step statements")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCompany("harvinders")]
[assembly: AssemblyProduct("SpecFlow.AdvanceSteps.SpecFlowPlugin")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static IEnumerable<StepDefinition> GetAllSteps(this ScenarioContext conte
{
if (!ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].PeekingEnabled)
{
throw new Exception($"Please set enable-peeking tag on the scenario before attempting to call the method {nameof(GetAllSteps)}");
throw new TagNotSetException("enable-peeking",nameof(GetAllSteps));
}

return ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].Steps.Where( def => !string.IsNullOrEmpty(def.Text));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public static StepDefinition CurrentStep(this ScenarioStepContext context)
if (!(ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].PeekingEnabled
|| ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].RegressionEnabled))
{
throw new Exception($"Please set enable-peeking tag on the scenario before attempting to call the method {nameof(CurrentStep)}");
throw new TagNotSetException("enable-peeking", nameof(CurrentStep));

}
return ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].CurrentStep;
}
Expand All @@ -22,7 +23,8 @@ public static StepDefinition NextStep(this ScenarioStepContext context)
if (!(ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].PeekingEnabled
|| ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].RegressionEnabled))
{
throw new Exception($"Please set enable-peeking tag on the scenario before attempting to call the method {nameof(NextStep)}");
throw new TagNotSetException("enable-peeking", nameof(NextStep));

}
return ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].NextStep;
}
Expand All @@ -32,7 +34,7 @@ public static StepDefinition PreviousStep(this ScenarioStepContext context)
if (!(ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].PeekingEnabled
|| ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].RegressionEnabled))
{
throw new Exception($"Please set enable-peeking tag on the scenario before attempting to call the method {nameof(PreviousStep)}");
throw new TagNotSetException("enable-peeking", nameof(PreviousStep));
}
return ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].PreviousStep;
}
Expand All @@ -41,15 +43,12 @@ public static void RegisterRepeatCount(this ScenarioStepContext context, string
{
if (!ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].RegressionEnabled)
{
throw new Exception($"Please set enable-regression tag on the scenario before attempting to call the method {nameof(RegisterRepeatCount)}");
throw new TagNotSetException("enable-regression", nameof(RegisterRepeatCount));

}

var contextDictionary = ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].RepeatContext;

if (contextDictionary.ContainsKey(repeatContextName))
{
throw new ArgumentException($"Repeat context with the name {repeatContextName} is already present, please use a different name");
}
contextDictionary[repeatContextName] = new RepeatContext()
{
Count = count,
Expand All @@ -61,7 +60,7 @@ public static void DecrementRepeatCount(this ScenarioStepContext context, string
{
if (!ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].RegressionEnabled)
{
throw new Exception($"Please set enable-regression tag on the scenario before attempting to call the method {nameof(DecrementRepeatCount)}");
throw new TagNotSetException("enable-regression", nameof(DecrementRepeatCount));
}

var contextDictionary = ExecutionContextContainer.Contexts[Thread.CurrentThread.ManagedThreadId].RepeatContext;
Expand All @@ -77,7 +76,7 @@ public static void DecrementRepeatCount(this ScenarioStepContext context, string
}
else
{
throw new Exception($"Repeat context with the name {repeatContextName} is not yet set, please call 'RegisterRepeatCount' to set it");
throw new AdvanceStepsException($"Repeat context with the name {repeatContextName} is not yet set, please call 'RegisterRepeatCount' to set it");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,23 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AdvanceStepsException.cs" />
<Compile Include="AdvanceStepsPlugin.cs" />
<Compile Include="ExecutionContext.cs" />
<Compile Include="ExecutionContextContainer.cs" />
<Compile Include="ScenarioContextExtentions.cs" />
<Compile Include="ScenarioStepContextExtension.cs" />
<Compile Include="StepDefinition.cs" />
<Compile Include="TagNotSetException.cs" />
<Compile Include="TestRunner.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\README.md">
<Link>README.md</Link>
</None>
<Content Include="app.config.install.xdt" />
<Content Include="app.config.uninstall.xdt" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
25 changes: 25 additions & 0 deletions src/SpecFlow.AdvanceSteps.SpecFlowPlugin/TagNotSetException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpecFlow.AdvanceSteps
{
public class TagNotSetException:Exception
{
private readonly string tagName;
private readonly string methodName;

public TagNotSetException(string tagName, string methodName)
{
this.tagName = tagName;
this.methodName = methodName;
}

public override string ToString()
{
return $"Please set '{this.tagName}' tag on the scenario before attempting to call the method '{this.methodName}'";
}
}
}
9 changes: 9 additions & 0 deletions src/SpecFlow.AdvanceSteps.SpecFlowPlugin/TestRunner.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using BoDi;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Bindings;
Expand Down Expand Up @@ -125,17 +126,25 @@ public void OnScenarioEnd()
this.executionContext.NextStep = null != node.Next?.Value.Text ? node.Next?.Value : null;

node.Value.Action(ExecutionEngine);
Trace.WriteLine($"executed statement {node.Value.Text}");

var endStep =
this.executionContext.RepeatContext.FirstOrDefault(
p => p.Value.EndStepDefinition == this.executionContext.CurrentStep);

if(null != endStep.Key)
Trace.WriteLine($"which is an end statement");


if (null != endStep.Key && 0 != endStep.Value.Count)
{
node = this.executionContext.Steps.Find(endStep.Value.BeginStepDefinition);

if (null == node)
throw new Exception("Shit happened");

Trace.WriteLine($"pointing back to statement {node.Value.Text}");

}
} while (null != (node = node.Next));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<specFlow xdt:Transform="InsertIfMissing" />
<specFlow>
<plugins xdt:Transform="InsertIfMissing" />
<plugins>
<add name="SpecFlow.PeekSteps" type="Runtime" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" />
</plugins>
</specFlow>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<specFlow>
<plugins>
<add name="SpecFlow.PeekSteps" type="Runtime" xdt:Transform="Remove" xdt:Locator="Match(name)" />
</plugins>
</specFlow>
</configuration>
5 changes: 5 additions & 0 deletions src/SpecFlow.AdvanceSteps.SpecFlowPlugin/pack.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Get-ChildItem *.csproj -Recurse | ForEach-Object { Remove-Item *.nupkg }
Get-ChildItem *.csproj -Recurse | ForEach-Object { & nuget.exe pack $_ -Symbols -IncludeReferencedProjects -Prop Configuration=Release -Prop Platform=AnyCPU}
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")


Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class RegressionSteps
private readonly List<int> numbers = new List<int>();
private int result = 0;
private int count;
private int addCount;
public RegressionSteps(ScenarioContext scenarioContext)
{
this.scenarioContext = scenarioContext;
Expand All @@ -26,7 +27,6 @@ public void GivenIHaveACalculator()
[Given(@"I would like to perform the following steps (.*) time")]
public void GivenIWouldLikeToPerformTheFollowingStepsTime(int count)
{
//ScenarioContext.Current.StepContext.RegisterRepeatCount("steps", count);
this.scenarioContext.StepContext.RegisterRepeatCount("steps", count);
}

Expand Down Expand Up @@ -62,5 +62,19 @@ public void ThenICloseTheCalculatorAndVerifyStepsExecutedTimes(int p0)
Assert.Equal(this.count, p0);
}


[Given(@"I would like to add a number (.*) times")]
public void GivenIWouldLikeToAddANumberTimes(int p0)
{
this.addCount++;
this.scenarioContext.StepContext.RegisterRepeatCount("addSteps", p0);
}

[Then(@"I repeat the add steps")]
public void ThenIRepeatTheAddSteps()
{
this.scenarioContext.StepContext.DecrementRepeatCount("addSteps");
}

}
}
15 changes: 15 additions & 0 deletions tests/SpecFlow.AdvanceSteps.Integration.Tests/Regression.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@ Scenario: Add two numbers
Then the result should be 120 on the screen
And I repeat the steps

Then I close the calculator and verify steps executed 3 times

@enable-regression
Scenario: Add numbers many times
Given I have a calculator
And I would like to perform the following steps 3 time

Given I would like to add a number 4 times
Given I have entered 50 into the calculator
When I press add
Then I repeat the add steps

Then the result should be 200 on the screen
And I repeat the steps

Then I close the calculator and verify steps executed 3 times

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ff15c1f

Please sign in to comment.