Skip to content

Commit 181db6b

Browse files
committed
fix: bug #236
test: add @prestidigitator-mt's test cases with some changes
1 parent aff713d commit 181db6b

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

position.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ func (r *Renderer) PlaceHorizontal(width int, pos Position, str string, opts ...
8080
default: // somewhere in the middle
8181
totalGap := gap + short
8282

83-
split := int(math.Round(float64(totalGap) * pos.value()))
84-
left := totalGap - split
85-
right := totalGap - left
83+
right := int(math.Round(float64(totalGap) * (1.0 - pos.value())))
84+
left := totalGap - right
8685

8786
b.WriteString(ws.render(left))
8887
b.WriteString(l)
@@ -137,9 +136,8 @@ func (r *Renderer) PlaceVertical(height int, pos Position, str string, opts ...W
137136
b.WriteString(str)
138137

139138
default: // Somewhere in the middle
140-
split := int(math.Round(float64(gap) * pos.value()))
141-
top := gap - split
142-
bottom := gap - top
139+
bottom := int(math.Round(float64(gap) * (1.0 - pos.value())))
140+
top := gap - bottom
143141

144142
b.WriteString(strings.Repeat(emptyLine+"\n", top))
145143
b.WriteString(str)

position_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package lipgloss_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/charmbracelet/lipgloss"
7+
)
8+
9+
type blackhole struct{}
10+
11+
func (bh blackhole) Write(b []byte) (int, error) { return len(b), nil }
12+
13+
func TestPlaceHorizontal(t *testing.T) {
14+
testCases := []struct {
15+
w int
16+
s string
17+
pos lipgloss.Position
18+
exp string
19+
}{
20+
// odd spacing
21+
{w: 10, s: "Hello", pos: lipgloss.Left, exp: "Hello "},
22+
{w: 10, s: "Hello", pos: 0, exp: "Hello "},
23+
{w: 10, s: "Hello", pos: 0.000000001, exp: "Hello "},
24+
{w: 10, s: "Hello", pos: lipgloss.Right, exp: " Hello"},
25+
{w: 10, s: "Hello", pos: 1, exp: " Hello"},
26+
{w: 10, s: "Hello", pos: 0.999999999, exp: " Hello"},
27+
{w: 10, s: "Hello", pos: 0.49, exp: " Hello "},
28+
{w: 10, s: "Hello", pos: lipgloss.Center, exp: " Hello "},
29+
{w: 10, s: "Hello", pos: 0.51, exp: " Hello "},
30+
}
31+
32+
for i, testCase := range testCases {
33+
r := lipgloss.NewRenderer(blackhole{})
34+
35+
act := r.PlaceHorizontal(testCase.w, testCase.pos, testCase.s)
36+
exp := testCase.exp
37+
38+
if exp != act {
39+
t.Errorf("Test %d: expected %q, got %q", i, exp, act)
40+
}
41+
}
42+
}
43+
44+
func TestPlaceVertical(t *testing.T) {
45+
testCases := []struct {
46+
height int
47+
content string
48+
position lipgloss.Position
49+
expected string
50+
}{
51+
{height: 3, content: "Hello", position: lipgloss.Top, expected: "Hello\n \n "},
52+
{height: 3, content: "Hello", position: 0, expected: "Hello\n \n "},
53+
{height: 3, content: "Hello", position: 0.000000001, expected: "Hello\n \n "},
54+
{height: 3, content: "Hello", position: lipgloss.Bottom, expected: " \n \nHello"},
55+
{height: 3, content: "Hello", position: 1, expected: " \n \nHello"},
56+
{height: 3, content: "Hello", position: 0.999999999, expected: " \n \nHello"},
57+
{height: 4, content: "Hello", position: 0.49, expected: " \nHello\n \n "},
58+
{height: 4, content: "Hello", position: lipgloss.Center, expected: " \nHello\n \n "},
59+
{height: 4, content: "Hello", position: 0.51, expected: " \n \nHello\n "},
60+
}
61+
62+
for i, testCase := range testCases {
63+
r := lipgloss.NewRenderer(blackhole{})
64+
65+
act := r.PlaceVertical(testCase.height, testCase.position, testCase.content)
66+
exp := testCase.expected
67+
68+
if exp != act {
69+
t.Errorf("Test %d: expected %q, got %q", i, exp, act)
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)