Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify nesting counter #19

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 17 additions & 43 deletions src/NScenario/NestingCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,41 @@ namespace NScenario
{
internal class NestingCounter
{
private readonly ConcurrentDictionary<string, Stack<(bool, int)>> _levelNumber = new ConcurrentDictionary<string, Stack<(bool, int)>>();
public Level StartLevel(string levelKey)
class LevelCounter
{
_ = _levelNumber.AddOrUpdate(levelKey, s =>
{
var stack = new Stack<(bool, int)>();
stack.Push((true, 0));
return stack;
}, (s, stack) =>
{
var (used, counter) = stack.Pop();
stack.Push((true, counter));
if (used)
{
stack.Push((true, 0));
}

return stack;
});
var levelNumber = _levelNumber[levelKey].Count;
var currentLevelData = IncrementCurrentStepNumber(levelKey);
return new Level(() => EndLevel(levelKey, levelNumber), currentLevelData);
public int Counter { get; set; }
}

private void EndLevel(string scenario, int height)
{
if (_levelNumber.TryGetValue(scenario, out var stepStack))
{
if (height < stepStack.Count)
{
stepStack.Pop();
}
private readonly Stack<LevelCounter> _stack = new Stack<LevelCounter>();

var (_, counter) = stepStack.Pop();
stepStack.Push((false, counter));
}
public NestingCounter()
{
_stack.Push(new LevelCounter());
}

private Stack<(bool, int)> IncrementCurrentStepNumber(string scenario)
public Level StartLevel()
{
var stepStack = _levelNumber[scenario];
var (used, counter) = stepStack.Pop();
counter++;
stepStack.Push((used, counter));
return stepStack;
_stack.Peek().Counter++;
_stack.Push(new LevelCounter());
var data = _stack.ToArray().Skip(1).Reverse().Select(x => x.Counter).ToArray();
return new Level(() => _stack.Pop(), data);
}

}

public class Level:IDisposable
public class Level : IDisposable
{
public int Nesting { get; }
private readonly Action _action;

public Level(Action action, Stack<(bool, int)> levelData)
public Level(Action action, int[] levelData)
{
Nesting = levelData.Count -1;
LevelPath = levelData.ToArray().Reverse().Select(x => x.Item2);
Nesting = levelData.Length - 1;
LevelPath = levelData;
_action = action;
}

public IEnumerable<int> LevelPath { get; }
public IReadOnlyList<int> LevelPath { get; }

public void Dispose() => _action.Invoke();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ protected LevelTrackingScenarioStepExecutor(IScenarioStepExecutor scenarioStepEx

public async Task Step(string scenarioName, string stepDescription, Func<Task> action, StepContext stepContext)
{
using var level = _counter.StartLevel(stepContext.ScenarioName);
using var level = _counter.StartLevel();
var decoratedDescription = DecorateDescription(stepDescription, level);
await _scenarioStepExecutorImplementation.Step(scenarioName, decoratedDescription, action, stepContext);
}

public async Task Step(string scenarioName, string stepDescription, Action action, StepContext stepContext)
{
using var level = _counter.StartLevel(stepContext.ScenarioName);
using var level = _counter.StartLevel();
var decoratedDescription = DecorateDescription(stepDescription, level);
await _scenarioStepExecutorImplementation.Step(scenarioName, decoratedDescription, action, stepContext);
}
Expand Down
Loading