generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
176 lines (142 loc) · 4.08 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package vars_test
import (
"context"
"errors"
"fmt"
"io"
"time"
"github.com/cucumber/godog"
"github.com/godogx/vars"
)
func ExampleAssert() {
// Variables are passed via chained context.
ctx := context.Background()
expected := []byte(`{"foo":"$foo","bar":123}`)
received := []byte(`{"foo":321,"bar":123,"baz":true}`)
// No error, $foo is populated with 321, "baz" is ignored for true `ignoreAddedJSONFields` argument.
ctx, err := vars.Assert(ctx, expected, received, true)
if err != nil {
fmt.Println("assertion failed: " + err.Error())
}
expected = []byte(`{"foo":"$foo","bar":123,"prefixed_foo":"ooo::$foo"}`)
received = []byte(`{"foo":313,"bar":123,"baz":true,"prefixed_foo":"ooo::321"}`)
// Assertion fails.
_, err = vars.Assert(ctx, expected, received, false)
if err != nil {
fmt.Println("assertion failed: " + err.Error())
}
// Output:
// assertion failed: not equal:
// {
// "bar": 123,
// - "foo": 321,
// + "foo": 313,
// "prefixed_foo": "ooo::321"
// + "baz": true
// }
}
func ExampleReplace() {
// Variables are passed via chained context.
ctx := context.Background()
ctx = vars.ToContext(ctx, "$foo", 321)
expected := []byte(`{"foo":"$foo","bar":123, "prefixed_foo":"ooo::$foo"}`)
_, expected, err := vars.Replace(ctx, expected)
if err != nil {
fmt.Println("replace failed: " + err.Error())
}
fmt.Println(string(expected))
// Output:
// {"foo":321,"bar":123,"prefixed_foo":"ooo::321"}
}
func ExampleSteps_AddFactory() {
vs := &vars.Steps{}
vs.AddFactory("now", func(ctx context.Context, args ...interface{}) (context.Context, interface{}, error) {
// "Now" is mocked with a constant value to reproducibility.
return ctx, time.Date(2023, 5, 22, 19, 38, 0, 0, time.UTC), nil
})
vs.AddFactory("addDuration", func(ctx context.Context, args ...interface{}) (context.Context, interface{}, error) {
if len(args) != 2 {
return ctx, nil, errors.New("addDuration expects 2 arguments: base time, duration")
}
var (
base time.Time
dur time.Duration
)
switch v := args[0].(type) {
case time.Time:
base = v
case string:
t, err := time.Parse(time.RFC3339Nano, v)
if err != nil {
return ctx, nil, fmt.Errorf("parsing base time: %w", err)
}
base = t
default:
return ctx, nil, fmt.Errorf("unexpected type %T for base time, string or time.Time expected", v)
}
switch v := args[1].(type) {
case time.Duration:
dur = v
case string:
d, err := time.ParseDuration(v)
if err != nil {
return ctx, nil, fmt.Errorf("parsing duration: %w", err)
}
dur = d
default:
return ctx, nil, fmt.Errorf("unexpected type %T for duration, string or time.Duration expected", v)
}
return ctx, base.Add(dur), nil
})
vs.AddFactory("newUserID", func(ctx context.Context, args ...interface{}) (context.Context, interface{}, error) {
if len(args) != 2 {
return ctx, nil, errors.New("newUserID expects 2 arguments: name, registeredAt")
}
var (
name string
registeredAt time.Time
)
switch v := args[0].(type) {
case string:
name = v
default:
return ctx, nil, fmt.Errorf("unexpected type %T for name, string expected", v)
}
switch v := args[1].(type) {
case time.Time:
registeredAt = v
case string:
t, err := time.Parse(time.RFC3339Nano, v)
if err != nil {
return ctx, nil, fmt.Errorf("parsing registeredAt: %w", err)
}
registeredAt = t
default:
return ctx, nil, fmt.Errorf("unexpected type %T for registeredAt, string or time.Time expected", v)
}
fmt.Println("creating user", name, registeredAt)
// Return relevant value, for example user id.
return ctx, 123, nil
})
s := godog.TestSuite{}
s.ScenarioInitializer = func(sc *godog.ScenarioContext) {
vs.Register(sc)
}
s.Options = &godog.Options{
Format: "pretty",
Output: io.Discard,
FeatureContents: []godog.Feature{
{
Name: "example",
Contents: []byte(`
Feature: example
Scenario: using var factory
Given variable $myUserID is set to newUserID("John Doe", addDuration(now(), "-10h"))
`),
},
},
}
s.Run()
// Output:
// creating user John Doe 2023-05-22 09:38:00 +0000 UTC
}