text_test.go

  1package text
  2
  3import (
  4	"reflect"
  5	"strings"
  6	"testing"
  7)
  8
  9func TestWrap(t *testing.T) {
 10	cases := []struct {
 11		Input, Output string
 12		Lim           int
 13	}{
 14		// A simple word passes through.
 15		{
 16			"foo",
 17			"foo",
 18			4,
 19		},
 20		// Word breaking
 21		{
 22			"foobarbaz",
 23			"foob\narba\nz",
 24			4,
 25		},
 26		// Lines are broken at whitespace.
 27		{
 28			"foo bar baz",
 29			"foo\nbar\nbaz",
 30			4,
 31		},
 32		// Word breaking
 33		{
 34			"foo bars bazzes",
 35			"foo\nbars\nbazz\nes",
 36			4,
 37		},
 38		// A word that would run beyond the width is wrapped.
 39		{
 40			"fo sop",
 41			"fo\nsop",
 42			4,
 43		},
 44		// A tab counts as 4 characters.
 45		{
 46			"foo\nb\t r\n baz",
 47			"foo\nb\nr\n baz",
 48			4,
 49		},
 50		// Trailing whitespace is removed after used for wrapping.
 51		// Runs of whitespace on which a line is broken are removed.
 52		{
 53			"foo    \nb   ar   ",
 54			"foo\n\nb\nar\n",
 55			4,
 56		},
 57		// An explicit line break at the end of the input is preserved.
 58		{
 59			"foo bar baz\n",
 60			"foo\nbar\nbaz\n",
 61			4,
 62		},
 63		// Explicit break are always preserved.
 64		{
 65			"\nfoo bar\n\n\nbaz\n",
 66			"\nfoo\nbar\n\n\nbaz\n",
 67			4,
 68		},
 69		// Ignore complete words with terminal color sequence
 70		{
 71			"foo \x1b[31mbar\x1b[0m baz",
 72			"foo\n\x1b[31mbar\x1b[0m\nbaz",
 73			4,
 74		},
 75		// Handle words with colors sequence inside the word
 76		{
 77			"foo b\x1b[31mbar\x1b[0mr baz",
 78			"foo\nb\x1b[31mbar\n\x1b[0mr\nbaz",
 79			4,
 80		},
 81		// Break words with colors sequence inside the word
 82		{
 83			"foo bb\x1b[31mbar\x1b[0mr baz",
 84			"foo\nbb\x1b[31mba\nr\x1b[0mr\nbaz",
 85			4,
 86		},
 87		// Complete example:
 88		{
 89			" This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* baz  \nBAM    ",
 90			" This\nis a\nlist:\n\n    *\nfoo\n    *\nbar\n\n\n    *\nbaz\nBAM\n",
 91			6,
 92		},
 93		// Handle chinese (wide characters)
 94		{
 95			"一只敏捷的狐狸跳过了一只懒狗。",
 96			"一只敏捷的狐\n狸跳过了一只\n懒狗。",
 97			12,
 98		},
 99		// Handle chinese with colors
100		{
101			"一只敏捷的\x1b[31m狐狸跳过\x1b[0m了一只懒狗。",
102			"一只敏捷的\x1b[31m狐\n狸跳过\x1b[0m了一只\n懒狗。",
103			12,
104		},
105		// Handle mixed wide and short characters
106		{
107			"敏捷 A quick 的狐狸 fox 跳过 jumps over a lazy 了一只懒狗 dog。",
108			"敏捷 A quick\n的狐狸 fox\n跳过 jumps\nover a lazy\n了一只懒狗\ndog。",
109			12,
110		},
111		// Handle mixed wide and short characters with color
112		{
113			"敏捷 A \x1b31mquick 的狐狸 fox 跳\x1b0m过 jumps over a lazy 了一只懒狗 dog。",
114			"敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
115			12,
116		},
117	}
118
119	for i, tc := range cases {
120		actual, lines := Wrap(tc.Input, tc.Lim)
121		if actual != tc.Output {
122			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s`\n\nActual Output:\n\n`%s`",
123				i, tc.Input, tc.Output, actual)
124		}
125
126		expected := len(strings.Split(tc.Output, "\n"))
127		if expected != lines {
128			t.Fatalf("Case %d Nb lines mismatch\nExpected:%d\nActual:%d",
129				i, expected, lines)
130		}
131	}
132}
133
134func TestWrapLeftPadded(t *testing.T) {
135	cases := []struct {
136		input, output string
137		lim, pad      int
138	}{
139		{
140			"The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.",
141			`    The Lorem ipsum text is typically composed of
142    pseudo-Latin words. It is commonly used as placeholder
143    text to examine or demonstrate the visual effects of
144    various graphic design.`,
145			59, 4,
146		},
147		// Handle Chinese
148		{
149			"婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。",
150			`    婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷
151    錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂
152    一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟
153    葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋
154    佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈
155    丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲
156    兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖
157    蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。`,
158			59, 4,
159		},
160	}
161
162	for i, tc := range cases {
163		actual, lines := WrapLeftPadded(tc.input, tc.lim, tc.pad)
164		if actual != tc.output {
165			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n`\n%s`\n\nActual Output:\n`\n%s\n%s`",
166				i, tc.input, tc.output,
167				"|"+strings.Repeat("-", tc.lim-2)+"|",
168				actual)
169		}
170
171		expected := len(strings.Split(tc.output, "\n"))
172		if expected != lines {
173			t.Fatalf("Case %d Nb lines mismatch\nExpected:%d\nActual:%d",
174				i, expected, lines)
175		}
176	}
177}
178
179func TestWordLen(t *testing.T) {
180	cases := []struct {
181		Input  string
182		Length int
183	}{
184		// A simple word
185		{
186			"foo",
187			3,
188		},
189		// A simple word with colors
190		{
191			"\x1b[31mbar\x1b[0m",
192			3,
193		},
194		// Handle prefix and suffix properly
195		{
196			"foo\x1b[31mfoobarHoy\x1b[0mbaaar",
197			17,
198		},
199		// Handle chinese
200		{
201			"快檢什麼望對",
202			12,
203		},
204		// Handle chinese with colors
205		{
206			"快\x1b[31m檢什麼\x1b[0m望對",
207			12,
208		},
209	}
210
211	for i, tc := range cases {
212		l := wordLen(tc.Input)
213		if l != tc.Length {
214			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%d`\n\nActual Output:\n\n`%d`",
215				i, tc.Input, tc.Length, l)
216		}
217	}
218}
219
220func TestSplitWord(t *testing.T) {
221	cases := []struct {
222		Input            string
223		Length           int
224		Result, Leftover string
225	}{
226		// A simple word passes through.
227		{
228			"foo",
229			4,
230			"foo", "",
231		},
232		// Cut at the right place
233		{
234			"foobarHoy",
235			4,
236			"foob", "arHoy",
237		},
238		// A simple word passes through with colors
239		{
240			"\x1b[31mbar\x1b[0m",
241			4,
242			"\x1b[31mbar\x1b[0m", "",
243		},
244		// Cut at the right place with colors
245		{
246			"\x1b[31mfoobarHoy\x1b[0m",
247			4,
248			"\x1b[31mfoob", "arHoy\x1b[0m",
249		},
250		// Handle prefix and suffix properly
251		{
252			"foo\x1b[31mfoobarHoy\x1b[0mbaaar",
253			4,
254			"foo\x1b[31mf", "oobarHoy\x1b[0mbaaar",
255		},
256		// Cut properly with length = 0
257		{
258			"foo",
259			0,
260			"", "foo",
261		},
262		// Handle chinese
263		{
264			"快檢什麼望對",
265			4,
266			"快檢", "什麼望對",
267		},
268		{
269			"快檢什麼望對",
270			5,
271			"快檢", "什麼望對",
272		},
273		// Handle chinese with colors
274		{
275			"快\x1b[31m檢什麼\x1b[0m望對",
276			4,
277			"快\x1b[31m檢", "什麼\x1b[0m望對",
278		},
279	}
280
281	for i, tc := range cases {
282		result, leftover := splitWord(tc.Input, tc.Length)
283		if result != tc.Result || leftover != tc.Leftover {
284			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s` - `%s`\n\nActual Output:\n\n`%s` - `%s`",
285				i, tc.Input, tc.Result, tc.Leftover, result, leftover)
286		}
287	}
288}
289
290func TestExtractApplyTermEscapes(t *testing.T) {
291	cases := []struct {
292		Input       string
293		Output      string
294		TermEscapes []escapeItem
295	}{
296		// A plain ascii line with escapes.
297		{
298			"This \x1b[31mis an\x1b[0m example.",
299			"This is an example.",
300			[]escapeItem{{"\x1b[31m", 5}, {"\x1b[0m", 10}},
301		},
302		// A plain wide line with escapes.
303		{
304			"一只敏捷\x1b[31m的狐狸\x1b[0m跳过了一只懒狗。",
305			"一只敏捷的狐狸跳过了一只懒狗。",
306			[]escapeItem{{"\x1b[31m", 4}, {"\x1b[0m", 7}},
307		},
308		// A normal-wide mixed line with escapes.
309		{
310			"一只 A Quick 敏捷\x1b[31m的狐 Fox 狸\x1b[0m跳过了Dog一只懒狗。",
311			"一只 A Quick 敏捷的狐 Fox 狸跳过了Dog一只懒狗。",
312			[]escapeItem{{"\x1b[31m", 13}, {"\x1b[0m", 21}},
313		},
314	}
315
316	for i, tc := range cases {
317		line2, escapes := extractTermEscapes(tc.Input)
318		if line2 != tc.Output || !reflect.DeepEqual(escapes, tc.TermEscapes) {
319			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\nLine: `%s`\nEscapes: `%+v`\n\nActual Output:\n\nLine: `%s`\nEscapes: `%+v`\n\n",
320				i, tc.Input, tc.Output, tc.TermEscapes, line2, escapes)
321		}
322		line3 := applyTermEscapes(line2, escapes)
323		if line3 != tc.Input {
324			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Result:\n\n`%s`\n\nActual Result:\n\n`%s`\n\n",
325				i, tc.Input, tc.Input, line3)
326		}
327	}
328}
329
330func TestSegmentLines(t *testing.T) {
331	cases := []struct {
332		Input  string
333		Output []string
334	}{
335		// A plain ascii line with escapes.
336		{
337			"This is an example.",
338			[]string{"This", " ", "is", " ", "an", " ", "example."},
339		},
340		// A plain wide line with escapes.
341		{
342			"一只敏捷的狐狸跳过了一只懒狗。",
343			[]string{"一", "只", "敏", "捷", "的", "狐", "狸", "跳", "过",
344				"了", "一", "只", "懒", "狗", "。"},
345		},
346		// A complex stentence.
347		{
348			"This is a 'complex' example, where   一只 and English 混合了。",
349			[]string{"This", " ", "is", " ", "a", " ", "'complex'", " ", "example,",
350				" ", "where", "   ", "一", "只", " ", "and", " ", "English", " ", "混",
351				"合", "了", "。"},
352		},
353	}
354
355	for i, tc := range cases {
356		chunks := segmentLine(tc.Input)
357		if !reflect.DeepEqual(chunks, tc.Output) {
358			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`[%s]`\n\nActual Output:\n\n`[%s]`\n\n",
359				i, tc.Input, strings.Join(tc.Output, ", "), strings.Join(chunks, ", "))
360		}
361	}
362}