1package survey
2
3import (
4	"bytes"
5	"fmt"
6	"io"
7	"os"
8	"testing"
9
10	"github.com/AlecAivazis/survey/v2/core"
11	"github.com/AlecAivazis/survey/v2/terminal"
12	expect "github.com/Netflix/go-expect"
13	"github.com/stretchr/testify/assert"
14)
15
16func init() {
17	// disable color output for all prompts to simplify testing
18	core.DisableColor = true
19}
20
21func TestConfirmRender(t *testing.T) {
22
23	tests := []struct {
24		title    string
25		prompt   Confirm
26		data     ConfirmTemplateData
27		expected string
28	}{
29		{
30			"Test Confirm question output with default true",
31			Confirm{Message: "Is pizza your favorite food?", Default: true},
32			ConfirmTemplateData{},
33			fmt.Sprintf("%s Is pizza your favorite food? (Y/n) ", defaultIcons().Question.Text),
34		},
35		{
36			"Test Confirm question output with default false",
37			Confirm{Message: "Is pizza your favorite food?", Default: false},
38			ConfirmTemplateData{},
39			fmt.Sprintf("%s Is pizza your favorite food? (y/N) ", defaultIcons().Question.Text),
40		},
41		{
42			"Test Confirm answer output",
43			Confirm{Message: "Is pizza your favorite food?"},
44			ConfirmTemplateData{Answer: "Yes"},
45			fmt.Sprintf("%s Is pizza your favorite food? Yes\n", defaultIcons().Question.Text),
46		},
47		{
48			"Test Confirm with help but help message is hidden",
49			Confirm{Message: "Is pizza your favorite food?", Help: "This is helpful"},
50			ConfirmTemplateData{},
51			fmt.Sprintf("%s Is pizza your favorite food? [%s for help] (y/N) ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
52		},
53		{
54			"Test Confirm help output with help message shown",
55			Confirm{Message: "Is pizza your favorite food?", Help: "This is helpful"},
56			ConfirmTemplateData{ShowHelp: true},
57			fmt.Sprintf("%s This is helpful\n%s Is pizza your favorite food? (y/N) ", defaultIcons().Help.Text, defaultIcons().Question.Text),
58		},
59	}
60
61	for _, test := range tests {
62		r, w, err := os.Pipe()
63		assert.Nil(t, err, test.title)
64
65		test.prompt.WithStdio(terminal.Stdio{Out: w})
66		test.data.Confirm = test.prompt
67
68		// set the runtime config
69		test.data.Config = defaultPromptConfig()
70
71		err = test.prompt.Render(
72			ConfirmQuestionTemplate,
73			test.data,
74		)
75		assert.Nil(t, err, test.title)
76
77		w.Close()
78		var buf bytes.Buffer
79		io.Copy(&buf, r)
80
81		assert.Contains(t, buf.String(), test.expected, test.title)
82	}
83}
84
85func TestConfirmPrompt(t *testing.T) {
86	tests := []PromptTest{
87		{
88			"Test Confirm prompt interaction",
89			&Confirm{
90				Message: "Is pizza your favorite food?",
91			},
92			func(c *expect.Console) {
93				c.ExpectString("Is pizza your favorite food? (y/N)")
94				c.SendLine("n")
95				c.ExpectEOF()
96			},
97			false,
98		},
99		{
100			"Test Confirm prompt interaction with default",
101			&Confirm{
102				Message: "Is pizza your favorite food?",
103				Default: true,
104			},
105			func(c *expect.Console) {
106				c.ExpectString("Is pizza your favorite food? (Y/n)")
107				c.SendLine("")
108				c.ExpectEOF()
109			},
110			true,
111		},
112		{
113			"Test Confirm prompt interaction overriding default",
114			&Confirm{
115				Message: "Is pizza your favorite food?",
116				Default: true,
117			},
118			func(c *expect.Console) {
119				c.ExpectString("Is pizza your favorite food? (Y/n)")
120				c.SendLine("n")
121				c.ExpectEOF()
122			},
123			false,
124		},
125		{
126			"Test Confirm prompt interaction and prompt for help",
127			&Confirm{
128				Message: "Is pizza your favorite food?",
129				Help:    "It probably is",
130			},
131			func(c *expect.Console) {
132				c.ExpectString(
133					fmt.Sprintf(
134						"Is pizza your favorite food? [%s for help] (y/N)",
135						string(defaultPromptConfig().HelpInput),
136					),
137				)
138				c.SendLine("?")
139				c.ExpectString("It probably is")
140				c.SendLine("Y")
141				c.ExpectEOF()
142			},
143			true,
144		},
145	}
146
147	for _, test := range tests {
148		t.Run(test.name, func(t *testing.T) {
149			RunPromptTest(t, test)
150		})
151	}
152}
153