Skip to content

Commit

Permalink
fix: bug #236
Browse files Browse the repository at this point in the history
test: add @prestidigitator-mt's test cases with some changes
  • Loading branch information
ndfsa committed Sep 19, 2024
1 parent aff713d commit 07146e3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
6 changes: 2 additions & 4 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func (r *Renderer) PlaceHorizontal(width int, pos Position, str string, opts ...
default: // somewhere in the middle
totalGap := gap + short

split := int(math.Round(float64(totalGap) * pos.value()))
left := totalGap - split
left := int(math.Round(float64(totalGap) * pos.value()))
right := totalGap - left

b.WriteString(ws.render(left))
Expand Down Expand Up @@ -137,8 +136,7 @@ func (r *Renderer) PlaceVertical(height int, pos Position, str string, opts ...W
b.WriteString(str)

default: // Somewhere in the middle
split := int(math.Round(float64(gap) * pos.value()))
top := gap - split
top := int(math.Round(float64(gap) * pos.value()))
bottom := gap - top

b.WriteString(strings.Repeat(emptyLine+"\n", top))
Expand Down
72 changes: 72 additions & 0 deletions position_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package lipgloss_test

import (
"testing"

"github.com/charmbracelet/lipgloss"
)

type blackhole struct{}

func (bh blackhole) Write(b []byte) (int, error) { return len(b), nil }

func TestPlaceHorizontal(t *testing.T) {
testCases := []struct {
w int
s string
pos lipgloss.Position
exp string
}{
// odd spacing
{w: 10, s: "Hello", pos: lipgloss.Left, exp: "Hello "},
{w: 10, s: "Hello", pos: 0, exp: "Hello "},
{w: 10, s: "Hello", pos: 0.000000001, exp: "Hello "},
{w: 10, s: "Hello", pos: lipgloss.Right, exp: " Hello"},
{w: 10, s: "Hello", pos: 1, exp: " Hello"},
{w: 10, s: "Hello", pos: 0.999999999, exp: " Hello"},
{w: 10, s: "Hello", pos: 0.49, exp: " Hello "},
{w: 10, s: "Hello", pos: lipgloss.Center, exp: " Hello "},
{w: 10, s: "Hello", pos: 0.51, exp: " Hello "},
}

for i, testCase := range testCases {
r := lipgloss.NewRenderer(blackhole{})

act := r.PlaceHorizontal(testCase.w, testCase.pos, testCase.s)
exp := testCase.exp

if exp != act {
t.Errorf("Test %d: expected %q, got %q", i, exp, act)
}
}
}

func TestPlaceVertical(t *testing.T) {
testCases := []struct {
height int
content string
position lipgloss.Position
expected string
}{
{height: 3, content: "Hello", position: lipgloss.Top, expected: "Hello\n \n "},
{height: 3, content: "Hello", position: 0, expected: "Hello\n \n "},
{height: 3, content: "Hello", position: 0.000000001, expected: "Hello\n \n "},
{height: 3, content: "Hello", position: lipgloss.Bottom, expected: " \n \nHello"},
{height: 3, content: "Hello", position: 1, expected: " \n \nHello"},
{height: 3, content: "Hello", position: 0.999999999, expected: " \n \nHello"},
{height: 3, content: "Hello", position: 0.49, expected: " \nHello\n "},
{height: 3, content: "Hello", position: lipgloss.Center, expected: " \nHello\n "},
{height: 3, content: "Hello", position: 0.51, expected: " \nHello\n "},
}

for i, testCase := range testCases {
r := lipgloss.NewRenderer(blackhole{})

act := r.PlaceVertical(testCase.height, testCase.position, testCase.content)
exp := testCase.expected

if exp != act {
t.Errorf("Test %d: expected %q, got %q", i, exp, act)
}
}
}

0 comments on commit 07146e3

Please sign in to comment.