-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgodog.go
More file actions
127 lines (97 loc) · 2.74 KB
/
godog.go
File metadata and controls
127 lines (97 loc) · 2.74 KB
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
package clocksteps
import (
"context"
"strconv"
"time"
"github.com/cucumber/godog"
"go.nhat.io/timeparser"
)
// RegisterSteps registers clock to godog tests.
func (c *Clock) RegisterSteps(s *godog.ScenarioContext) {
s.After(func(ctx context.Context, _ *godog.Scenario, _ error) (context.Context, error) {
// Unfreeze the clock.
c.Unfreeze()
return ctx, nil //nolint: nilnil
})
s.Step(`(?:the )?clock is at "([^"]*)"`, c.set)
s.Step(`(?:the )?clock is set to "([^"]*)"`, c.set)
s.Step(`sets? (?:the )?clock to "([^"]*)"`, c.set)
s.Step(`now is "([^"]*)"`, c.set)
s.Step(`(?:the )?clock advances to "([^"]*)"`, c.next)
s.Step(`(?:the )?clock changes to "([^"]*)"`, c.next)
s.Step(`(?:the )?clock moves forward to "([^"]*)"`, c.next)
s.Step(`adds? ([^\s]*) to (?:the )?clock`, c.add)
s.Step(`adds? ([0-9]+) days? to (?:the )?clock`, c.addDay)
s.Step(`adds? ([0-9]+) months? to (?:the )?clock`, c.addMonth)
s.Step(`adds? ([0-9]+) years? to (?:the )?clock`, c.addYear)
s.Step(`adds? ([0-9]+) months?,? ([0-9]+) days? to (?:the )?clock`, c.addMonthDay)
s.Step(`adds? ([0-9]+) years?,? ([0-9]+) days? to (?:the )?clock`, c.addYearDay)
s.Step(`adds? ([0-9]+) years?,? ([0-9]+) months? to (?:the )?clock`, c.addYearMonth)
s.Step(`adds? ([0-9]+) years?,? ([0-9]+) months?,? ([0-9]+) days? to (?:the )?clock`, c.addDate)
s.Step(`\s*freeze (?:the )?clock`, c.freeze)
s.Step(`(?:(?:release)|(?:unset)|(?:reset)) (?:the )?clock$`, c.unfreeze)
}
func (c *Clock) set(t string) error {
ts, err := timeparser.Parse(t)
if err != nil {
return err
}
c.Set(ts)
return nil
}
func (c *Clock) next(t string) error {
ts, err := timeparser.Parse(t)
if err != nil {
return err
}
c.Next(ts)
return nil
}
func (c *Clock) add(s string) error {
d, err := time.ParseDuration(s)
if err != nil {
return err
}
return c.Add(d)
}
func (c *Clock) addDay(days string) error {
return c.addDate("0", "0", days)
}
func (c *Clock) addMonth(months string) error {
return c.addDate("0", months, "0")
}
func (c *Clock) addYear(years string) error {
return c.addDate(years, "0", "0")
}
func (c *Clock) addMonthDay(months, days string) error {
return c.addDate("0", months, days)
}
func (c *Clock) addYearDay(years, days string) error {
return c.addDate(years, "0", days)
}
func (c *Clock) addYearMonth(years, months string) error {
return c.addDate(years, months, "0")
}
func (c *Clock) addDate(years, months, days string) error {
y, err := strconv.Atoi(years)
if err != nil {
return err
}
m, err := strconv.Atoi(months)
if err != nil {
return err
}
d, err := strconv.Atoi(days)
if err != nil {
return err
}
return c.AddDate(y, m, d)
}
func (c *Clock) freeze() error {
c.Freeze()
return nil
}
func (c *Clock) unfreeze() error {
c.Freeze()
return nil
}