-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect.go
56 lines (44 loc) · 1.13 KB
/
expect.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
package surveyexpect
import (
"time"
"github.com/AlecAivazis/survey/v2/core"
"github.com/stretchr/testify/assert"
)
// TestingT is an interface wrapper around *testing.T.
type TestingT interface {
Errorf(format string, args ...interface{})
FailNow()
Cleanup(f func())
Log(args ...interface{})
Logf(format string, args ...interface{})
}
// ExpectOption is option for the survey.
type ExpectOption func(s *Survey)
// Expector exp survey.
type Expector func(t TestingT) *Survey
// New creates a new expected survey.
func New(t TestingT, options ...ExpectOption) *Survey {
s := &Survey{
test: t,
timeout: 3 * time.Second,
}
for _, o := range options {
o(s)
}
return s
}
// Expect creates an expected survey with expectations and assures that ExpectationsWereMet() is called.
func Expect(options ...ExpectOption) Expector {
return func(t TestingT) *Survey {
// Setup the survey.
s := New(t, options...)
t.Cleanup(func() {
assert.NoError(t, s.ExpectationsWereMet())
})
return s
}
}
func init() { //nolint: gochecknoinits
// Disable color output for all prompts to simplify testing.
core.DisableColor = true
}