File: ui_mock_test.go

package info (click to toggle)
golang-github-mitchellh-cli 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 276 kB
  • sloc: makefile: 13
file content (44 lines) | stat: -rw-r--r-- 748 bytes parent folder | download
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
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,
			}

			go in_w.Write([]byte(tc.input))

			result, err := ui.Ask(tc.query)
			if err != nil {
				t.Fatalf("err: %s", err)
			}

			if result != tc.expectedResult {
				t.Fatalf("bad: %#v", result)
			}
		})
	}
}