Skip to content

Commit

Permalink
more test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 committed Feb 3, 2025
1 parent fd54d9d commit c6e52ba
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
5 changes: 5 additions & 0 deletions internal/viewport/linebuffer/linebuffer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linebuffer

import (
"fmt"
"github.com/charmbracelet/lipgloss/v2"
"github.com/mattn/go-runewidth"
"github.com/robinovitch61/kl/internal/constants"
Expand Down Expand Up @@ -77,6 +78,10 @@ func New(line string) *LineBuffer {
return &lb
}

func (l LineBuffer) Repr() string {
return fmt.Sprintf("LB(%q)", l.content)
}

func (l LineBuffer) Width() int {
if len(l.lineNoAnsiCumWidths) > 0 {
return int(l.lineNoAnsiCumWidths[len(l.lineNoAnsiCumWidths)-1])
Expand Down
1 change: 1 addition & 0 deletions internal/viewport/linebuffer/linebufferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linebuffer
import "github.com/charmbracelet/lipgloss/v2"

type LineBufferer interface {
Repr() string
Width() int
Content() string
SeekToWidth(width int)
Expand Down
17 changes: 16 additions & 1 deletion internal/viewport/linebuffer/multilinebuffer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package linebuffer

import "github.com/charmbracelet/lipgloss/v2"
import (
"github.com/charmbracelet/lipgloss/v2"
)

// MultiLineBuffer implements LineBufferer by wrapping multiple LineBuffers without extra memory allocation
type MultiLineBuffer struct {
Expand Down Expand Up @@ -28,11 +30,24 @@ func NewMulti(buffers ...*LineBuffer) *MultiLineBuffer {
}
}

func (m MultiLineBuffer) Repr() string {
v := "Multi("
for i := range m.buffers {
if i > 0 {
v += ", "
}
v += m.buffers[i].Repr()
}
v += ")"
return v
}

func (m *MultiLineBuffer) Width() int {
return m.totalWidth
}

// TODO LEO: don't use this for e.g. search, instead inject filter into a Matches() bool method here
// TODO LEO: or store the concatenated string on init?
func (m *MultiLineBuffer) Content() string {
totalLen := 0
for _, buf := range m.buffers {
Expand Down
50 changes: 47 additions & 3 deletions internal/viewport/linebuffer/multlinebuffer_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package linebuffer

import "testing"
import (
"github.com/charmbracelet/lipgloss/v2"
"testing"
)

var equivalentLineBuffers = [][]LineBufferer{
{
var equivalentLineBuffers = map[string][]LineBufferer{
"hello world": {
New("hello world"),
NewMulti(
New("hello"),
Expand Down Expand Up @@ -31,6 +34,16 @@ var equivalentLineBuffers = [][]LineBufferer{
},
}

func TestMultiLineBuffer_Width(t *testing.T) {
for _, eq := range equivalentLineBuffers {
for i, lb := range eq {
if lb.Width() != eq[0].Width() {
t.Errorf("expected %d, got %d for line buffer %d", eq[0].Width(), lb.Width(), i)
}
}
}
}

func TestMultiLineBuffer_Content(t *testing.T) {
for _, eq := range equivalentLineBuffers {
for i, lb := range eq {
Expand All @@ -41,4 +54,35 @@ func TestMultiLineBuffer_Content(t *testing.T) {
}
}

func TestMultiLineBuffer_SeekToWidth(t *testing.T) {
tests := []struct {
name string
key string
seekToWidth int
takeWidth int
continuation string
expectedPopLeft string
}{
{
name: "hello world 0",
key: "hello world",
seekToWidth: 0,
takeWidth: 7,
continuation: "",
expectedPopLeft: "hello w",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, eq := range equivalentLineBuffers[tt.key] {
eq.SeekToWidth(tt.seekToWidth)
if actual := eq.PopLeft(tt.takeWidth, tt.continuation, "", lipgloss.NewStyle()); actual != tt.expectedPopLeft {
t.Errorf("for %s, expected %q, got %q", eq.Repr(), tt.expectedPopLeft, actual)
}
}
})
}
}

//func TestMultiLin

0 comments on commit c6e52ba

Please sign in to comment.