Skip to content

Commit

Permalink
refactor: replace CurrentTopLevelStep with CurrentTopLevelStepDefinit…
Browse files Browse the repository at this point in the history
…ionType & rename methods
  • Loading branch information
gasparnagy committed Oct 13, 2016
1 parent 43368b1 commit 6188082
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
25 changes: 15 additions & 10 deletions TechTalk.SpecFlow/Infrastructure/ContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.Linq;
using BoDi;
using TechTalk.SpecFlow.Bindings;
using TechTalk.SpecFlow.Tracing;

namespace TechTalk.SpecFlow.Infrastructure
Expand Down Expand Up @@ -81,12 +82,12 @@ public TContext Instance
get { return instances.Any()?instances.Peek():null; }
}

public void Init(TContext newInstance)
public void Push(TContext newInstance)
{
instances.Push(newInstance);
}

public void Cleanup()
public void RemoveTop()
{
if (!instances.Any())
{
Expand All @@ -95,14 +96,18 @@ public void Cleanup()
}
var instance = instances.Pop();
((IDisposable)instance).Dispose();

}

public void Dispose()
{
Reset();
}

public void Reset()
{
while (instances.Any())
{
Cleanup();
RemoveTop();
}
}
}
Expand All @@ -117,7 +122,7 @@ public void Dispose()
/// <summary>
/// Holds the ScenarioStepContext of the last step that was executed from the actual featrure file, and not any steps that were executed during the calling of a step
/// </summary>
public ScenarioStepContext CurrentTopLevelStep { get; private set; }
public StepDefinitionType? CurrentTopLevelStepDefinitionType { get; private set; }

public ContextManager(ITestTracer testTracer, IObjectContainer testThreadContainer, IContainerBuilder containerBuilder)
{
Expand Down Expand Up @@ -167,8 +172,9 @@ public void InitializeScenarioContext(ScenarioInfo scenarioInfo)

private void ResetCurrentStepStack()
{
stepContextManager.Reset();
stepDepth = 0;
CurrentTopLevelStep = null;
CurrentTopLevelStepDefinitionType = null;
ScenarioStepContext.Current = null;
}

Expand All @@ -181,13 +187,12 @@ public void InitializeStepContext(StepInfo stepInfo)
{
stepDepth++;
var newContext = new ScenarioStepContext(stepInfo);
stepContextManager.Init(newContext);
stepContextManager.Push(newContext);
if (ExecutingTopLevelStep())
{
CurrentTopLevelStep = newContext;
CurrentTopLevelStepDefinitionType = stepInfo.StepDefinitionType;
}
ScenarioStepContext.Current = newContext;

}

private bool ExecutingTopLevelStep()
Expand All @@ -197,7 +202,7 @@ private bool ExecutingTopLevelStep()

public void CleanupStepContext()
{
stepContextManager.Cleanup();
stepContextManager.RemoveTop();

ScenarioStepContext.Current = stepContextManager.Instance;
stepDepth--;
Expand Down
3 changes: 2 additions & 1 deletion TechTalk.SpecFlow/Infrastructure/IContextManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Globalization;
using System.Text;
using TechTalk.SpecFlow.Bindings;

namespace TechTalk.SpecFlow.Infrastructure
{
Expand All @@ -10,7 +11,7 @@ public interface IContextManager
FeatureContext FeatureContext { get; }
ScenarioContext ScenarioContext { get; }
ScenarioStepContext StepContext { get; }
ScenarioStepContext CurrentTopLevelStep { get; }
StepDefinitionType? CurrentTopLevelStepDefinitionType { get; }

void InitializeFeatureContext(FeatureInfo featureInfo, CultureInfo bindingCulture);
void CleanupFeatureContext();
Expand Down
2 changes: 1 addition & 1 deletion TechTalk.SpecFlow/Infrastructure/TestExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public void Step(StepDefinitionKeyword stepDefinitionKeyword, string keyword, st

private StepDefinitionType GetCurrentBindingType()
{
return contextManager.CurrentTopLevelStep?.StepInfo.StepDefinitionType ?? StepDefinitionType.Given;
return contextManager.CurrentTopLevelStepDefinitionType ?? StepDefinitionType.Given;
}

#endregion
Expand Down
42 changes: 21 additions & 21 deletions Tests/TechTalk.SpecFlow.RuntimeTests/ScenarioStepContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ public void ShouldReportCorrectCurrentTopLevelStep()
var contextManager = container.Resolve<IContextManager>();
var firstStepInfo = new StepInfo(StepDefinitionType.Given, "I have called initialise once", null, string.Empty);
contextManager.InitializeStepContext(firstStepInfo);
Assert.AreEqual(firstStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.Given, contextManager.CurrentTopLevelStepDefinitionType); // firstStepInfo
var secondStepInfo = new StepInfo(StepDefinitionType.When, "I have called initialise twice", null, string.Empty);
contextManager.InitializeStepContext(secondStepInfo);
Assert.AreEqual(firstStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
contextManager.CleanupStepContext();
Assert.AreEqual(firstStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
contextManager.CleanupStepContext();
Assert.AreEqual(firstStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.Given, contextManager.CurrentTopLevelStepDefinitionType); // firstStepInfo
contextManager.CleanupStepContext(); // remove second
Assert.AreEqual(StepDefinitionType.Given, contextManager.CurrentTopLevelStepDefinitionType); // firstStepInfo
contextManager.CleanupStepContext(); // remove first
Assert.AreEqual(StepDefinitionType.Given, contextManager.CurrentTopLevelStepDefinitionType); // firstStepInfo
}

[Test]
Expand All @@ -112,13 +112,13 @@ public void ShouldReportCorrectCurrentTopLevelStepIfWeHaveExecutedMoreThan1Step(
var contextManager = container.Resolve<IContextManager>();
var firstStepInfo = new StepInfo(StepDefinitionType.Given, "I have called initialise once", null, string.Empty);
contextManager.InitializeStepContext(firstStepInfo);
Assert.AreEqual(firstStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.Given, contextManager.CurrentTopLevelStepDefinitionType); // firstStepInfo
contextManager.CleanupStepContext();
var secondStepInfo = new StepInfo(StepDefinitionType.When, "I have called initialise twice", null, string.Empty);
contextManager.InitializeStepContext(secondStepInfo);
Assert.AreEqual(secondStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.When, contextManager.CurrentTopLevelStepDefinitionType); // secondStepInfo
contextManager.CleanupStepContext();
Assert.AreEqual(secondStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.When, contextManager.CurrentTopLevelStepDefinitionType); // secondStepInfo
}

[Test]
Expand All @@ -130,25 +130,25 @@ public void ShouldReportCorrectCurrentTopLevelStepIfWeHaveStepsMoreThan1Deep()
var contextManager = container.Resolve<IContextManager>();
var firstStepInfo = new StepInfo(StepDefinitionType.Given, "I have called initialise once", null, string.Empty);
contextManager.InitializeStepContext(firstStepInfo);
Assert.AreEqual(firstStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.Given, contextManager.CurrentTopLevelStepDefinitionType); // firstStepInfo
contextManager.CleanupStepContext();
var secondStepInfo = new StepInfo(StepDefinitionType.When, "I have called initialise twice", null, string.Empty);
contextManager.InitializeStepContext(secondStepInfo);
Assert.AreEqual(secondStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
var thirdStepInfo = new StepInfo(StepDefinitionType.When, "I have called initialise a third time", null, string.Empty);
Assert.AreEqual(StepDefinitionType.When, contextManager.CurrentTopLevelStepDefinitionType); // secondStepInfo
var thirdStepInfo = new StepInfo(StepDefinitionType.Given, "I have called initialise a third time", null, string.Empty);
contextManager.InitializeStepContext(thirdStepInfo); //Call sub step
Assert.AreEqual(secondStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
var fourthStepInfo = new StepInfo(StepDefinitionType.When, "I have called initialise a forth time", null, string.Empty);
Assert.AreEqual(StepDefinitionType.When, contextManager.CurrentTopLevelStepDefinitionType); // secondStepInfo
var fourthStepInfo = new StepInfo(StepDefinitionType.Then, "I have called initialise a forth time", null, string.Empty);
contextManager.InitializeStepContext(fourthStepInfo); //call sub step of sub step
contextManager.CleanupStepContext(); // return from sub step of sub step
Assert.AreEqual(secondStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.When, contextManager.CurrentTopLevelStepDefinitionType); // secondStepInfo
contextManager.CleanupStepContext(); // return from sub step
Assert.AreEqual(secondStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.When, contextManager.CurrentTopLevelStepDefinitionType); // secondStepInfo
contextManager.CleanupStepContext(); // finish 2nd step
Assert.AreEqual(secondStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
var fifthStepInfo = new StepInfo(StepDefinitionType.When, "I have called initialise a fifth time", null, string.Empty);
Assert.AreEqual(StepDefinitionType.When, contextManager.CurrentTopLevelStepDefinitionType); // secondStepInfo
var fifthStepInfo = new StepInfo(StepDefinitionType.Then, "I have called initialise a fifth time", null, string.Empty);
contextManager.InitializeStepContext(fifthStepInfo);
Assert.AreEqual(fifthStepInfo, contextManager.CurrentTopLevelStep.StepInfo);
Assert.AreEqual(StepDefinitionType.Then, contextManager.CurrentTopLevelStepDefinitionType); // fifthStepInfo
}

[Test]
Expand All @@ -158,7 +158,7 @@ public void TopLevelStepShouldBeNullInitially()
var mockTracer = new Mock<ITestTracer>();
TestObjectFactories.CreateTestRunner(out container, objectContainer => objectContainer.RegisterInstanceAs(mockTracer.Object));
var contextManager = container.Resolve<IContextManager>();
Assert.IsNull(contextManager.CurrentTopLevelStep);
Assert.IsNull(contextManager.CurrentTopLevelStepDefinitionType);
}

[Test]
Expand All @@ -174,7 +174,7 @@ public void ScenarioStartShouldResetTopLevelStep()

contextManager.InitializeScenarioContext(new ScenarioInfo("my scenario"));

Assert.IsNull(contextManager.CurrentTopLevelStep);
Assert.IsNull(contextManager.CurrentTopLevelStepDefinitionType);
}

[Test]
Expand Down

0 comments on commit 6188082

Please sign in to comment.