Gim/internal/core/buffer_test.go
2026-03-06 18:27:16 -07:00

303 lines
7.1 KiB
Go

package core
import "testing"
// --------------------------------------------------
// Buffer Tests (generated by ClaudeCode)
// --------------------------------------------------
func TestBuffer_InsertLine(t *testing.T) {
t.Run("inserts at beginning", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"line 1", "line 2"}).
Build()
buf.InsertLine(0, "new line")
if buf.LineCount() != 3 {
t.Errorf("expected 3 lines, got %d", buf.LineCount())
}
if buf.Line(0) != "new line" {
t.Errorf("expected 'new line', got '%s'", buf.Line(0))
}
if buf.Line(1) != "line 1" {
t.Errorf("expected 'line 1' at index 1, got '%s'", buf.Line(1))
}
})
t.Run("inserts at end", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"line 1", "line 2"}).
Build()
buf.InsertLine(2, "new line")
if buf.LineCount() != 3 {
t.Errorf("expected 3 lines, got %d", buf.LineCount())
}
if buf.Line(2) != "new line" {
t.Errorf("expected 'new line' at end, got '%s'", buf.Line(2))
}
})
t.Run("inserts in middle", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"line 1", "line 3"}).
Build()
buf.InsertLine(1, "line 2")
if buf.LineCount() != 3 {
t.Errorf("expected 3 lines, got %d", buf.LineCount())
}
if buf.Line(1) != "line 2" {
t.Errorf("expected 'line 2' at index 1, got '%s'", buf.Line(1))
}
})
t.Run("handles empty buffer", func(t *testing.T) {
buf := NewBufferBuilder().Build()
buf.InsertLine(0, "first line")
if buf.LineCount() != 2 { // Original empty line + new line
t.Errorf("expected 2 lines, got %d", buf.LineCount())
}
})
}
func TestBuffer_DeleteLine(t *testing.T) {
t.Run("deletes from beginning", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"DELETE ME", "line 2", "line 3"}).
Build()
buf.DeleteLine(0)
if buf.LineCount() != 2 {
t.Errorf("expected 2 lines, got %d", buf.LineCount())
}
if buf.Line(0) != "line 2" {
t.Errorf("expected 'line 2' at index 0, got '%s'", buf.Line(0))
}
})
t.Run("deletes from middle", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"line 1", "DELETE ME", "line 3"}).
Build()
buf.DeleteLine(1)
if buf.LineCount() != 2 {
t.Errorf("expected 2 lines, got %d", buf.LineCount())
}
if buf.Line(1) != "line 3" {
t.Errorf("expected 'line 3' at index 1, got '%s'", buf.Line(1))
}
})
t.Run("deletes from end", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"line 1", "line 2", "DELETE ME"}).
Build()
buf.DeleteLine(2)
if buf.LineCount() != 2 {
t.Errorf("expected 2 lines, got %d", buf.LineCount())
}
if buf.Line(1) != "line 2" {
t.Errorf("expected 'line 2' at index 1, got '%s'", buf.Line(1))
}
})
t.Run("can delete all lines", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"only line"}).
Build()
buf.DeleteLine(0)
// Buffer allows being completely empty (0 lines)
if buf.LineCount() != 0 {
t.Errorf("expected 0 lines after deleting last line, got %d", buf.LineCount())
}
})
}
func TestBuffer_SetLine(t *testing.T) {
t.Run("updates existing line", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"old content"}).
Build()
buf.SetLine(0, "new content")
if buf.Line(0) != "new content" {
t.Errorf("expected 'new content', got '%s'", buf.Line(0))
}
})
t.Run("updates middle line", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"line 1", "old", "line 3"}).
Build()
buf.SetLine(1, "new")
if buf.Line(1) != "new" {
t.Errorf("expected 'new', got '%s'", buf.Line(1))
}
// Verify other lines unchanged
if buf.Line(0) != "line 1" {
t.Error("line 0 should be unchanged")
}
if buf.Line(2) != "line 3" {
t.Error("line 2 should be unchanged")
}
})
t.Run("can set to empty string", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"has content"}).
Build()
buf.SetLine(0, "")
if buf.Line(0) != "" {
t.Errorf("expected empty line, got '%s'", buf.Line(0))
}
})
}
func TestBuffer_LineCount(t *testing.T) {
t.Run("empty buffer has one line", func(t *testing.T) {
buf := NewBufferBuilder().Build()
if buf.LineCount() != 1 {
t.Errorf("expected 1 line in empty buffer, got %d", buf.LineCount())
}
})
t.Run("counts multiple lines", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"a", "b", "c", "d", "e"}).
Build()
if buf.LineCount() != 5 {
t.Errorf("expected 5 lines, got %d", buf.LineCount())
}
})
t.Run("counts after insertions", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"line 1"}).
Build()
buf.InsertLine(1, "line 2")
buf.InsertLine(2, "line 3")
if buf.LineCount() != 3 {
t.Errorf("expected 3 lines, got %d", buf.LineCount())
}
})
t.Run("counts after deletions", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"a", "b", "c", "d", "e"}).
Build()
buf.DeleteLine(2)
buf.DeleteLine(2)
if buf.LineCount() != 3 {
t.Errorf("expected 3 lines after 2 deletions, got %d", buf.LineCount())
}
})
}
func TestBuffer_Line(t *testing.T) {
t.Run("retrieves correct line", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"first", "second", "third"}).
Build()
if buf.Line(0) != "first" {
t.Errorf("expected 'first', got '%s'", buf.Line(0))
}
if buf.Line(1) != "second" {
t.Errorf("expected 'second', got '%s'", buf.Line(1))
}
if buf.Line(2) != "third" {
t.Errorf("expected 'third', got '%s'", buf.Line(2))
}
})
t.Run("handles special characters", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"hello\tworld", "foo\nbar"}).
Build()
if buf.Line(0) != "hello\tworld" {
t.Errorf("expected tabs preserved, got '%s'", buf.Line(0))
}
})
t.Run("handles unicode", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"hello 世界", "emoji 🎉"}).
Build()
if buf.Line(0) != "hello 世界" {
t.Errorf("expected unicode preserved, got '%s'", buf.Line(0))
}
if buf.Line(1) != "emoji 🎉" {
t.Errorf("expected emoji preserved, got '%s'", buf.Line(1))
}
})
}
func TestBufferBuilder(t *testing.T) {
t.Run("builds with default values", func(t *testing.T) {
buf := NewBufferBuilder().Build()
if buf.LineCount() != 1 {
t.Errorf("expected 1 empty line, got %d", buf.LineCount())
}
if buf.Filename != "" {
t.Errorf("expected empty filename, got '%s'", buf.Filename)
}
})
t.Run("builds with custom lines", func(t *testing.T) {
buf := NewBufferBuilder().
WithLines([]string{"line 1", "line 2"}).
Build()
if buf.LineCount() != 2 {
t.Errorf("expected 2 lines, got %d", buf.LineCount())
}
})
t.Run("builds with filename", func(t *testing.T) {
buf := NewBufferBuilder().
WithFilename("test.txt").
Build()
if buf.Filename != "test.txt" {
t.Errorf("expected filename 'test.txt', got '%s'", buf.Filename)
}
})
t.Run("builds with filetype", func(t *testing.T) {
buf := NewBufferBuilder().
WithFiletype("go").
Build()
if buf.Filetype != "go" {
t.Errorf("expected filetype 'go', got '%s'", buf.Filetype)
}
})
}