Skip to content

Commit eb08616

Browse files
committed
Add render tests.
1 parent 2ae29f9 commit eb08616

File tree

7 files changed

+433
-2
lines changed

7 files changed

+433
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/charmbracelet/lipgloss
33
go 1.15
44

55
require (
6+
github.com/cockroachdb/datadriven v1.0.2
67
github.com/knz/lipgloss-convert v0.1.1
78
github.com/mattn/go-runewidth v0.0.13
89
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
2+
github.com/cockroachdb/datadriven v1.0.2 h1:H9MtNqVoVhvd9nCBwOyDjUEdZCREqbIdCJD93PBm/jA=
3+
github.com/cockroachdb/datadriven v1.0.2/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=
24
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
35
github.com/knz/lipgloss-convert v0.1.1 h1:11Wfcdif9jyyI5H3ohANyqkmIpVPjiBoGZdCzPWz9fA=
46
github.com/knz/lipgloss-convert v0.1.1/go.mod h1:S14GmtoiW/VAHqB7xEzuZOt0/G6GQ2dfjJN0fHpm30Q=

style_test.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"testing"
77

88
"github.com/charmbracelet/lipgloss"
9+
"github.com/cockroachdb/datadriven"
910
lipglossc "github.com/knz/lipgloss-convert"
11+
"github.com/muesli/termenv"
1012
)
1113

1214
func BenchmarkStyleRender(b *testing.B) {
@@ -115,8 +117,8 @@ func Example_frame() {
115117

116118
type S = lipgloss.Style
117119

118-
// TestStyle validates most of the Get, Set and Unset methods.
119-
func TestStyle(t *testing.T) {
120+
// TestStyleMethods validates most of the Get, Set and Unset methods.
121+
func TestStyleMethod(t *testing.T) {
120122
g := lipgloss.Color("#0f0")
121123
r := lipgloss.Color("#f00")
122124
b := lipgloss.Color("#00f")
@@ -255,3 +257,56 @@ func TestStyle(t *testing.T) {
255257
}
256258
}
257259
}
260+
261+
func TestRender(t *testing.T) {
262+
curProfile := lipgloss.ColorProfile()
263+
defer lipgloss.SetColorProfile(curProfile)
264+
265+
lipgloss.SetColorProfile(termenv.TrueColor)
266+
267+
datadriven.Walk(t, "testdata", func(t *testing.T, path string) {
268+
d := driver{
269+
s: lipgloss.NewStyle(),
270+
text: "hello!",
271+
}
272+
datadriven.RunTest(t, path, func(t *testing.T, td *datadriven.TestData) string {
273+
return d.renderTest(t, td)
274+
})
275+
})
276+
}
277+
278+
type driver struct {
279+
s lipgloss.Style
280+
text string
281+
}
282+
283+
func (d *driver) renderTest(t *testing.T, td *datadriven.TestData) string {
284+
switch td.Cmd {
285+
case "text":
286+
d.text = td.Input
287+
return "ok"
288+
289+
case "show":
290+
return lipglossc.Export(d.s)
291+
292+
case "set":
293+
newStyle, err := lipglossc.Import(d.s, td.Input)
294+
if err != nil {
295+
t.Fatalf("%s: invalid style: %v", td.Pos, err)
296+
}
297+
d.s = newStyle
298+
299+
o := d.s.Render(d.text)
300+
o = strings.ReplaceAll(o, "\n", "␤\n")
301+
o = strings.ReplaceAll(o, " ", "·")
302+
// Add a "no newline at end" marker if there was no newline at the end.
303+
if len(o) == 0 || o[len(o)-1] != '\n' {
304+
o += "🛇"
305+
}
306+
return o
307+
308+
default:
309+
t.Fatalf("%s: unknown command: %q", td.Pos, td.Cmd)
310+
return "" // unreachable
311+
}
312+
}

testdata/align

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
set
2+
width: 20;
3+
----
4+
hello!··············🛇
5+
6+
subtest single_line
7+
8+
set
9+
align: right
10+
----
11+
··············hello!🛇
12+
13+
set
14+
align: center
15+
----
16+
·······hello!·······🛇
17+
18+
set
19+
align: left
20+
----
21+
hello!··············🛇
22+
23+
subtest end
24+
25+
subtest multi_line
26+
27+
text
28+
small
29+
30+
long line
31+
----
32+
ok
33+
34+
set
35+
align: right
36+
----
37+
···············small␤
38+
····················␤
39+
···········long·line🛇
40+
41+
set
42+
align: center
43+
----
44+
·······small········␤
45+
····················␤
46+
·····long·line······🛇
47+
48+
set
49+
align: left
50+
----
51+
small···············␤
52+
····················␤
53+
long·line···········🛇
54+
55+
subtest end

testdata/color

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
set
2+
width: 20;
3+
----
4+
hello!··············🛇
5+
6+
# The remainder of this test is best viewed
7+
# with a text editor that renders ANSI sequences.
8+
9+
set
10+
foreground: #f00
11+
----
12+
hello!··············🛇
13+
14+
set
15+
background: #0f0
16+
----
17+
hello!··············🛇
18+
19+
subtest fill_colors
20+
21+
set
22+
width: 20;
23+
height: 3;
24+
background: unset;
25+
foreground: #f00;
26+
----
27+
hello!··············␤
28+
····················␤
29+
····················🛇
30+
31+
set
32+
foreground: unset;
33+
background: #f00;
34+
----
35+
hello!··············␤
36+
····················␤
37+
····················🛇
38+
39+
subtest end
40+
41+
subtest border_colors
42+
43+
set
44+
clear;
45+
width: 20;
46+
border-style: normal;
47+
border-foreground: #f00
48+
----
49+
┌────────────────────┐␤
50+
│hello!··············│␤
51+
└────────────────────┘🛇
52+
53+
set
54+
border-foreground: unset;
55+
border-top-foreground: #f00
56+
----
57+
┌────────────────────┐␤
58+
│hello!··············│␤
59+
└────────────────────┘🛇
60+
61+
set
62+
border-foreground: unset;
63+
border-left-foreground: #f00
64+
----
65+
┌────────────────────┐␤
66+
│hello!··············│␤
67+
└────────────────────┘🛇
68+
69+
set
70+
border-foreground: unset;
71+
border-right-foreground: #f00
72+
----
73+
┌────────────────────┐␤
74+
│hello!··············│␤
75+
└────────────────────┘🛇
76+
77+
set
78+
border-foreground: unset;
79+
border-bottom-foreground: #f00
80+
----
81+
┌────────────────────┐␤
82+
│hello!··············│␤
83+
└────────────────────┘🛇
84+
85+
set
86+
border-foreground: unset;
87+
border-background: #0f0
88+
----
89+
┌────────────────────┐␤
90+
│hello!··············│␤
91+
└────────────────────┘🛇
92+
93+
set
94+
border-background: unset;
95+
border-top-background: #0f0
96+
----
97+
┌────────────────────┐␤
98+
│hello!··············│␤
99+
└────────────────────┘🛇
100+
101+
set
102+
border-background: unset;
103+
border-left-background: #0f0
104+
----
105+
┌────────────────────┐␤
106+
│hello!··············│␤
107+
└────────────────────┘🛇
108+
109+
set
110+
border-background: unset;
111+
border-right-background: #0f0
112+
----
113+
┌────────────────────┐␤
114+
│hello!··············│␤
115+
└────────────────────┘🛇
116+
117+
set
118+
border-background: unset;
119+
border-bottom-background: #0f0
120+
----
121+
┌────────────────────┐␤
122+
│hello!··············│␤
123+
└────────────────────┘🛇
124+
125+
126+
subtest end

testdata/formatting

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Use a text with a space, to show space styling.
2+
text
3+
hello world!
4+
----
5+
ok
6+
7+
set
8+
clear; width: 20;
9+
underline: true
10+
----
11+
hello····world!·····🛇
12+
13+
# Seemingly no difference!
14+
# See issue: https://github.com/charmbracelet/lipgloss/issues/113
15+
set
16+
clear; width: 20;
17+
underline: true; underline-spaces: true
18+
----
19+
hello····world!·····🛇
20+
21+
# Weird output.
22+
# See issue: https://github.com/charmbracelet/lipgloss/issues/113
23+
set
24+
clear; width: 20;
25+
underline: true; underline-spaces: false
26+
----
27+
hello····world!·····🛇
28+
29+
# If underline is not set, underline-spaces does nothing.
30+
set
31+
clear; width: 20;
32+
underline-spaces: true
33+
----
34+
hello····world!·····🛇
35+
36+
set
37+
clear; width: 20;
38+
italic: true
39+
----
40+
hello····world!·····🛇
41+
42+
43+
set
44+
clear; width: 20;
45+
bold: true
46+
----
47+
hello····world!·····🛇
48+
49+
50+
set
51+
clear; width: 20;
52+
strikethrough: true
53+
----
54+
hello····world!·····🛇
55+
56+
# Seemingly no difference!
57+
# See issue: https://github.com/charmbracelet/lipgloss/issues/113
58+
set
59+
clear; width: 20;
60+
strikethrough: true; strikethrough-spaces: true
61+
----
62+
hello····world!·····🛇
63+
64+
# Weird output.
65+
# See issue: https://github.com/charmbracelet/lipgloss/issues/113
66+
set
67+
clear; width: 20;
68+
strikethrough: true; strikethrough-spaces: false
69+
----
70+
hello····world!·····🛇
71+
72+
# If strikethrough is not set, strikethrough-spaces does nothing.
73+
set
74+
clear; width: 20;
75+
strikethrough-spaces: true
76+
----
77+
hello····world!·····🛇
78+
79+
set
80+
clear; width: 20;
81+
reverse: true
82+
----
83+
hello····world!·····🛇
84+
85+
86+
set
87+
clear; width: 20;
88+
blink: true
89+
----
90+
hello····world!·····🛇
91+
92+
set
93+
clear; width: 20;
94+
faint: true
95+
----
96+
hello····world!·····🛇

0 commit comments

Comments
 (0)