67 lines
2.3 KiB
Go
67 lines
2.3 KiB
Go
package editor
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.gophernest.net/azpect/TextEditor/internal/action"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/x/exp/teatest"
|
|
)
|
|
|
|
// sendKeys sends a sequence of keys to the test model
|
|
func sendKeys(tm *teatest.TestModel, keys ...string) {
|
|
for _, key := range keys {
|
|
switch key {
|
|
case "esc":
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyEscape})
|
|
case "enter":
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
case "backspace":
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyBackspace})
|
|
case "ctrl+c":
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlC})
|
|
case "ctrl+d":
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlD})
|
|
case "ctrl+u":
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlU})
|
|
case "ctrl+v":
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlV})
|
|
case "ctrl+w":
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlW})
|
|
default:
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(key)})
|
|
}
|
|
}
|
|
}
|
|
|
|
// newTestModel creates a test model with default content
|
|
func newTestModel(t *testing.T) *teatest.TestModel {
|
|
lines := []string{"line 1", "line 2", "line 3", "line 4", "line 5", "line 6"}
|
|
return teatest.NewTestModel(t, NewModel(lines, action.Position{Col: 0, Line: 0}), teatest.WithInitialTermSize(80, 24))
|
|
}
|
|
|
|
func newTestModelWithLines(t *testing.T, lines []string) *teatest.TestModel {
|
|
return teatest.NewTestModel(t, NewModel(lines, action.Position{Col: 0, Line: 0}), teatest.WithInitialTermSize(80, 24))
|
|
}
|
|
|
|
func newTestModelWithCursorPos(t *testing.T, pos action.Position) *teatest.TestModel {
|
|
lines := []string{"line 1", "line 2", "line 3", "line 4", "line 5", "line 6"}
|
|
return teatest.NewTestModel(t, NewModel(lines, pos), teatest.WithInitialTermSize(80, 24))
|
|
}
|
|
|
|
func newTestModelWithLinesAndCursorPos(t *testing.T, lines []string, pos action.Position) *teatest.TestModel {
|
|
return teatest.NewTestModel(t, NewModel(lines, pos), teatest.WithInitialTermSize(80, 24))
|
|
}
|
|
|
|
func newTestModelWithTermSize(t *testing.T, lines []string, pos action.Position, width, height int) *teatest.TestModel {
|
|
return teatest.NewTestModel(t, NewModel(lines, pos), teatest.WithInitialTermSize(width, height))
|
|
}
|
|
|
|
// getFinalModel extracts the final model state (sends ctrl+c to quit first)
|
|
func getFinalModel(t *testing.T, tm *teatest.TestModel) Model {
|
|
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlC})
|
|
fm := tm.FinalModel(t, teatest.WithFinalTimeout(time.Second))
|
|
return fm.(Model)
|
|
}
|