Skip to content

Commit

Permalink
Padding, margin and border tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
knz committed Sep 3, 2022
1 parent f65eb7f commit 5fa5021
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
10 changes: 8 additions & 2 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,22 @@ func (s Style) GetBorderLeftBackground() TerminalColor {
return s.getAsColor(borderLeftBackgroundKey)
}

// GetBorderTopWidth returns the width of the top border. If borders contain
// GetBorderTopSize returns the width of the top border. If borders contain
// runes of varying widths, the widest rune is returned. If no border exists on
// the top edge, 0 is returned.
func (s Style) GetBorderTopWidth() int {
func (s Style) GetBorderTopSize() int {
if !s.getAsBool(borderTopKey, false) {
return 0
}
return s.getBorderStyle().GetTopSize()
}

// GetBorderTopWidth is the old name of GetBorderTopSize.
// Preserved for compatibility.
func (s Style) GetBorderTopWidth() int {
return s.GetBorderTopSize()
}

// GetBorderLeftSize returns the width of the left border. If borders contain
// runes of varying widths, the widest rune is returned. If no border exists on
// the left edge, 0 is returned.
Expand Down
85 changes: 84 additions & 1 deletion style_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lipgloss_test

import (
"fmt"
"strings"
"testing"

Expand All @@ -18,8 +19,76 @@ func BenchmarkStyleRender(b *testing.B) {
}
}

// Example_padding exercises the computed padding getters.
func Example_padding() {
s := lipgloss.NewStyle().Padding(10001, 10010, 10100, 11000)

fmt.Println(s.GetPadding())
fmt.Println(s.GetHorizontalPadding())
fmt.Println(s.GetVerticalPadding())

// Output:
// 10001 10010 10100 11000
// 21010
// 20101
}

// Example_margin exercises the computed margin getters.
func Example_margin() {
s := lipgloss.NewStyle().Margin(10001, 10010, 10100, 11000)

fmt.Println(s.GetMargin())
fmt.Println(s.GetHorizontalMargins())
fmt.Println(s.GetVerticalMargins())

// Output:
// 10001 10010 10100 11000
// 21010
// 20101
}

// Example_border exercises the computed border getters.
func Example_border() {
fmt.Println(lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).GetBorderStyle())
fmt.Println(lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).GetBorderStyle())
fmt.Println(lipgloss.NewStyle().BorderStyle(lipgloss.ThickBorder()).GetBorderStyle())
fmt.Println(lipgloss.NewStyle().BorderStyle(lipgloss.DoubleBorder()).GetBorderStyle())
fmt.Println(lipgloss.NewStyle().BorderStyle(lipgloss.HiddenBorder()).GetBorderStyle())

s := lipgloss.NewStyle().Border(
lipgloss.Border{"x", "xx", "xxx", "xxxx", "a", "a", "a", "a"},
true,
)

fmt.Println(s.GetBorder())

// Note: the border size computations seem to be wrong.
// The top/bottom borders should be size 1 (just 1 line)
// and the left/right borders should _add_ the rune sizes,
// not compute the maximum.
// See: https://github.com/charmbracelet/lipgloss/issues/112
fmt.Println(s.GetBorderTopSize(), s.GetBorderBottomSize())
fmt.Println(s.GetBorderLeftSize(), s.GetBorderRightSize())

fmt.Println(s.GetHorizontalBorderSize())
fmt.Println(s.GetVerticalBorderSize())

// Output:
// {โ”€ โ”€ โ”‚ โ”‚ โ”Œ โ” โ”˜ โ””}
// {โ”€ โ”€ โ”‚ โ”‚ โ•ญ โ•ฎ โ•ฏ โ•ฐ}
// {โ” โ” โ”ƒ โ”ƒ โ” โ”“ โ”› โ”—}
// {โ• โ• โ•‘ โ•‘ โ•” โ•— โ• โ•š}
// { }
// {x xx xxx xxxx a a a a} true true true true
// 1 1
// 1 1
// 2
// 2
}

type S = lipgloss.Style

// TestStyle validates most of the Get, Set and Unset methods.
func TestStyle(t *testing.T) {
td := []struct {
changeStyle func(S) S
Expand Down Expand Up @@ -66,7 +135,6 @@ func TestStyle(t *testing.T) {
{func(s S) S { return s.MarginBackground(lipgloss.Color("#0f0")) }, `margin-background: #0f0;`},
{func(s S) S { return s.MaxHeight(3) }, `max-height: 3;`},
{func(s S) S { return s.MaxWidth(3) }, `max-width: 3;`},
{func(s S) S { return s.Padding(1, 2, 3, 4) }, `padding-bottom: 3; padding-left: 4; padding-right: 2; padding-top: 1;`},
{func(s S) S { return s.PaddingBottom(3) }, `padding-bottom: 3;`},
{func(s S) S { return s.PaddingLeft(3) }, `padding-left: 3;`},
{func(s S) S { return s.PaddingRight(3) }, `padding-right: 3;`},
Expand All @@ -77,6 +145,17 @@ func TestStyle(t *testing.T) {
{func(s S) S { return s.Underline(true) }, `underline: true;`},
{func(s S) S { return s.UnderlineSpaces(true) }, `underline-spaces: true;`},
{func(s S) S { return s.Width(3) }, `width: 3;`},
// Variable size setters.
{func(s S) S { return s.Padding(1, 2, 3, 4) }, `padding-bottom: 3; padding-left: 4; padding-right: 2; padding-top: 1;`},
{func(s S) S { return s.Padding(1, 2, 3) }, `padding-bottom: 3; padding-left: 2; padding-right: 2; padding-top: 1;`},
{func(s S) S { return s.Padding(1, 2) }, `padding-bottom: 1; padding-left: 2; padding-right: 2; padding-top: 1;`},
{func(s S) S { return s.Padding() }, ``},
{func(s S) S { return s.Padding(1, 2, 3, 4, 5) }, ``},
{func(s S) S { return s.Margin(1, 2, 3, 4) }, `margin-bottom: 3; margin-left: 4; margin-right: 2; margin-top: 1;`},
{func(s S) S { return s.Margin(1, 2, 3) }, `margin-bottom: 3; margin-left: 2; margin-right: 2; margin-top: 1;`},
{func(s S) S { return s.Margin(1, 2) }, `margin-bottom: 1; margin-left: 2; margin-right: 2; margin-top: 1;`},
{func(s S) S { return s.Margin() }, ``},
{func(s S) S { return s.Margin(1, 2, 3, 4, 5) }, ``},
}

for _, tc := range td {
Expand All @@ -89,6 +168,10 @@ func TestStyle(t *testing.T) {
continue
}

if tc.repr == `` {
continue
}

// Apply the unset function and assert the resulting style is
// empty.
r := strings.Split(tc.repr, ":")
Expand Down

0 comments on commit 5fa5021

Please sign in to comment.