11package testframework
22
33import (
4+ "context"
45 "fmt"
5-
66 "github.com/cucumber/godog"
77)
88
99// initializeContextSteps registers evaluation context step definitions
10- func initializeContextSteps (ctx * godog.ScenarioContext , state * TestState ) {
11- ctx .Step (`^a context containing a key "([^"]*)", with type "([^"]*)" and with value "([^"]*)"$` , state .addContextValue )
12- ctx .Step (`^an empty context$` , state .clearContext )
13- ctx .Step (`^a context with the following keys:$` , state .addContextFromTable )
10+ func InitializeContextSteps (ctx * godog.ScenarioContext ) {
11+ ctx .Step (`^a context containing a key "([^"]*)", with type "([^"]*)" and with value "([^"]*)"$` ,
12+ withState3Args ((* TestState ).addContextValue ))
13+ ctx .Step (`^an empty context$` ,
14+ withStateNoArgs ((* TestState ).clearContext ))
15+ ctx .Step (`^a context with the following keys:$` ,
16+ withStateTable ((* TestState ).addContextFromTable ))
1417
1518 // Missing step definitions - added as stubs
16- ctx .Step (`^a context containing a nested property with outer key "([^"]*)" and inner key "([^"]*)", with value "([^"]*)"$` , state .addNestedContextProperty )
17- ctx .Step (`^a context containing a targeting key with value "([^"]*)"$` , state .addTargetingKeyToContext )
19+ ctx .Step (`^a context containing a nested property with outer key "([^"]*)" and inner key "([^"]*)", with value "([^"]*)"$` ,
20+ withState3Args ((* TestState ).addNestedContextProperty ))
21+ ctx .Step (`^a context containing a targeting key with value "([^"]*)"$` ,
22+ withState1Arg ((* TestState ).addTargetingKeyToContext ))
23+
24+ // Context validation steps
25+ ctx .Step (`^the context should contain key "([^"]*)"$` ,
26+ withState1Arg ((* TestState ).contextContainsKeyStep ))
27+ ctx .Step (`^the context should be empty$` ,
28+ withStateNoArgs ((* TestState ).contextIsEmptyStep ))
29+ ctx .Step (`^the context should have (\d+) keys?$` ,
30+ withStateIntArg ((* TestState ).contextShouldHaveKeysStep ))
31+ ctx .Step (`^the context value for "([^"]*)" should be "([^"]*)" of type "([^"]*)"$` ,
32+ withState3Args ((* TestState ).contextValueShouldBeStep ))
33+ }
34+
35+ // Additional helper functions for different signatures
36+ func withState1Arg (fn func (* TestState , context.Context , string ) error ) func (context.Context , string ) error {
37+ return func (ctx context.Context , arg1 string ) error {
38+ state := GetStateFromContext (ctx )
39+ if state == nil {
40+ return fmt .Errorf ("test state not found in context" )
41+ }
42+ return fn (state , ctx , arg1 )
43+ }
44+ }
45+
46+ func withStateTable (fn func (* TestState , context.Context , * godog.Table ) error ) func (context.Context , * godog.Table ) error {
47+ return func (ctx context.Context , table * godog.Table ) error {
48+ state := GetStateFromContext (ctx )
49+ if state == nil {
50+ return fmt .Errorf ("test state not found in context" )
51+ }
52+ return fn (state , ctx , table )
53+ }
1854}
1955
56+ func withStateIntArg (fn func (* TestState , context.Context , int ) error ) func (context.Context , int ) error {
57+ return func (ctx context.Context , arg1 int ) error {
58+ state := GetStateFromContext (ctx )
59+ if state == nil {
60+ return fmt .Errorf ("test state not found in context" )
61+ }
62+ return fn (state , ctx , arg1 )
63+ }
64+ }
65+
66+ // State methods - these now expect context as first parameter after state
67+
2068// addContextValue adds a typed value to the evaluation context
21- func (s * TestState ) addContextValue (key , valueType , value string ) error {
69+ func (s * TestState ) addContextValue (ctx context. Context , key , valueType , value string ) error {
2270 convertedValue , err := convertValueForSteps (value , valueType )
2371 if err != nil {
2472 return fmt .Errorf ("failed to convert context value for key %s: %w" , key , err )
@@ -29,13 +77,13 @@ func (s *TestState) addContextValue(key, valueType, value string) error {
2977}
3078
3179// clearContext removes all values from the evaluation context
32- func (s * TestState ) clearContext () error {
80+ func (s * TestState ) clearContext (ctx context. Context ) error {
3381 s .EvalContext = make (map [string ]interface {})
3482 return nil
3583}
3684
3785// addContextFromTable adds multiple context values from a Gherkin data table
38- func (s * TestState ) addContextFromTable (table * godog.Table ) error {
86+ func (s * TestState ) addContextFromTable (ctx context. Context , table * godog.Table ) error {
3987 if len (table .Rows ) < 2 {
4088 return fmt .Errorf ("table must have at least header row and one data row" )
4189 }
@@ -72,7 +120,7 @@ func (s *TestState) addContextFromTable(table *godog.Table) error {
72120 valueType := row .Cells [typeCol ].Value
73121 value := row .Cells [valueCol ].Value
74122
75- if err := s .addContextValue (key , valueType , value ); err != nil {
123+ if err := s .addContextValue (ctx , key , valueType , value ); err != nil {
76124 return err
77125 }
78126 }
@@ -138,28 +186,20 @@ func (s *TestState) contextIsEmpty() error {
138186 return nil
139187}
140188
141- // Additional step definitions for context validation
142-
143- // registerContextValidationSteps adds validation steps for evaluation context
144- func (s * TestState ) registerContextValidationSteps (ctx * godog.ScenarioContext ) {
145- ctx .Step (`^the context should contain key "([^"]*)"$` , s .contextContainsKeyStep )
146- ctx .Step (`^the context should be empty$` , s .contextIsEmptyStep )
147- ctx .Step (`^the context should have (\d+) keys?$` , s .contextShouldHaveKeysStep )
148- ctx .Step (`^the context value for "([^"]*)" should be "([^"]*)" of type "([^"]*)"$` , s .contextValueShouldBeStep )
149- }
189+ // Step definition wrappers
150190
151191// contextContainsKeyStep is a step definition wrapper
152- func (s * TestState ) contextContainsKeyStep (key string ) error {
192+ func (s * TestState ) contextContainsKeyStep (ctx context. Context , key string ) error {
153193 return s .contextContainsKey (key )
154194}
155195
156196// contextIsEmptyStep is a step definition wrapper
157- func (s * TestState ) contextIsEmptyStep () error {
197+ func (s * TestState ) contextIsEmptyStep (ctx context. Context ) error {
158198 return s .contextIsEmpty ()
159199}
160200
161201// contextShouldHaveKeysStep checks the number of keys in context
162- func (s * TestState ) contextShouldHaveKeysStep (expectedCount int ) error {
202+ func (s * TestState ) contextShouldHaveKeysStep (ctx context. Context , expectedCount int ) error {
163203 actualCount := s .contextSize ()
164204 if actualCount != expectedCount {
165205 return fmt .Errorf ("expected context to have %d keys, but it has %d" , expectedCount , actualCount )
@@ -168,7 +208,7 @@ func (s *TestState) contextShouldHaveKeysStep(expectedCount int) error {
168208}
169209
170210// contextValueShouldBeStep checks a specific context value
171- func (s * TestState ) contextValueShouldBeStep (key , expectedValue , valueType string ) error {
211+ func (s * TestState ) contextValueShouldBeStep (ctx context. Context , key , expectedValue , valueType string ) error {
172212 convertedExpected , err := convertValueForSteps (expectedValue , valueType )
173213 if err != nil {
174214 return fmt .Errorf ("failed to convert expected value: %w" , err )
@@ -177,15 +217,15 @@ func (s *TestState) contextValueShouldBeStep(key, expectedValue, valueType strin
177217 return s .contextValueEquals (key , convertedExpected )
178218}
179219
180- // Missing step definition implementations - added as stubs that throw errors
220+ // Missing step definition implementations
181221
182222// addNestedContextProperty adds a nested property to evaluation context
183- func (s * TestState ) addNestedContextProperty (outerKey , innerKey , value string ) error {
184- return s .addContextValue (outerKey , "Object" , fmt .Sprintf ("{\" %s\" : \" %s\" }" , innerKey , value ))
223+ func (s * TestState ) addNestedContextProperty (ctx context. Context , outerKey , innerKey , value string ) error {
224+ return s .addContextValue (ctx , outerKey , "Object" , fmt .Sprintf ("{\" %s\" : \" %s\" }" , innerKey , value ))
185225}
186226
187227// addTargetingKeyToContext adds a targeting key to evaluation context
188- func (s * TestState ) addTargetingKeyToContext (value string ) error {
228+ func (s * TestState ) addTargetingKeyToContext (ctx context. Context , value string ) error {
189229 s .TargetingKey = value
190230 return nil
191231}
0 commit comments