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		// Handle long unbreakable words in a full stentence
161		{
162			"OT: there are alternatives to maintainer-/user-set priority, e.g. \"[user pain](http://www.lostgarden.com/2008/05/improving-bug-triage-with-user-pain.html)\".",
163			`    OT: there are alternatives to maintainer-/user-set
164    priority, e.g. "[user pain](http://www.lostgarden.com/
165    2008/05/improving-bug-triage-with-user-pain.html)".`,
166			58, 4,
167		},
168	}
169
170	for i, tc := range cases {
171		actual, lines := WrapLeftPadded(tc.input, tc.lim, tc.pad)
172		if actual != tc.output {
173			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n`\n%s`\n\nActual Output:\n`\n%s\n%s`",
174				i, tc.input, tc.output,
175				"|"+strings.Repeat("-", tc.lim-2)+"|",
176				actual)
177		}
178
179		expected := len(strings.Split(tc.output, "\n"))
180		if expected != lines {
181			t.Fatalf("Case %d Nb lines mismatch\nExpected:%d\nActual:%d",
182				i, expected, lines)
183		}
184	}
185}
186
187func TestWordLen(t *testing.T) {
188	cases := []struct {
189		Input  string
190		Length int
191	}{
192		// A simple word
193		{
194			"foo",
195			3,
196		},
197		// A simple word with colors
198		{
199			"\x1b[31mbar\x1b[0m",
200			3,
201		},
202		// Handle prefix and suffix properly
203		{
204			"foo\x1b[31mfoobarHoy\x1b[0mbaaar",
205			17,
206		},
207		// Handle chinese
208		{
209			"快檢什麼望對",
210			12,
211		},
212		// Handle chinese with colors
213		{
214			"快\x1b[31m檢什麼\x1b[0m望對",
215			12,
216		},
217	}
218
219	for i, tc := range cases {
220		l := wordLen(tc.Input)
221		if l != tc.Length {
222			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%d`\n\nActual Output:\n\n`%d`",
223				i, tc.Input, tc.Length, l)
224		}
225	}
226}
227
228func TestSplitWord(t *testing.T) {
229	cases := []struct {
230		Input            string
231		Length           int
232		Result, Leftover string
233	}{
234		// A simple word passes through.
235		{
236			"foo",
237			4,
238			"foo", "",
239		},
240		// Cut at the right place
241		{
242			"foobarHoy",
243			4,
244			"foob", "arHoy",
245		},
246		// A simple word passes through with colors
247		{
248			"\x1b[31mbar\x1b[0m",
249			4,
250			"\x1b[31mbar\x1b[0m", "",
251		},
252		// Cut at the right place with colors
253		{
254			"\x1b[31mfoobarHoy\x1b[0m",
255			4,
256			"\x1b[31mfoob", "arHoy\x1b[0m",
257		},
258		// Handle prefix and suffix properly
259		{
260			"foo\x1b[31mfoobarHoy\x1b[0mbaaar",
261			4,
262			"foo\x1b[31mf", "oobarHoy\x1b[0mbaaar",
263		},
264		// Cut properly with length = 0
265		{
266			"foo",
267			0,
268			"", "foo",
269		},
270		// Handle chinese
271		{
272			"快檢什麼望對",
273			4,
274			"快檢", "什麼望對",
275		},
276		{
277			"快檢什麼望對",
278			5,
279			"快檢", "什麼望對",
280		},
281		// Handle chinese with colors
282		{
283			"快\x1b[31m檢什麼\x1b[0m望對",
284			4,
285			"快\x1b[31m檢", "什麼\x1b[0m望對",
286		},
287	}
288
289	for i, tc := range cases {
290		result, leftover := splitWord(tc.Input, tc.Length)
291		if result != tc.Result || leftover != tc.Leftover {
292			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s` - `%s`\n\nActual Output:\n\n`%s` - `%s`",
293				i, tc.Input, tc.Result, tc.Leftover, result, leftover)
294		}
295	}
296}
297
298func TestExtractApplyTermEscapes(t *testing.T) {
299	cases := []struct {
300		Input       string
301		Output      string
302		TermEscapes []escapeItem
303	}{
304		// A plain ascii line with escapes.
305		{
306			"This \x1b[31mis an\x1b[0m example.",
307			"This is an example.",
308			[]escapeItem{{"\x1b[31m", 5}, {"\x1b[0m", 10}},
309		},
310		// Escape at the end
311		{
312			"This \x1b[31mis an example.\x1b[0m",
313			"This is an example.",
314			[]escapeItem{{"\x1b[31m", 5}, {"\x1b[0m", 19}},
315		},
316		// A plain wide line with escapes.
317		{
318			"一只敏捷\x1b[31m的狐狸\x1b[0m跳过了一只懒狗。",
319			"一只敏捷的狐狸跳过了一只懒狗。",
320			[]escapeItem{{"\x1b[31m", 4}, {"\x1b[0m", 7}},
321		},
322		// A normal-wide mixed line with escapes.
323		{
324			"一只 A Quick 敏捷\x1b[31m的狐 Fox 狸\x1b[0m跳过了Dog一只懒狗。",
325			"一只 A Quick 敏捷的狐 Fox 狸跳过了Dog一只懒狗。",
326			[]escapeItem{{"\x1b[31m", 13}, {"\x1b[0m", 21}},
327		},
328	}
329
330	for i, tc := range cases {
331		line2, escapes := extractTermEscapes(tc.Input)
332		if line2 != tc.Output || !reflect.DeepEqual(escapes, tc.TermEscapes) {
333			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",
334				i, tc.Input, tc.Output, tc.TermEscapes, line2, escapes)
335		}
336		line3 := applyTermEscapes(line2, escapes)
337		if line3 != tc.Input {
338			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Result:\n\n`%s`\n\nActual Result:\n\n`%s`\n\n",
339				i, tc.Input, tc.Input, line3)
340		}
341	}
342}
343
344func TestSegmentLines(t *testing.T) {
345	cases := []struct {
346		Input  string
347		Output []string
348	}{
349		// A plain ascii line with escapes.
350		{
351			"This is an example.",
352			[]string{"This", " ", "is", " ", "an", " ", "example."},
353		},
354		// A plain wide line with escapes.
355		{
356			"一只敏捷的狐狸跳过了一只懒狗。",
357			[]string{"一", "只", "敏", "捷", "的", "狐", "狸", "跳", "过",
358				"了", "一", "只", "懒", "狗", "。"},
359		},
360		// A complex stentence.
361		{
362			"This is a 'complex' example, where   一只 and English 混合了。",
363			[]string{"This", " ", "is", " ", "a", " ", "'complex'", " ", "example,",
364				" ", "where", "   ", "一", "只", " ", "and", " ", "English", " ", "混",
365				"合", "了", "。"},
366		},
367	}
368
369	for i, tc := range cases {
370		chunks := segmentLine(tc.Input)
371		if !reflect.DeepEqual(chunks, tc.Output) {
372			t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`[%s]`\n\nActual Output:\n\n`[%s]`\n\n",
373				i, tc.Input, strings.Join(tc.Output, ", "), strings.Join(chunks, ", "))
374		}
375	}
376}