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 eb08616
Show file tree
Hide file tree
Showing 7 changed files with 433 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
59 changes: 57 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,56 @@ 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
}

func (d *driver) renderTest(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "text":
d.text = td.Input
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")
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
126 changes: 126 additions & 0 deletions testdata/color
@@ -0,0 +1,126 @@
set
width: 20;
----
hello!··············🛇

# The remainder of this test is best viewed
# with a text editor that renders ANSI sequences.

set
foreground: #f00
----
hello!··············🛇

set
background: #0f0
----
hello!··············🛇

subtest fill_colors

set
width: 20;
height: 3;
background: unset;
foreground: #f00;
----
hello!··············␤
····················␤
····················🛇

set
foreground: unset;
background: #f00;
----
hello!··············␤
····················␤
····················🛇

subtest end

subtest border_colors

set
clear;
width: 20;
border-style: normal;
border-foreground: #f00
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-foreground: unset;
border-top-foreground: #f00
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-foreground: unset;
border-left-foreground: #f00
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-foreground: unset;
border-right-foreground: #f00
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-foreground: unset;
border-bottom-foreground: #f00
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-foreground: unset;
border-background: #0f0
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-background: unset;
border-top-background: #0f0
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-background: unset;
border-left-background: #0f0
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-background: unset;
border-right-background: #0f0
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇

set
border-background: unset;
border-bottom-background: #0f0
----
┌────────────────────┐␤
│hello!··············│␤
└────────────────────┘🛇


subtest end
96 changes: 96 additions & 0 deletions testdata/formatting
@@ -0,0 +1,96 @@
# Use a text with a space, to show space styling.
text
hello world!
----
ok

set
clear; width: 20;
underline: true
----
hello····world!·····🛇

# Seemingly no difference!
# See issue: https://github.com/charmbracelet/lipgloss/issues/113
set
clear; width: 20;
underline: true; underline-spaces: true
----
hello····world!·····🛇

# Weird output.
# See issue: https://github.com/charmbracelet/lipgloss/issues/113
set
clear; width: 20;
underline: true; underline-spaces: false
----
hello····world!·····🛇

# If underline is not set, underline-spaces does nothing.
set
clear; width: 20;
underline-spaces: true
----
hello····world!·····🛇

set
clear; width: 20;
italic: true
----
hello····world!·····🛇


set
clear; width: 20;
bold: true
----
hello····world!·····🛇


set
clear; width: 20;
strikethrough: true
----
hello····world!·····🛇

# Seemingly no difference!
# See issue: https://github.com/charmbracelet/lipgloss/issues/113
set
clear; width: 20;
strikethrough: true; strikethrough-spaces: true
----
hello····world!·····🛇

# Weird output.
# See issue: https://github.com/charmbracelet/lipgloss/issues/113
set
clear; width: 20;
strikethrough: true; strikethrough-spaces: false
----
hello····world!·····🛇

# If strikethrough is not set, strikethrough-spaces does nothing.
set
clear; width: 20;
strikethrough-spaces: true
----
hello····world!·····🛇

set
clear; width: 20;
reverse: true
----
hello····world!·····🛇


set
clear; width: 20;
blink: true
----
hello····world!·····🛇

set
clear; width: 20;
faint: true
----
hello····world!·····🛇

0 comments on commit eb08616

Please sign in to comment.