File: edit_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 (196 lines) | stat: -rw-r--r-- 6,329 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
// 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 edit

import (
	"io"
	"strings"
	"testing"
	"unicode/utf8"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/gwtest"
	tcell "github.com/gdamore/tcell/v2"
	"github.com/stretchr/testify/assert"
)

func evclick(x, y int) *tcell.EventMouse {
	return tcell.NewEventMouse(x, y, tcell.Button1, 0)
}

func evunclick(x, y int) *tcell.EventMouse {
	return tcell.NewEventMouse(x, y, tcell.ButtonNone, 0)
}

func TestType1(t *testing.T) {
	w := New(Options{Caption: "", Text: "hi: 现在 abc"})
	sz := gowid.RenderFlowWith{C: 15}
	c1 := w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "hi: 现在 abc   ", c1.String())

	evq := tcell.NewEventKey(tcell.KeyRune, 'q', tcell.ModNone)
	evdel := tcell.NewEventKey(tcell.KeyDelete, 'X', tcell.ModNone)

	w.SetCursorPos(0, gwtest.D)
	w.UserInput(evq, sz, gowid.Focused, gwtest.D)
	c1 = w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "qhi: 现在 abc  ", c1.String())

	w.SetCursorPos(6, gwtest.D)
	w.UserInput(evq, sz, gowid.Focused, gwtest.D)
	c1 = w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "qhi: 现q在 abc ", c1.String())
	w.UserInput(evq, sz, gowid.Focused, gwtest.D)
	c1 = w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "qhi: 现qq在 abc", c1.String())
	w.UserInput(evdel, sz, gowid.Focused, gwtest.D)
	c1 = w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "qhi: 现qq abc  ", c1.String())

	w.SetReadOnly(true, gwtest.D)
	w.UserInput(evq, sz, gowid.Focused, gwtest.D)
	c1 = w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "qhi: 现qq abc  ", c1.String())
	w.UserInput(evdel, sz, gowid.Focused, gwtest.D)
	c1 = w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "qhi: 现qq abc  ", c1.String())

	w.SetReadOnly(false, gwtest.D)
	w.UserInput(evq, sz, gowid.Focused, gwtest.D)
	c1 = w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "qhi: 现qqq abc ", c1.String())
}

func TestRender1(t *testing.T) {
	w := New(Options{Caption: "", Text: "abcde现fgh"})
	sz := gowid.RenderFlowWith{C: 6}
	c1 := w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "abcde \n现fgh ", c1.String())
}

func TestType2(t *testing.T) {
	w := New(Options{Caption: "", Text: "hi:  abc"})
	sz := gowid.RenderFlowWith{C: 15}
	c1 := w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "hi:  abc       ", c1.String())

	evq := tcell.NewEventKey(tcell.KeyRune, 'q', tcell.ModNone)

	w.SetCursorPos(0, gwtest.D)
	w.UserInput(evq, sz, gowid.Focused, gwtest.D)
	c1 = w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "qhi:  abc      ", c1.String())
}

func TestMove1(t *testing.T) {
	w := New(Options{Caption: "hi: ", Text: "now\n\nis the time"})
	sz := gowid.RenderFlowWith{C: 12}
	c1 := w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "hi: now     \n            \nis the time ", c1.String())

	w.SetCursorPos(0, gwtest.D)
	assert.Equal(t, 0, w.CursorPos())
	w.UserInput(gwtest.CursorDown(), sz, gowid.Focused, gwtest.D)
	assert.Equal(t, 4, w.CursorPos())
	w.UserInput(gwtest.CursorRight(), sz, gowid.Focused, gwtest.D)
	assert.Equal(t, 5, w.CursorPos())
	w.UserInput(gwtest.CursorLeft(), sz, gowid.Focused, gwtest.D)
	assert.Equal(t, 4, w.CursorPos())
	w.UserInput(gwtest.CursorDown(), sz, gowid.Focused, gwtest.D)
	assert.Equal(t, 5, w.CursorPos())
}

func TestLong1(t *testing.T) {
	w := New(Options{Caption: "现: ", Text: "现在是hetimeforallgoodmentocometotheaid\n\nofthe"})
	sz := gowid.RenderFlowWith{C: 12}
	c1 := w.Render(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, "现: 现在是he\ntimeforallgo\nodmentocomet\notheaid     \n            \nofthe       ", c1.String())

	clickat := func(x, y int) {
		w.UserInput(evclick(x, y), sz, gowid.Focused, gwtest.D)
		gwtest.D.SetLastMouseState(gowid.MouseState{true, false, false})
		w.UserInput(evunclick(x, y), sz, gowid.Focused, gwtest.D)
		gwtest.D.SetLastMouseState(gowid.MouseState{false, false, false})
	}

	clickat(4, 0)
	assert.Equal(t, 0, w.CursorPos())
	w.SetCursorPos(1, gwtest.D)
	assert.Equal(t, 1, w.CursorPos())
	x := utf8.RuneCountInString(w.Text())
	w.SetCursorPos(500, gwtest.D)
	assert.Equal(t, x, w.CursorPos())

	clickat(11, 0)
	assert.Equal(t, 4, w.CursorPos())

	clickat(0, 1)
	assert.Equal(t, 5, w.CursorPos())

	w.UserInput(gwtest.CursorLeft(), sz, gowid.Focused, gwtest.D)
	assert.Equal(t, 4, w.CursorPos())

	w.UserInput(gwtest.CursorDown(), sz, gowid.Focused, gwtest.D)
	assert.Equal(t, 16, w.CursorPos())
}

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

	tset := false
	w.OnTextSet(gowid.WidgetCallback{"cb", func(app gowid.IApp, w gowid.IWidget) {
		tset = true
	}})
	cset := false
	w.OnCaptionSet(gowid.WidgetCallback{"cb", func(app gowid.IApp, w gowid.IWidget) {
		cset = true
	}})
	pset := false
	w.OnCursorPosSet(gowid.WidgetCallback{"cb", func(app gowid.IApp, w gowid.IWidget) {
		pset = 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(sz, gowid.Focused, gwtest.D)
	assert.Equal(t, c2.String(), "hi: goodbye everyone")
	assert.Equal(t, w.CursorEnabled(), true)
	assert.Equal(t, c2.CursorEnabled(), true)

	_, 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(), "hi: multi \nline      \ntest      ")

	w.SetCaption("bye", gwtest.D)
	assert.Equal(t, w.Caption(), "bye")
	assert.Equal(t, cset, true)

	assert.Equal(t, w.CursorPos(), 0)
	w.SetCursorPos(1, gwtest.D)
	assert.Equal(t, w.CursorPos(), 1)
	assert.Equal(t, pset, true)
	x := len(w.Text())
	w.SetCursorPos(500, gwtest.D)
	assert.Equal(t, w.CursorPos(), x)

	// Make sure no crashes!
	for i := 0; i < 100; i++ {
		w.UserInput(gwtest.CursorDown(), gowid.RenderBox{C: 20, R: 5}, gowid.Focused, gwtest.D)
	}

}

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