1package ui
2
3import (
4 "testing"
5
6 renderer "github.com/bnema/vev-vt"
7 "github.com/bnema/vev/internal/domain"
8 "github.com/stretchr/testify/require"
9)
10
11func TestTextInputEditsValue(t *testing.T) {
12 var input TextInput
13
14 input.Insert('w')
15 input.Insert('ø')
16 input.Insert('k')
17 require.Equal(t, "wøk", input.Value())
18
19 input.Backspace()
20 require.Equal(t, "wø", input.Value())
21
22 input.SetValue("ship")
23 require.Equal(t, "ship", input.Value())
24}
25
26func TestDrawInputLineDrawsPrefixAndValueWithoutCaret(t *testing.T) {
27 frame := renderer.NewFrame(6, 1)
28 style := renderer.DefaultStyle()
29
30 DrawInputLine(frame, 0, "> ", "abc", style)
31
32 require.Equal(t, '>', frame.At(0, 0).Rune)
33 require.Equal(t, ' ', frame.At(1, 0).Rune)
34 require.Equal(t, 'a', frame.At(2, 0).Rune)
35 require.Equal(t, 'b', frame.At(3, 0).Rune)
36 require.Equal(t, 'c', frame.At(4, 0).Rune)
37 require.False(t, frame.At(5, 0).Style.Inverse, "the real terminal cursor is the caret")
38}
39
40func TestInputCaret(t *testing.T) {
41 for _, tt := range []struct {
42 name string
43 width, rows int
44 prefix, value string
45 wantCol int
46 wantOK bool
47 }{
48 {name: "after value", width: 6, rows: 1, prefix: "> ", value: "abc", wantCol: 5, wantOK: true},
49 {name: "empty value", width: 6, rows: 1, prefix: "/", wantCol: 1, wantOK: true},
50 {name: "wide runes count two columns", width: 8, rows: 1, prefix: "> ", value: "日本", wantCol: 6, wantOK: true},
51 {name: "value fills the line", width: 5, rows: 1, prefix: "> ", value: "abc", wantCol: 5, wantOK: false},
52 {name: "value clipped", width: 4, rows: 1, prefix: "> ", value: "abcdef", wantCol: 8, wantOK: false},
53 {name: "no row", width: 6, rows: 0, prefix: "> ", value: "a", wantCol: 3, wantOK: false},
54 } {
55 t.Run(tt.name, func(t *testing.T) {
56 col, ok := InputCaret(domain.Size{Cols: tt.width, Rows: tt.rows}, tt.prefix, tt.value)
57 require.Equal(t, tt.wantCol, col)
58 require.Equal(t, tt.wantOK, ok)
59 })
60 }
61}
62
63func TestDrawInputLineIgnoresInvalidRow(t *testing.T) {
64 frame := renderer.NewFrame(4, 1)
65
66 DrawInputLine(frame, 2, "> ", "abc", renderer.DefaultStyle())
67
68 require.Equal(t, ' ', frame.At(0, 0).Rune)
69}