Skip to content

Commit

Permalink
Simplify nesting counter
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Nov 23, 2023
1 parent 47ae2db commit 39582cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 45 deletions.
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

0 comments on commit 39582cb

Please sign in to comment.