-
Notifications
You must be signed in to change notification settings - Fork 15
/
input_test.go
146 lines (134 loc) · 2.92 KB
/
input_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
package clif
import (
"bytes"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"regexp"
"sync"
"testing"
)
func TestDefaultInput(t *testing.T) {
Convey("Input data", t, func() {
bufIn := bytes.NewBuffer(nil)
bufOut := bytes.NewBuffer(nil)
out := NewMonochromeOutput(bufOut)
in := NewDefaultInput(bufIn, out)
Convey("Ask returns on any non-empty input", func() {
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
bufIn.WriteString("Foo\n")
}()
res := ""
go func() {
defer wg.Done()
res = in.Ask("Foo? ", nil)
}()
wg.Wait()
So(res, ShouldEqual, "Foo")
So(bufOut.String(), ShouldEqual, "Foo? ")
})
Convey("Ask with check tries until check ok", func() {
var wg sync.WaitGroup
var wgFirst sync.WaitGroup
wgFirst.Add(1)
wg.Add(3)
go func() {
defer wg.Done()
defer wgFirst.Done()
bufIn.WriteString("Foo\nBaz\n")
}()
go func() {
defer wg.Done()
wgFirst.Wait()
bufIn.WriteString("Bar\n")
}()
res := ""
go func() {
defer wg.Done()
res = in.Ask("Foo? ", func(c string) error {
if c == "Bar" {
return nil
} else {
return fmt.Errorf("Not Bar!")
}
})
}()
wg.Wait()
So(res, ShouldEqual, "Bar")
So(bufOut.String(), ShouldEqual, `Foo? Not Bar!
Foo? Not Bar!
Foo? `)
})
Convey("Ask with regular expression tries until matches", func() {
var wg sync.WaitGroup
var wgFirst sync.WaitGroup
wgFirst.Add(1)
wg.Add(3)
rx := regexp.MustCompile(`Bar`)
go func() {
defer wg.Done()
defer wgFirst.Done()
bufIn.WriteString("Foo\nBaz\n")
}()
go func() {
defer wg.Done()
wgFirst.Wait()
bufIn.WriteString("Bar\n")
}()
res := ""
go func() {
defer wg.Done()
res = in.AskRegex("Foo? ", rx)
}()
wg.Wait()
So(res, ShouldEqual, "Bar")
So(bufOut.String(), ShouldEqual, `Foo? Input does not match criteria
Foo? Input does not match criteria
Foo? `)
})
Convey("Choose presents options and returns on valid choice", func() {
var wg sync.WaitGroup
var wgFirst sync.WaitGroup
wgFirst.Add(1)
wg.Add(3)
go func() {
defer wg.Done()
defer wgFirst.Done()
bufIn.WriteString("Foo\nBaz\n")
}()
go func() {
defer wg.Done()
wgFirst.Wait()
bufIn.WriteString("the bar\n")
}()
res := ""
go func() {
defer wg.Done()
res = in.Choose("Choose or loose!", map[string]string{
"foo": "Foo!!!",
"the bar": "One bar please",
"42": "Take that",
})
}()
wg.Wait()
So(res, ShouldEqual, "the bar")
So(bufOut.String(), ShouldEqual, `Choose or loose!
42) Take that
foo) Foo!!!
the bar) One bar please
Choose: Choose one of: 42, foo, the bar
Choose or loose!
42) Take that
foo) Foo!!!
the bar) One bar please
Choose: Choose one of: 42, foo, the bar
Choose or loose!
42) Take that
foo) Foo!!!
the bar) One bar please
Choose: `)
})
})
}