File: text_test.go

package info (click to toggle)
golang-github-gcla-gowid 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 4
file content (405 lines) | stat: -rw-r--r-- 14,911 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.

package text

import (
	"io"
	"strings"
	"testing"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/gwtest"
	log "github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
)

//======================================================================

var testl gowid.PaletteRef
var testl2 gowid.PaletteRef

func init() {
	testl = gowid.MakePaletteRef("test")
	testl2 = gowid.MakePaletteRef("test2")
}

func TestAdd1(t *testing.T) {
	m1 := StyledContent("hello world", testl)
	m2 := StyledContent("foobar", testl2)

	t1 := NewContent([]ContentSegment{m1})
	t1.AddAt(5, m2)
	assert.Equal(t, "hellofoobar world", t1.String())

	t1.DeleteAt(3, 7)
	assert.Equal(t, "helr world", t1.String())
}

func TestLayout1(t *testing.T) {
	tm1 := []ContentSegment{StyledContent("hello world", testl)}

	t1 := NewContent(tm1)
	l1 := MakeTextLayout(t1, 5, WrapClip, gowid.HAlignLeft{})

	assert.Equal(t, 1, len(l1.Lines))
	assert.Equal(t, LineLayout{StartWidth: 0, StartLength: 0, EndLength: 5, EndWidth: 5, Clipped: true}, l1.Lines[0])

	t2 := NewContent(tm1)
	l2 := MakeTextLayout(t2, 5, WrapAny, gowid.HAlignLeft{})

	assert.Equal(t, 3, len(l2.Lines))
	assert.Equal(t, LineLayout{StartWidth: 0, StartLength: 0, EndLength: 5, EndWidth: 5}, l2.Lines[0])
	assert.Equal(t, LineLayout{StartWidth: 5, StartLength: 5, EndLength: 10, EndWidth: 10}, l2.Lines[1])
	assert.Equal(t, LineLayout{StartWidth: 10, StartLength: 10, EndLength: 11, EndWidth: 11}, l2.Lines[2])
}

func TestLayoutW1(t *testing.T) {
	tm1 := []ContentSegment{StyledContent("hell现o world", testl)}

	t1 := NewContent(tm1)
	l1 := MakeTextLayout(t1, 5, WrapClip, gowid.HAlignLeft{})

	assert.Equal(t, 1, len(l1.Lines))
	assert.Equal(t, LineLayout{StartWidth: 0, StartLength: 0, EndLength: 4, EndWidth: 4, Clipped: true}, l1.Lines[0])

	t2 := NewContent(tm1)
	l2 := MakeTextLayout(t2, 5, WrapAny, gowid.HAlignLeft{})

	assert.Equal(t, 3, len(l2.Lines))
	assert.Equal(t, LineLayout{StartWidth: 0, StartLength: 0, EndLength: 4, EndWidth: 4}, l2.Lines[0])
	assert.Equal(t, LineLayout{StartWidth: 4, StartLength: 4, EndLength: 8, EndWidth: 9}, l2.Lines[1])
	assert.Equal(t, LineLayout{StartWidth: 9, StartLength: 8, EndLength: 12, EndWidth: 13}, l2.Lines[2])
}

func TestLayout2(t *testing.T) {
	tm1 := []ContentSegment{StyledContent("hello world", testl)}

	t1 := NewContent(tm1)
	l1 := MakeTextLayout(t1, 20, WrapClip, gowid.HAlignLeft{})

	assert.Equal(t, len(l1.Lines), 1)
	assert.Equal(t, l1.Lines[0], LineLayout{StartWidth: 0, StartLength: 0, EndLength: 11, EndWidth: 11})
}

func TestLayout3(t *testing.T) {
	tm1 := []ContentSegment{StyledContent("hello world\nsome more", testl)}

	t1 := NewContent(tm1)
	l1 := MakeTextLayout(t1, 7, WrapClip, gowid.HAlignLeft{})

	assert.Equal(t, len(l1.Lines), 2)
	assert.Equal(t, l1.Lines[0], LineLayout{StartWidth: 0, StartLength: 0, EndLength: 7, Clipped: true, EndWidth: 7})
	assert.Equal(t, l1.Lines[1], LineLayout{StartWidth: 12, StartLength: 12, EndLength: 19, Clipped: true, EndWidth: 19})

	t2 := NewContent(tm1)
	l2 := MakeTextLayout(t2, 7, WrapAny, gowid.HAlignLeft{})

	assert.Equal(t, len(l2.Lines), 4)
	assert.Equal(t, l2.Lines[0], LineLayout{StartWidth: 0, StartLength: 0, EndLength: 7, EndWidth: 7})
	assert.Equal(t, l2.Lines[1], LineLayout{StartWidth: 7, StartLength: 7, EndLength: 11, EndWidth: 11})
	assert.Equal(t, l2.Lines[2], LineLayout{StartWidth: 12, StartLength: 12, EndLength: 19, EndWidth: 19})
	assert.Equal(t, l2.Lines[3], LineLayout{StartWidth: 19, StartLength: 19, EndLength: 21, EndWidth: 21})
}

func TestLayout31(t *testing.T) {
	tm1 := []ContentSegment{StyledContent("he\nsome more", testl)}

	t1 := NewContent(tm1)
	l1 := MakeTextLayout(t1, 7, WrapClip, gowid.HAlignLeft{})

	assert.Equal(t, len(l1.Lines), 2)
	assert.Equal(t, l1.Lines[0], LineLayout{StartWidth: 0, StartLength: 0, EndLength: 2, EndWidth: 2})
	assert.Equal(t, l1.Lines[1], LineLayout{StartWidth: 3, StartLength: 3, EndLength: 10, Clipped: true, EndWidth: 10})
}

func TestLayout4(t *testing.T) {
	tm1 := []ContentSegment{StyledContent("hello world\nsome", testl)}

	t1 := NewContent(tm1)
	l1 := MakeTextLayout(t1, 7, WrapClip, gowid.HAlignLeft{})

	assert.Equal(t, len(l1.Lines), 2)
	assert.Equal(t, l1.Lines[0], LineLayout{StartWidth: 0, StartLength: 0, EndLength: 7, Clipped: true, EndWidth: 7})
	assert.Equal(t, l1.Lines[1], LineLayout{StartWidth: 12, StartLength: 12, EndLength: 16, EndWidth: 16})
}

func TestLayout5(t *testing.T) {
	tm1 := []ContentSegment{StyledContent("hello world\n\nsome", testl)}

	t1 := NewContent(tm1)
	l1 := MakeTextLayout(t1, 7, WrapClip, gowid.HAlignLeft{})

	assert.Equal(t, len(l1.Lines), 3)
	assert.Equal(t, l1.Lines[0], LineLayout{StartWidth: 0, StartLength: 0, EndLength: 7, Clipped: true, EndWidth: 7})
	assert.Equal(t, l1.Lines[1], LineLayout{StartWidth: 12, StartLength: 12, EndLength: 12, EndWidth: 12})
	assert.Equal(t, l1.Lines[2], LineLayout{StartWidth: 13, StartLength: 13, EndLength: 17, EndWidth: 17})

	t2 := NewContent(tm1)
	l2 := MakeTextLayout(t2, 7, WrapAny, gowid.HAlignLeft{})

	assert.Equal(t, len(l2.Lines), 4)
	assert.Equal(t, l2.Lines[0], LineLayout{StartWidth: 0, StartLength: 0, EndLength: 7, EndWidth: 7})
	assert.Equal(t, l2.Lines[1], LineLayout{StartWidth: 7, StartLength: 7, EndLength: 11, EndWidth: 11})
	assert.Equal(t, l2.Lines[2], LineLayout{StartWidth: 12, StartLength: 12, EndLength: 12, EndWidth: 12})
	assert.Equal(t, l2.Lines[3], LineLayout{StartWidth: 13, StartLength: 13, EndLength: 17, EndWidth: 17})
}

func TestLayout6(t *testing.T) {
	tm1 := []ContentSegment{StyledContent("hello world\n", testl)}

	t1 := NewContent(tm1)
	l1 := MakeTextLayout(t1, 7, WrapClip, gowid.HAlignLeft{})

	assert.Equal(t, 2, len(l1.Lines))
	assert.Equal(t, LineLayout{StartWidth: 0, StartLength: 0, EndLength: 7, Clipped: true, EndWidth: 7}, l1.Lines[0])
	assert.Equal(t, LineLayout{StartWidth: 12, StartLength: 12, EndLength: 12, Clipped: false, EndWidth: 12}, l1.Lines[1])
}

func TestMultiline1(t *testing.T) {
	msg := `hello
world
this
is
cool`
	widget1 := New(msg)
	canvas1 := widget1.Render(gowid.RenderBox{C: 16, R: 6}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget1 is %v", widget1)
	log.Infof("Canvas1 is %s", canvas1.String())
	res := strings.Join([]string{
		"hello           ",
		"world           ",
		"this            ",
		"is              ",
		"cool            ",
		"                ",
	}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas1(t *testing.T) {
	widget1 := New("hello world")
	canvas1 := widget1.Render(gowid.RenderBox{C: 20, R: 1}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget1 is %v", widget1)
	log.Infof("Canvas1 is %s", canvas1.String())
	res := strings.Join([]string{"hello world         "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas1b(t *testing.T) {
	widget1 := New("hello world this is a test")
	canvas1 := widget1.Render(gowid.RenderBox{C: 7, R: 3}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget1 is %v", widget1)
	log.Infof("Canvas1 is %s", canvas1.String())
	res := strings.Join([]string{"hello w", "orld th", "is is a"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas2(t *testing.T) {
	widget1 := New("hello world")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 7}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget2 is %v", widget1)
	log.Infof("Canvas2 is %s", canvas1.String())
	res := strings.Join([]string{"hello w", "orld   "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas3(t *testing.T) {
	widget1 := New("hello world")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 7}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget3 is %v", widget1)
	log.Infof("Canvas3 is %s", canvas1.String())
	res := strings.Join([]string{"hello w", "orld   "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas4(t *testing.T) {
	widget1 := New("hello world every day")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 7}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget4 is %v", widget1)
	log.Infof("Canvas4 is %s", canvas1.String())
	res := strings.Join([]string{"hello w", "orld ev", "ery day"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas5(t *testing.T) {
	widget1 := New("hello world every day")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 3}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget5 is %v", widget1)
	log.Infof("Canvas5 is %s", canvas1.String())
	res := strings.Join([]string{"hel", "lo ", "wor", "ld ", "eve", "ry ", "day"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas6(t *testing.T) {
	widget1 := New("hello transubstantiation")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 8}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget6 is %v", widget1)
	log.Infof("Canvas6 is %s", canvas1.String())
	res := strings.Join([]string{"hello tr", "ansubsta", "ntiation"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas6b(t *testing.T) {
	widget1 := New("hello transubstantiation good")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 8}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget6 is %v", widget1)
	log.Infof("Canvas6 is %s", canvas1.String())
	res := strings.Join([]string{"hello tr", "ansubsta", "ntiation", " good   "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas7(t *testing.T) {
	widget1 := New("hello transubstantiation good")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 11}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget7 is %v", widget1)
	log.Infof("Canvas7 is %s", canvas1.String())
	res := strings.Join([]string{"hello trans", "ubstantiati", "on good    "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas8(t *testing.T) {
	widget1 := New("hello transubstantiation boy")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 11}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget8 is %v", widget1)
	log.Infof("Canvas8 is %s", canvas1.String())
	res := strings.Join([]string{"hello trans", "ubstantiati", "on boy     "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas11(t *testing.T) {
	widget1 := New("hello transubstantiation good")
	//fwidget1 := NewFramed(widget1)
	canvas1 := widget1.Render(gowid.RenderBox{C: 5, R: 3}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget11 is %v", widget1)
	log.Infof("Canvas11 is %s", canvas1.String())
	res := strings.Join([]string{"hello", " tran", "subst"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas16(t *testing.T) {
	widget1 := New("line 1 line 2 line 3")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 8}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget16 is %v", widget1)
	log.Infof("Canvas16 is %s", canvas1.String())
	res := strings.Join([]string{"line 1 l", "ine 2 li", "ne 3    "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas24(t *testing.T) {
	widget1 := New("line 1line 2line 3")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 6}, gowid.NotSelected, gwtest.D)
	canvas1.Truncate(1, 0)
	log.Infof("Widget18 is %v", widget1)
	log.Infof("Canvas18 is %v", canvas1)
	log.Infof("Canvas18 is %s", canvas1.String())
	res := strings.Join([]string{"line 2", "line 3"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas25(t *testing.T) {
	widget1 := New("line 1line 2line 3")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 6}, gowid.NotSelected, gwtest.D)
	canvas1.Truncate(0, 2)
	log.Infof("Widget18 is %v", widget1)
	log.Infof("Canvas18 is %s", canvas1.String())
	res := strings.Join([]string{"line 1"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas27(t *testing.T) {
	widget1 := New("")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 0}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget is %v", widget1)
	log.Infof("Canvas is '%s'", canvas1.String())
	res := strings.Join([]string{""}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestCanvas28(t *testing.T) {
	widget1 := New("the needs of the many outweigh\nthe needs of the few.\nOr the one.", Options{
		Wrap: WrapClip,
	})
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 10}, gowid.NotSelected, gwtest.D)
	log.Infof("Widget is %v", widget1)
	log.Infof("Canvas is '%s'", canvas1.String())
	res := strings.Join([]string{"the needs ", "the needs ", "Or the one"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestTextAlign1(t *testing.T) {
	widget1 := New("hel你o\nworld\nf你o\nba")
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 7}, gowid.NotSelected, gwtest.D)
	res := strings.Join([]string{"hel你o ", "world  ", "f你o   ", "ba     "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestTextAlign2(t *testing.T) {
	widget1 := New("hello\nworld\nfoo\nba", Options{
		Align: gowid.HAlignRight{},
	})
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 7}, gowid.NotSelected, gwtest.D)
	res := strings.Join([]string{"  hello", "  world", "    foo", "     ba"}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestTextAlign3(t *testing.T) {
	widget1 := New("hello\nworld\nfoo\nba", Options{
		Align: gowid.HAlignMiddle{},
	})
	canvas1 := widget1.Render(gowid.RenderFlowWith{C: 7}, gowid.NotSelected, gwtest.D)
	res := strings.Join([]string{" hello ", " world ", "  foo  ", "  ba   "}, "\n")
	assert.Equal(t, res, canvas1.String())
}

func TestText1(t *testing.T) {
	w := New("hello world")
	c1 := w.Render(gowid.RenderFlowWith{C: 20}, gowid.Focused, gwtest.D)
	assert.Equal(t, c1.String(), "hello world         ")

	tset := false
	w.OnContentSet(gowid.WidgetCallback{"cb", func(app gowid.IApp, w gowid.IWidget) {
		tset = true
	}})

	_, err := io.Copy(&Writer{w, gwtest.D}, strings.NewReader("goodbye everyone"))
	assert.NoError(t, err)
	assert.Equal(t, tset, true)
	tset = false
	c2 := w.Render(gowid.RenderFlowWith{C: 20}, gowid.Focused, gwtest.D)
	assert.Equal(t, c2.String(), "goodbye everyone    ")

	_, err = io.Copy(&Writer{w, gwtest.D}, strings.NewReader("multi\nline\ntest"))
	assert.NoError(t, err)
	assert.Equal(t, tset, true)
	tset = false
	c3 := w.Render(gowid.RenderFlowWith{C: 10}, gowid.Focused, gwtest.D)
	assert.Equal(t, c3.String(), "multi     \nline      \ntest      ")
}

func TestText2(t *testing.T) {
	w := New("hello world")
	c1 := w.Render(gowid.RenderFixed{}, gowid.Focused, gwtest.D)
	assert.Equal(t, c1.String(), "hello world")
}

func TestText3(t *testing.T) {
	w := New("hello yo")

	// TODO - make 2nd last arg 0
	gwtest.RenderBoxManyTimes(t, w, 0, 10, 0, 10)
	gwtest.RenderFlowManyTimes(t, w, 0, 10)
}

func TestChinese1(t *testing.T) {
	w := New("|你|好|,|世|界|")
	c1 := w.Render(gowid.RenderFixed{}, gowid.Focused, gwtest.D)
	// Each width-2 rune takes up 2 screen cells
	assert.Equal(t, "|你|好|,|世|界|", c1.String())
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: