File: write_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 (126 lines) | stat: -rw-r--r-- 3,415 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
122
123
124
125
126
package write_test

import (
	"bytes"
	"strings"
	"testing"

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

func TestMultiChoose(t *testing.T) {
	defaultVal := "default value"
	val := "abcdefghij\r\nklmnopqrst\r\nuvwxyz1.2.\r\n3.4.5.6.7.\r\n8.9.0-=~!@\r\n#$%^&*()_+\r\n[]\\{}|;':\",./<>?"

	for _, testcase := range []struct {
		model      write.Model
		keys       []byte
		data       string
		dataString string
	}{
		{
			model:      *write.New(defaultVal),
			keys:       []byte{byte(tea.KeyCtrlD)},
			data:       defaultVal,
			dataString: defaultVal,
		},
		{
			model:      *write.New(defaultVal),
			keys:       append([]byte(val), byte(tea.KeyCtrlD)),
			data:       val,
			dataString: "...(82 bytes)",
		},
	} {
		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.(write.Model)
		require.Equal(t, true, ok)

		require.Equal(t, strings.ReplaceAll(testcase.data, "\r", ""), m.Data())
		require.Equal(t, testcase.dataString, 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(*write.New(""), tea.WithInput(&in), tea.WithOutput(&out)).Run()
	require.Nil(t, err)

	m, ok := tm.(write.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 write.Model
		view  string
	}{
		{
			model: *write.New(defaultVal),
			view: "\n┃ default value                      " +
				`
┃                                    `,
		},
		{
			model: *write.New(defaultVal, write.WithHelp(true)),
			view: "\n┃ default value                      " + `

ctrl+d confirm • esc quit`,
		},
		{
			model: *write.New(defaultVal, write.WithLineNumbers(true)),
			view: "\n┃  1 default value                      " + `
┃  ~                                    
┃  ~                                    
┃  ~                                    
┃  ~                                    
┃  ~                                    `,
		},
		{
			model: func() write.Model {
				var tm tea.Model
				tm = write.New(defaultVal, write.WithCharLimit(2))
				tm, _ = tm.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("test")})
				return tm.(write.Model)
			}(),
			view: "\n┃ te                                 " +
				`
┃                                    `,
		},
		{
			model: *write.New(defaultVal, write.WithWidth(3)),
			view:  "\n┃ .\n┃  \n┃  \n┃  \n┃  \n┃  ",
		},
	} {
		require.Equal(t, testcase.view, testcase.model.View())
	}
}