@@ -9,9 +9,46 @@ namespace BddDotNet.Internal.Services;
99internal sealed class StepExecutionService (
1010 IServiceProvider serviceProvider ,
1111 IEnumerable < Step > steps ,
12- IEnumerable < IArgumentTransformation > argumentTransformations )
12+ IEnumerable < IArgumentTransformation > argumentTransformations ,
13+ IEnumerable < IBeforeStep > beforeStepHooks ,
14+ IEnumerable < IAfterStep > afterStepHook )
1315{
1416 public async Task ExecuteAsync ( StepType stepType , string text , object ? [ ] additionalStepArguments )
17+ {
18+ var keyword = stepType . ToString ( ) ;
19+
20+ await BeforeStep ( ) ;
21+
22+ try
23+ {
24+ await FindAndExecuteStepAsync ( stepType , text , additionalStepArguments ) ;
25+ }
26+ catch ( Exception exception )
27+ {
28+ await AfterStep ( exception . GetBaseException ( ) ) ;
29+ throw ;
30+ }
31+
32+ await AfterStep ( null ) ;
33+ }
34+
35+ private async Task BeforeStep ( )
36+ {
37+ foreach ( var beforeStepHook in beforeStepHooks )
38+ {
39+ await beforeStepHook . BeforeStep ( ) ;
40+ }
41+ }
42+
43+ private async Task AfterStep ( Exception ? exception )
44+ {
45+ foreach ( var afterStepHook in afterStepHook . Reverse ( ) )
46+ {
47+ await afterStepHook . AfterStep ( exception ) ;
48+ }
49+ }
50+
51+ private async Task FindAndExecuteStepAsync ( StepType stepType , string text , object ? [ ] additionalStepArguments )
1552 {
1653 var ( step , match ) = FindGherkinStep ( stepType , text ) ;
1754
0 commit comments