forked from mitchellh/cli
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathui_mock_test.go
56 lines (46 loc) · 971 Bytes
/
ui_mock_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cli
import (
"io"
"testing"
)
func TestMockUi_implements(t *testing.T) {
var _ Ui = new(MockUi)
}
func TestMockUi_Ask(t *testing.T) {
tests := []struct {
name string
query, input string
expectedResult string
}{
{"EmptyString", "Middle Name?", "\n", ""},
{"NonEmptyString", "Name?", "foo bar\nbaz\n", "foo bar"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
in_r, in_w := io.Pipe()
defer in_r.Close()
defer in_w.Close()
ui := &MockUi{
InputReader: in_r,
}
errors := make(chan error, 1)
go func() {
_, err := in_w.Write([]byte(tc.input))
errors <- err
}()
result, err := ui.Ask(tc.query)
if err != nil {
t.Fatalf("err: %s", err)
}
err = <-errors
if err != nil {
t.Fatalf("err: %v", err)
}
if result != tc.expectedResult {
t.Fatalf("bad: %#v", result)
}
})
}
}