@@ -2,8 +2,12 @@ package vars_test
22
33import (
44 "context"
5+ "errors"
56 "fmt"
7+ "io"
8+ "time"
69
10+ "github.com/cucumber/godog"
711 "github.com/godogx/vars"
812)
913
@@ -57,3 +61,111 @@ func ExampleReplace() {
5761 // Output:
5862 // {"foo":321,"bar":123,"prefixed_foo":"ooo::321"}
5963}
64+
65+ func ExampleSteps_AddFactory () {
66+ vs := & vars.Steps {}
67+
68+ vs .AddFactory ("now" , func (ctx context.Context , args ... interface {}) (context.Context , interface {}, error ) {
69+ // "Now" is mocked with a constant value to reproducibility.
70+ return ctx , time .Date (2023 , 5 , 22 , 19 , 38 , 0 , 0 , time .UTC ), nil
71+ })
72+
73+ vs .AddFactory ("addDuration" , func (ctx context.Context , args ... interface {}) (context.Context , interface {}, error ) {
74+ if len (args ) != 2 {
75+ return ctx , nil , errors .New ("addDuration expects 2 arguments: base time, duration" )
76+ }
77+ var (
78+ base time.Time
79+ dur time.Duration
80+ )
81+
82+ switch v := args [0 ].(type ) {
83+ case time.Time :
84+ base = v
85+ case string :
86+ t , err := time .Parse (time .RFC3339Nano , v )
87+ if err != nil {
88+ return ctx , nil , fmt .Errorf ("parsing base time: %w" , err )
89+ }
90+ base = t
91+ default :
92+ return ctx , nil , fmt .Errorf ("unexpected type %T for base time, string or time.Time expected" , v )
93+ }
94+
95+ switch v := args [1 ].(type ) {
96+ case time.Duration :
97+ dur = v
98+ case string :
99+ d , err := time .ParseDuration (v )
100+ if err != nil {
101+ return ctx , nil , fmt .Errorf ("parsing duration: %w" , err )
102+ }
103+ dur = d
104+ default :
105+ return ctx , nil , fmt .Errorf ("unexpected type %T for duration, string or time.Duration expected" , v )
106+ }
107+
108+ return ctx , base .Add (dur ), nil
109+ })
110+
111+ vs .AddFactory ("newUserID" , func (ctx context.Context , args ... interface {}) (context.Context , interface {}, error ) {
112+ if len (args ) != 2 {
113+ return ctx , nil , errors .New ("newUserID expects 2 arguments: name, registeredAt" )
114+ }
115+ var (
116+ name string
117+ registeredAt time.Time
118+ )
119+
120+ switch v := args [0 ].(type ) {
121+ case string :
122+ name = v
123+ default :
124+ return ctx , nil , fmt .Errorf ("unexpected type %T for name, string expected" , v )
125+ }
126+
127+ switch v := args [1 ].(type ) {
128+ case time.Time :
129+ registeredAt = v
130+ case string :
131+ t , err := time .Parse (time .RFC3339Nano , v )
132+ if err != nil {
133+ return ctx , nil , fmt .Errorf ("parsing registeredAt: %w" , err )
134+ }
135+ registeredAt = t
136+ default :
137+ return ctx , nil , fmt .Errorf ("unexpected type %T for registeredAt, string or time.Time expected" , v )
138+ }
139+
140+ fmt .Println ("creating user" , name , registeredAt )
141+
142+ // Return relevant value, for example user id.
143+ return ctx , 123 , nil
144+ })
145+
146+ s := godog.TestSuite {}
147+
148+ s .ScenarioInitializer = func (sc * godog.ScenarioContext ) {
149+ vs .Register (sc )
150+ }
151+
152+ s .Options = & godog.Options {
153+ Format : "pretty" ,
154+ Output : io .Discard ,
155+ FeatureContents : []godog.Feature {
156+ {
157+ Name : "example" ,
158+ Contents : []byte (`
159+ Feature: example
160+ Scenario: using var factory
161+ Given variable $myUserID is set to newUserID("John Doe", addDuration(now(), "-10h"))
162+ ` ),
163+ },
164+ },
165+ }
166+
167+ s .Run ()
168+
169+ // Output:
170+ // creating user John Doe 2023-05-22 09:38:00 +0000 UTC
171+ }
0 commit comments