Skip to content

Commit

Permalink
Add render tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
knz committed Sep 3, 2022
1 parent 2ae29f9 commit bfcaf17
Show file tree
Hide file tree
Showing 8 changed files with 699 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -3,6 +3,7 @@ module github.com/charmbracelet/lipgloss
go 1.15

require (
github.com/cockroachdb/datadriven v1.0.2
github.com/knz/lipgloss-convert v0.1.1
github.com/mattn/go-runewidth v0.0.13
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68
Expand Down
2 changes: 2 additions & 0 deletions go.sum
@@ -1,4 +1,6 @@
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
github.com/cockroachdb/datadriven v1.0.2 h1:H9MtNqVoVhvd9nCBwOyDjUEdZCREqbIdCJD93PBm/jA=
github.com/cockroachdb/datadriven v1.0.2/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/knz/lipgloss-convert v0.1.1 h1:11Wfcdif9jyyI5H3ohANyqkmIpVPjiBoGZdCzPWz9fA=
github.com/knz/lipgloss-convert v0.1.1/go.mod h1:S14GmtoiW/VAHqB7xEzuZOt0/G6GQ2dfjJN0fHpm30Q=
Expand Down
66 changes: 64 additions & 2 deletions style_test.go
Expand Up @@ -6,7 +6,9 @@ import (
"testing"

"github.com/charmbracelet/lipgloss"
"github.com/cockroachdb/datadriven"
lipglossc "github.com/knz/lipgloss-convert"
"github.com/muesli/termenv"
)

func BenchmarkStyleRender(b *testing.B) {
Expand Down Expand Up @@ -115,8 +117,8 @@ func Example_frame() {

type S = lipgloss.Style

// TestStyle validates most of the Get, Set and Unset methods.
func TestStyle(t *testing.T) {
// TestStyleMethods validates most of the Get, Set and Unset methods.
func TestStyleMethod(t *testing.T) {
g := lipgloss.Color("#0f0")
r := lipgloss.Color("#f00")
b := lipgloss.Color("#00f")
Expand Down Expand Up @@ -255,3 +257,63 @@ func TestStyle(t *testing.T) {
}
}
}

func TestRender(t *testing.T) {
curProfile := lipgloss.ColorProfile()
defer lipgloss.SetColorProfile(curProfile)

lipgloss.SetColorProfile(termenv.TrueColor)

datadriven.Walk(t, "testdata", func(t *testing.T, path string) {
d := driver{
s: lipgloss.NewStyle(),
text: "hello!",
}
datadriven.RunTest(t, path, func(t *testing.T, td *datadriven.TestData) string {
return d.renderTest(t, td)
})
})
}

type driver struct {
s lipgloss.Style
text string
spaces bool
}

func (d *driver) renderTest(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "text":
d.text = td.Input
return "ok"

case "spvis":
d.spaces = !d.spaces
return "ok"

case "show":
return lipglossc.Export(d.s)

case "set":
newStyle, err := lipglossc.Import(d.s, td.Input)
if err != nil {
t.Fatalf("%s: invalid style: %v", td.Pos, err)
}
d.s = newStyle

o := d.s.Render(d.text)
o = strings.ReplaceAll(o, "\n", "␤\n")
if !d.spaces {
o = strings.ReplaceAll(o, " ", "·")
}
// Add a "no newline at end" marker if there was no newline at the end.
if len(o) == 0 || o[len(o)-1] != '\n' {
o += "🛇"
}
return o

default:
t.Fatalf("%s: unknown command: %q", td.Pos, td.Cmd)
return "" // unreachable
}
}
55 changes: 55 additions & 0 deletions testdata/align
@@ -0,0 +1,55 @@
set
width: 20;
----
hello!··············🛇

subtest single_line

set
align: right
----
··············hello!🛇

set
align: center
----
·······hello!·······🛇

set
align: left
----
hello!··············🛇

subtest end

subtest multi_line

text
small

long line
----
ok

set
align: right
----
···············small␤
····················␤
···········long·line🛇

set
align: center
----
·······small········␤
····················␤
·····long·line······🛇

set
align: left
----
small···············␤
····················␤
long·line···········🛇

subtest end

0 comments on commit bfcaf17

Please sign in to comment.