Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TestPlace for position.go #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
ssh_example_ed25519*
*.out
125 changes: 125 additions & 0 deletions position_test.go
@@ -0,0 +1,125 @@
package lipgloss

import (
"fmt"
"strings"
"testing"
)

const (
topRight uint = iota
topLeft
bottomRight
bottomLeft
)

func TestPlace(t *testing.T) {
// TODO: make tests with word len that changes
word := "Hello"

tests := []struct {
name string
size int
horizontalPosition Position
verticalPosition Position
want uint
}{
{
name: "top left, no padding",
size: 1,
horizontalPosition: Top,
verticalPosition: Top,
want: topLeft,
},
{
name: "top left, some padding",
size: 3,
horizontalPosition: Left,
verticalPosition: Top,
want: topLeft,
},
{
name: "top right, some padding",
size: 3,
horizontalPosition: Right,
verticalPosition: Top,
want: topRight,
},
{
name: "bottom left, some padding",
size: 3,
horizontalPosition: Left,
verticalPosition: Bottom,
want: bottomLeft,
},
{
name: "bottom right, some padding",
size: 3,
horizontalPosition: Right,
verticalPosition: Bottom,
want: bottomRight,
},
{
name: "top left, padding gt word",
size: 7,
horizontalPosition: Left,
verticalPosition: Top,
want: topLeft,
},
{
name: "top right, padding gt word",
size: 7,
horizontalPosition: Right,
verticalPosition: Top,
want: topRight,
},
{
name: "bottom left, padding gt word",
size: 7,
horizontalPosition: Left,
verticalPosition: Bottom,
want: bottomLeft,
},
{
name: "bottom right, padding gt word",
size: 7,
horizontalPosition: Right,
verticalPosition: Bottom,
want: bottomRight,
},
}
for _, tc := range tests {
got := Place(tc.size, tc.size, tc.horizontalPosition, tc.verticalPosition, word, WithWhitespaceForeground(NoColor{}))
want := manualFormatString(tc.want, tc.size, word)
if got != want {
fmt.Println([]byte(got))
fmt.Println([]byte(want))
t.Errorf("%s\ngot: %s\nwant: %s", tc.name, []byte(got), []byte(want))
}
}
}

func manualFormatString(choice uint, size int, word string) string {
var cols string
var linewithword string

if len(word) < size {
cols = strings.Repeat(" ", size)
linewithword = strings.Repeat(" ", size-len(word))
} else {
cols = strings.Repeat(" ", len(word))
linewithword = ""
}
leftFiller := strings.Repeat(cols+"\n", size-1)
rightFiller := strings.Repeat("\n"+cols, size-1)
switch choice {
case topRight:
return linewithword + word + rightFiller
case bottomRight:
return leftFiller + linewithword + word
case bottomLeft:
return leftFiller + word + linewithword
default:
return word + linewithword + rightFiller
}
}
4 changes: 2 additions & 2 deletions style_test.go
Expand Up @@ -50,8 +50,8 @@ func TestStyleRender(t *testing.T) {
}

for i, tc := range tt {
s := tc.style.Copy().SetString("hello")
res := s.Render()
SetColorProfile(termenv.TrueColor)
res := tc.style.Render("hello")
if res != tc.expected {
t.Errorf("Test %d, expected:\n\n`%s`\n`%s`\n\nActual output:\n\n`%s`\n`%s`\n\n",
i, tc.expected, formatEscapes(tc.expected),
Expand Down