forked from tcnksm/go-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_test.go
62 lines (52 loc) · 1.21 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
package input
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
)
func TestValidateFunc_implement(t *testing.T) {
var _ ValidateFunc = defaultValidateFunc
}
func ExampleValidateFunc() {
ui := &UI{
// In real world, Reader is os.Stdin and input comes
// from user actual input
Reader: bytes.NewBufferString("Y\n"),
Writer: ioutil.Discard,
}
query := "Do you love golang [Y/n]"
ans, _ := ui.Ask(query, &Options{
// Define validateFunc to validate user input is
// 'Y' or 'n'. If not returns error.
ValidateFunc: func(s string) error {
if s != "Y" && s != "n" {
return fmt.Errorf("input must be Y or n")
}
return nil
},
})
fmt.Println(ans)
// Output: Y
}
func ExampleValidateFunc_windows() {
ui := &UI{
// In real world, Reader is os.Stdin and input comes
// from user actual input
Reader: bytes.NewBufferString("Y\r\n"),
Writer: ioutil.Discard,
}
query := "Do you love golang [Y/n]"
ans, _ := ui.Ask(query, &Options{
// Define validateFunc to validate user input is
// 'Y' or 'n'. If not returns error.
ValidateFunc: func(s string) error {
if s != "Y" && s != "n" {
return fmt.Errorf("input must be Y or n")
}
return nil
},
})
fmt.Println(ans)
// Output: Y
}