File: input_test.go

package info (click to toggle)
golang-github-cqroot-prompt 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: makefile: 17; sh: 12
file content (121 lines) | stat: -rw-r--r-- 3,046 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package input_test

import (
	"bytes"
	"testing"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/cqroot/prompt/constants"
	"github.com/cqroot/prompt/input"
	"github.com/stretchr/testify/require"
)

func TestChoose(t *testing.T) {
	defaultVal := "default value"
	val := `abcdefghijklmnopqrstuvwxyz1.2.3.4.5.6.7.8.9.0-=~!@#$%^&*()_+[]\{}|;':",./<>?`

	for _, testcase := range []struct {
		model input.Model
		keys  []byte
		data  string
	}{
		{
			model: *input.New(defaultVal),
			keys:  []byte("\r\n"),
			data:  defaultVal,
		},
		{
			model: *input.New(defaultVal),
			keys:  []byte(val + "\r\n"),
			data:  val,
		},
		{
			model: *input.New(defaultVal, input.WithInputMode(input.InputInteger)),
			keys:  []byte(val + "\r\n"),
			data:  "1234567890",
		},
		{
			model: *input.New(defaultVal, input.WithInputMode(input.InputNumber)),
			keys:  []byte(val + "\r\n"),
			data:  "1.234567890",
		},
	} {
		var in bytes.Buffer
		var out bytes.Buffer

		in.Write(testcase.keys)
		tm, err := tea.NewProgram(testcase.model, tea.WithInput(&in), tea.WithOutput(&out)).Run()
		require.Nil(t, err)

		m, ok := tm.(input.Model)
		require.Equal(t, true, ok)

		require.Equal(t, testcase.data, m.Data())
		require.Equal(t, testcase.data, m.DataString())
		require.Equal(t, true, m.Quitting())
	}
}

func TestErrors(t *testing.T) {
	var in bytes.Buffer
	var out bytes.Buffer

	in.Write([]byte{byte(tea.KeyCtrlC)})
	tm, err := tea.NewProgram(*input.New(""), tea.WithInput(&in), tea.WithOutput(&out)).Run()
	require.Nil(t, err)

	m, ok := tm.(input.Model)
	require.Equal(t, true, ok)

	require.Equal(t, constants.ErrUserQuit, m.Error())
}

func TestThemes(t *testing.T) {
	defaultVal := "default value"

	for _, testcase := range []struct {
		model input.Model
		view  string
	}{
		{
			model: *input.New(defaultVal),
			view:  "default value",
		},
		{
			model: *input.New(defaultVal, input.WithHelp(true)),
			view:  "default value\n\nenter confirm • esc quit",
		},
		{
			model: func() input.Model {
				var tm tea.Model
				tm = input.New(defaultVal)
				tm, _ = tm.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("test")})
				tm, _ = tm.Update(tea.KeyMsg{Type: tea.KeyEnter})
				return tm.(input.Model)
			}(),
			view: "test                                     ",
		},
		{
			model: func() input.Model {
				var tm tea.Model
				tm = input.New(defaultVal, input.WithEchoMode(input.EchoNone))
				tm, _ = tm.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("test")})
				tm, _ = tm.Update(tea.KeyMsg{Type: tea.KeyEnter})
				return tm.(input.Model)
			}(),
			view: "                                     ",
		},
		{
			model: func() input.Model {
				var tm tea.Model
				tm = input.New(defaultVal, input.WithEchoMode(input.EchoPassword))
				tm, _ = tm.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("test")})
				tm, _ = tm.Update(tea.KeyMsg{Type: tea.KeyEnter})
				return tm.(input.Model)
			}(),
			view: "****                                     ",
		},
	} {
		require.Equal(t, testcase.view, testcase.model.View())
	}
}