129 lines
3.6 KiB
Go
129 lines
3.6 KiB
Go
package editor
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.gophernest.net/azpect/TextEditor/internal/core"
|
|
)
|
|
|
|
// TestHelperExamples demonstrates the different ways to use the test helpers
|
|
func TestHelperExamples(t *testing.T) {
|
|
t.Run("basic model with defaults", func(t *testing.T) {
|
|
// Uses default: 6 lines, cursor at 0,0, terminal 80x24
|
|
tm := newTestModel(t)
|
|
_ = getFinalModel(t, tm)
|
|
})
|
|
|
|
t.Run("custom lines only", func(t *testing.T) {
|
|
tm := newTestModel(t,
|
|
WithLines([]string{"hello", "world"}),
|
|
)
|
|
m := getFinalModel(t, tm)
|
|
buf := m.ActiveBuffer()
|
|
if buf.LineCount() != 2 {
|
|
t.Errorf("expected 2 lines, got %d", buf.LineCount())
|
|
}
|
|
})
|
|
|
|
t.Run("custom cursor position", func(t *testing.T) {
|
|
tm := newTestModel(t,
|
|
WithCursorPos(core.Position{Line: 2, Col: 3}),
|
|
)
|
|
m := getFinalModel(t, tm)
|
|
win := m.ActiveWindow()
|
|
if win.Cursor.Line != 2 || win.Cursor.Col != 3 {
|
|
t.Errorf("expected cursor at (2,3), got (%d,%d)", win.Cursor.Line, win.Cursor.Col)
|
|
}
|
|
})
|
|
|
|
t.Run("custom terminal size", func(t *testing.T) {
|
|
tm := newTestModel(t,
|
|
WithTermSize(120, 40),
|
|
)
|
|
m := getFinalModel(t, tm)
|
|
win := m.ActiveWindow()
|
|
if win.Width != 120 || win.Height != 40 {
|
|
t.Errorf("expected size 120x40, got %dx%d", win.Width, win.Height)
|
|
}
|
|
})
|
|
|
|
t.Run("with register content for paste testing", func(t *testing.T) {
|
|
tm := newTestModel(t,
|
|
WithLines([]string{"hello world"}),
|
|
WithRegister('"', core.CharwiseRegister, []string{"foo"}),
|
|
)
|
|
m := getFinalModel(t, tm)
|
|
|
|
reg, ok := m.GetRegister('"')
|
|
if !ok {
|
|
t.Fatal("expected register to be set")
|
|
}
|
|
if reg.Type != core.CharwiseRegister {
|
|
t.Errorf("expected charwise register, got %v", reg.Type)
|
|
}
|
|
if len(reg.Content) != 1 || reg.Content[0] != "foo" {
|
|
t.Errorf("expected content ['foo'], got %v", reg.Content)
|
|
}
|
|
})
|
|
|
|
t.Run("combine multiple options", func(t *testing.T) {
|
|
tm := newTestModel(t,
|
|
WithLines([]string{"line one", "line two", "line three"}),
|
|
WithCursorPos(core.Position{Line: 1, Col: 5}),
|
|
WithTermSize(100, 30),
|
|
WithRegister('"', core.LinewiseRegister, []string{"deleted line 1", "deleted line 2"}),
|
|
)
|
|
m := getFinalModel(t, tm)
|
|
|
|
// Verify all options were applied
|
|
win := m.ActiveWindow()
|
|
buf := m.ActiveBuffer()
|
|
|
|
if buf.LineCount() != 3 {
|
|
t.Errorf("expected 3 lines, got %d", buf.LineCount())
|
|
}
|
|
if win.Cursor.Line != 1 || win.Cursor.Col != 5 {
|
|
t.Errorf("expected cursor at (1,5), got (%d,%d)", win.Cursor.Line, win.Cursor.Col)
|
|
}
|
|
if win.Width != 100 || win.Height != 30 {
|
|
t.Errorf("expected size 100x30, got %dx%d", win.Width, win.Height)
|
|
}
|
|
|
|
reg, ok := m.GetRegister('"')
|
|
if !ok || reg.Type != core.LinewiseRegister {
|
|
t.Error("register not set correctly")
|
|
}
|
|
})
|
|
|
|
t.Run("backward compatible helpers still work", func(t *testing.T) {
|
|
// Old style helpers still work for existing tests
|
|
tm1 := newTestModelWithLines(t, []string{"a", "b"})
|
|
m1 := getFinalModel(t, tm1)
|
|
buf1 := m1.ActiveBuffer()
|
|
if buf1.LineCount() != 2 {
|
|
t.Error("newTestModelWithLines failed")
|
|
}
|
|
|
|
tm2 := newTestModelWithCursorPos(t, core.Position{Line: 1, Col: 2})
|
|
m2 := getFinalModel(t, tm2)
|
|
win2 := m2.ActiveWindow()
|
|
if win2.Cursor.Line != 1 {
|
|
t.Error("newTestModelWithCursorPos failed")
|
|
}
|
|
|
|
tm3 := newTestModelWithLinesAndCursorPos(t, []string{"x"}, core.Position{Line: 0, Col: 0})
|
|
m3 := getFinalModel(t, tm3)
|
|
buf3 := m3.ActiveBuffer()
|
|
if buf3.LineCount() != 1 {
|
|
t.Error("newTestModelWithLinesAndCursorPos failed")
|
|
}
|
|
|
|
tm4 := newTestModelWithTermSize(t, []string{"y"}, core.Position{Line: 0, Col: 0}, 50, 20)
|
|
m4 := getFinalModel(t, tm4)
|
|
win4 := m4.ActiveWindow()
|
|
if win4.Width != 50 {
|
|
t.Error("newTestModelWithTermSize failed")
|
|
}
|
|
})
|
|
}
|