forked from AlecAivazis/survey
/
test.go
51 lines (45 loc) · 1.17 KB
/
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
package TestUtil
import (
"fmt"
"reflect"
"gopkg.in/AlecAivazis/survey.v1"
)
type TestTableEntry struct {
Name string
Prompt survey.Prompt
Value interface{}
Validate func(interface{}) error
}
func formatAnswer(ans interface{}) {
// show the answer to the user
fmt.Printf("Answered %v.\n", reflect.ValueOf(ans).Elem())
fmt.Println("---------------------")
}
func RunTable(table []TestTableEntry) {
// go over every entry in the table
for _, entry := range table {
// tell the user what we are going to ask them
fmt.Println(entry.Name)
// perform the ask
err := survey.AskOne(entry.Prompt, entry.Value, entry.Validate)
if err != nil {
fmt.Printf("AskOne on %v's prompt failed: %v.", entry.Name, err.Error())
break
}
// show the answer to the user
formatAnswer(entry.Value)
}
}
func RunErrorTable(table []TestTableEntry) {
// go over every entry in the table
for _, entry := range table {
// tell the user what we are going to ask them
fmt.Println(entry.Name)
// perform the ask
err := survey.AskOne(entry.Prompt, entry.Value, entry.Validate)
if err == nil {
fmt.Printf("AskOne on %v's prompt didn't fail.", entry.Name)
break
}
}
}