github.com/aretext/aretext@v1.3.0/display/textfield_test.go (about) 1 package display 2 3 import ( 4 "testing" 5 6 "github.com/gdamore/tcell/v2" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/aretext/aretext/state" 11 ) 12 13 func buildTextFieldState(t *testing.T, promptText, inputText string) *state.TextFieldState { 14 s, err := newEditorStateWithPath("test.txt") 15 require.NoError(t, err) 16 17 emptyAction := func(_ *state.EditorState, _ string) error { return nil } 18 state.ShowTextField(s, promptText, emptyAction, nil) 19 for _, r := range inputText { 20 state.AppendRuneToTextField(s, r) 21 } 22 23 return s.TextField() 24 25 } 26 27 func TestDrawTextField(t *testing.T) { 28 testCases := []struct { 29 name string 30 promptText string 31 inputText string 32 screenWidth int 33 screenHeight int 34 expectContents [][]rune 35 expectCursorVisible bool 36 expectCursorCol int 37 expectCursorRow int 38 }{ 39 { 40 name: "prompt with no input", 41 screenWidth: 15, 42 screenHeight: 4, 43 promptText: "Test prompt:", 44 inputText: "", 45 expectContents: [][]rune{ 46 {'T', 'e', 's', 't', ' ', 'p', 'r', 'o', 'm', 'p', 't', ':', ' ', ' ', ' '}, 47 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 48 {'─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─'}, 49 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 50 }, 51 expectCursorVisible: true, 52 expectCursorCol: 0, 53 expectCursorRow: 1, 54 }, 55 { 56 name: "prompt with input", 57 screenWidth: 15, 58 screenHeight: 4, 59 promptText: "Test prompt:", 60 inputText: "foo.txt", 61 expectContents: [][]rune{ 62 {'T', 'e', 's', 't', ' ', 'p', 'r', 'o', 'm', 'p', 't', ':', ' ', ' ', ' '}, 63 {'f', 'o', 'o', '.', 't', 'x', 't', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 64 {'─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─', '─'}, 65 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 66 }, 67 expectCursorVisible: true, 68 expectCursorCol: 7, 69 expectCursorRow: 1, 70 }, 71 { 72 name: "prompt with input, two rows no border", 73 screenWidth: 15, 74 screenHeight: 2, 75 promptText: "Test prompt:", 76 inputText: "foo.txt", 77 expectContents: [][]rune{ 78 {'T', 'e', 's', 't', ' ', 'p', 'r', 'o', 'm', 'p', 't', ':', ' ', ' ', ' '}, 79 {'f', 'o', 'o', '.', 't', 'x', 't', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 80 }, 81 expectCursorVisible: true, 82 expectCursorCol: 7, 83 expectCursorRow: 1, 84 }, 85 { 86 name: "prompt with input, single row no border or input", 87 screenWidth: 15, 88 screenHeight: 1, 89 promptText: "Test prompt:", 90 inputText: "foo.txt", 91 expectContents: [][]rune{ 92 {'T', 'e', 's', 't', ' ', 'p', 'r', 'o', 'm', 'p', 't', ':', ' ', ' ', ' '}, 93 }, 94 expectCursorVisible: false, 95 }, 96 } 97 98 for _, tc := range testCases { 99 t.Run(tc.name, func(t *testing.T) { 100 withSimScreen(t, func(s tcell.SimulationScreen) { 101 s.SetSize(tc.screenWidth, tc.screenHeight) 102 palette := NewPalette() 103 textFieldState := buildTextFieldState(t, tc.promptText, tc.inputText) 104 DrawTextField(s, palette, textFieldState) 105 s.Sync() 106 assertCellContents(t, s, tc.expectContents) 107 cursorCol, cursorRow, cursorVisible := s.GetCursor() 108 assert.Equal(t, tc.expectCursorVisible, cursorVisible) 109 if tc.expectCursorVisible { 110 assert.Equal(t, tc.expectCursorCol, cursorCol) 111 assert.Equal(t, tc.expectCursorRow, cursorRow) 112 } 113 }) 114 }) 115 } 116 }