File: text_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (176 lines) | stat: -rw-r--r-- 5,316 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package ui

import (
	"errors"
	"testing"

	"src.elv.sh/pkg/eval/vals"
	"src.elv.sh/pkg/tt"
)

func TestT(t *testing.T) {
	tt.Test(t, T,
		Args("test").Rets(Text{&Segment{Text: "test"}}),
		Args("test red", FgRed).Rets(Text{&Segment{
			Text: "test red", Style: Style{Fg: Red}}}),
		Args("test red", FgRed, Bold).Rets(Text{&Segment{
			Text: "test red", Style: Style{Fg: Red, Bold: true}}}),
	)
}

func TestConcat(t *testing.T) {
	tt.Test(t, Concat,
		Args().Rets(Text(nil)),
		Args(T("red", FgRed), T("blue", FgBlue), T("green", FgGreen)).
			Rets(Text{red("red"), blue("blue"), green("green")}),
		// Merging adjacent segments with the same style
		Args(T("red", FgRed), T("red", FgRed)).Rets(T("redred", FgRed)),
		// Concatenating texts with multiple segments
		Args(Concat(T("red", FgRed), T("blue", FgBlue)),
			Concat(T("blue", FgBlue), T("green", FgGreen))).
			Rets(Text{red("red"), blue("blueblue"), green("green")}),
		// Concatenating empty texts
		Args(T(""), T("red", FgRed), T("")).Rets(T("red", FgRed)),
	)
}

func TestTextAsElvishValue(t *testing.T) {
	vals.TestValue(t, T("text")).
		Kind("ui:text").
		Repr("[^styled text]").
		AllKeys("0").
		Index("0", &Segment{Text: "text"}).
		IndexError("a", errors.New("index must be integer"))

	vals.TestValue(t, Concat(T("red", FgRed), T("blue", FgBlue), T("green", FgGreen))).
		Index("0..2", Concat(T("red", FgRed), T("blue", FgBlue)))

	vals.TestValue(t, T("text", FgRed)).
		Repr("[^styled (styled-segment text &fg-color=red)]")
	vals.TestValue(t, T("text", Bold)).
		Repr("[^styled (styled-segment text &bold)]")
}

var (
	text0 = Text{}
	text1 = Text{red("lorem")}
	text2 = Text{red("lorem"), blue("foobar")}
)

var partitionTests = []*tt.Case{
	Args(text0).Rets([]Text{nil}),
	Args(text1).Rets([]Text{text1}),
	Args(text1, 0).Rets([]Text{nil, text1}),
	Args(text1, 1).Rets([]Text{{red("l")}, {red("orem")}}),
	Args(text1, 5).Rets([]Text{text1, nil}),
	Args(text2).Rets([]Text{text2}),
	Args(text2, 0).Rets([]Text{nil, text2}),
	Args(text2, 1).Rets([]Text{
		{red("l")}, {red("orem"), blue("foobar")}}),
	Args(text2, 2).Rets([]Text{
		{red("lo")}, {red("rem"), blue("foobar")}}),
	Args(text2, 5).Rets([]Text{{red("lorem")}, {blue("foobar")}}),
	Args(text2, 6).Rets([]Text{
		{red("lorem"), blue("f")}, {blue("oobar")}}),
	Args(text2, 11).Rets([]Text{text2, nil}),

	Args(text1, 1, 2).Rets([]Text{{red("l")}, {red("o")}, {red("rem")}}),
	Args(text1, 1, 2, 3, 4).Rets([]Text{
		{red("l")}, {red("o")}, {red("r")}, {red("e")}, {red("m")}}),
	Args(text2, 2, 4, 6).Rets([]Text{
		{red("lo")}, {red("re")},
		{red("m"), blue("f")}, {blue("oobar")}}),
	Args(text2, 6, 8).Rets([]Text{
		{red("lorem"), blue("f")}, {blue("oo")}, {blue("bar")}}),
}

func TestPartition(t *testing.T) {
	tt.Test(t, tt.Fn(Text.Partition).Named("Text.Partition"), partitionTests...)
}

func TestCountRune(t *testing.T) {
	text := Text{red("lorem"), blue("ipsum")}
	tt.Test(t, tt.Fn(Text.CountRune).Named("Text.CountRune"),
		Args(text, 'l').Rets(1),
		Args(text, 'i').Rets(1),
		Args(text, 'm').Rets(2),
		Args(text, '\n').Rets(0),
	)
}

func TestCountLines(t *testing.T) {
	tt.Test(t, tt.Fn(Text.CountLines).Named("Text.CountLines"),
		Args(Text{red("lorem")}).Rets(1),
		Args(Text{red("lorem"), blue("ipsum")}).Rets(1),
		Args(Text{red("lor\nem"), blue("ipsum")}).Rets(2),
		Args(Text{red("lor\nem"), blue("ip\nsum")}).Rets(3),
	)
}

func TestSplitByRune(t *testing.T) {
	tt.Test(t, tt.Fn(Text.SplitByRune).Named("Text.SplitByRune"),
		Args(Text{}, '\n').Rets([]Text(nil)),
		Args(Text{red("lorem")}, '\n').Rets([]Text{{red("lorem")}}),
		Args(Text{red("lorem"), blue("ipsum"), red("dolar")}, '\n').Rets(
			[]Text{
				{red("lorem"), blue("ipsum"), red("dolar")},
			}),
		Args(Text{red("lo\nrem")}, '\n').Rets([]Text{
			{red("lo")}, {red("rem")},
		}),
		Args(Text{red("lo\nrem"), blue("ipsum")}, '\n').Rets(
			[]Text{
				{red("lo")},
				{red("rem"), blue("ipsum")},
			}),
		Args(Text{red("lo\nrem"), blue("ip\nsum")}, '\n').Rets(
			[]Text{
				{red("lo")},
				{red("rem"), blue("ip")},
				{blue("sum")},
			}),
		Args(Text{red("lo\nrem"), blue("ip\ns\num"), red("dolar")}, '\n').Rets(
			[]Text{
				{red("lo")},
				{red("rem"), blue("ip")},
				{blue("s")},
				{blue("um"), red("dolar")},
			}),
		Args(Text{red("lorem\n")}, '\n').Rets(
			[]Text{
				{red("lorem")},
				nil,
			}),
	)
}

func TestTrimWcwidth(t *testing.T) {
	tt.Test(t, tt.Fn(Text.TrimWcwidth).Named("Text.TrimWcwidth"),
		Args(Text{}, 1).Rets(Text(nil)),
		Args(Text{red("lorem")}, 3).Rets(Text{red("lor")}),
		Args(Text{red("lorem"), blue("ipsum")}, 6).Rets(
			Text{red("lorem"), blue("i")}),
		Args(Text{red("你好")}, 3).Rets(Text{red("你")}),
		Args(Text{red("你好"), blue("精灵语"), red("x")}, 7).Rets(
			Text{red("你好"), blue("精")}),
	)
}

type textVTStringTest struct {
	text         Text
	wantVTString string
}

func testTextVTString(t *testing.T, tests []textVTStringTest) {
	t.Helper()
	for _, test := range tests {
		vtString := test.text.VTString()
		if vtString != test.wantVTString {
			t.Errorf("got %q, want %q", vtString, test.wantVTString)
		}
	}
}

func red(s string) *Segment   { return &Segment{Style{Fg: Red}, s} }
func blue(s string) *Segment  { return &Segment{Style{Fg: Blue}, s} }
func green(s string) *Segment { return &Segment{Style{Fg: Green}, s} }