forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_test.go
85 lines (82 loc) · 2.36 KB
/
form_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
package ui
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestForm(t *testing.T) {
t.Parallel()
t.Run("Blank", func(t *testing.T) {
t.Parallel()
data, err := Form{}.Run(strings.NewReader(""), bytes.NewBuffer(nil))
assert.NoError(t, err)
assert.Equal(t, map[string]string{}, data)
})
t.Run("Banner", func(t *testing.T) {
t.Parallel()
out := bytes.NewBuffer(nil)
data, err := Form{Banner: "Hi!"}.Run(strings.NewReader(""), out)
assert.NoError(t, err)
assert.Equal(t, map[string]string{}, data)
assert.Equal(t, "Hi!\n\n", out.String())
})
t.Run("Field", func(t *testing.T) {
t.Parallel()
f := Form{
Fields: []Field{
StringField{Key: "key", Label: "label"},
},
}
in := "Value\n"
out := bytes.NewBuffer(nil)
data, err := f.Run(strings.NewReader(in), out)
assert.NoError(t, err)
assert.Equal(t, map[string]string{"key": "Value"}, data)
assert.Equal(t, " label: ", out.String())
})
t.Run("Fields", func(t *testing.T) {
t.Parallel()
f := Form{
Fields: []Field{
StringField{Key: "a", Label: "label a"},
StringField{Key: "b", Label: "label b"},
},
}
in := "1\n2\n"
out := bytes.NewBuffer(nil)
data, err := f.Run(strings.NewReader(in), out)
assert.NoError(t, err)
assert.Equal(t, map[string]string{"a": "1", "b": "2"}, data)
assert.Equal(t, " label a: label b: ", out.String())
})
t.Run("Defaults", func(t *testing.T) {
t.Parallel()
f := Form{
Fields: []Field{
StringField{Key: "a", Label: "label a", Default: "default a"},
StringField{Key: "b", Label: "label b", Default: "default b"},
},
}
in := "\n2\n"
out := bytes.NewBuffer(nil)
data, err := f.Run(strings.NewReader(in), out)
assert.NoError(t, err)
assert.Equal(t, map[string]string{"a": "default a", "b": "2"}, data)
assert.Equal(t, " label a [default a]: label b [default b]: ", out.String())
})
t.Run("Errors", func(t *testing.T) {
t.Parallel()
f := Form{
Fields: []Field{
StringField{Key: "key", Label: "label", Min: 6, Max: 10},
},
}
in := "short\ntoo damn long\nperfect\n"
out := bytes.NewBuffer(nil)
data, err := f.Run(strings.NewReader(in), out)
assert.NoError(t, err)
assert.Equal(t, map[string]string{"key": "perfect"}, data)
assert.Equal(t, " label: - invalid input, min length is 6\n label: - invalid input, max length is 10\n label: ", out.String())
})
}