From 39582cb040c97967916dc915c9cc1c0afaf4e6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Pi=C4=85tek?= Date: Thu, 23 Nov 2023 22:15:58 +0100 Subject: [PATCH] Simplify nesting counter --- src/NScenario/NestingCounter.cs | 60 ++++++------------- .../LevelTrackingScenarioStepExecutor.cs | 4 +- 2 files changed, 19 insertions(+), 45 deletions(-) diff --git a/src/NScenario/NestingCounter.cs b/src/NScenario/NestingCounter.cs index 16784d4..e8519e3 100644 --- a/src/NScenario/NestingCounter.cs +++ b/src/NScenario/NestingCounter.cs @@ -7,67 +7,41 @@ namespace NScenario { internal class NestingCounter { - private readonly ConcurrentDictionary> _levelNumber = new ConcurrentDictionary>(); - 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 _stack = new Stack(); - 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 LevelPath { get; } + public IReadOnlyList LevelPath { get; } public void Dispose() => _action.Invoke(); } diff --git a/src/NScenario/StepExecutors/LevelTrackingScenarioStepExecutor.cs b/src/NScenario/StepExecutors/LevelTrackingScenarioStepExecutor.cs index 68cf6cc..476c37c 100644 --- a/src/NScenario/StepExecutors/LevelTrackingScenarioStepExecutor.cs +++ b/src/NScenario/StepExecutors/LevelTrackingScenarioStepExecutor.cs @@ -16,14 +16,14 @@ protected LevelTrackingScenarioStepExecutor(IScenarioStepExecutor scenarioStepEx public async Task Step(string scenarioName, string stepDescription, Func 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); }