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