-
Notifications
You must be signed in to change notification settings - Fork 38
/
term_test.go
128 lines (112 loc) · 2.41 KB
/
term_test.go
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
package terminal
import (
"errors"
"fmt"
"testing"
"time"
"fyne.io/fyne/v2"
_ "fyne.io/fyne/v2/test"
"github.com/stretchr/testify/assert"
)
func TestNewTerminal(t *testing.T) {
term := New()
assert.NotNil(t, term)
assert.NotNil(t, term.content)
}
func TestExitCode(t *testing.T) {
term := New()
assert.Equal(t, term.ExitCode(), int(-1))
}
func testExitCodeN(t *testing.T, n int) {
term := New()
term.Resize(fyne.NewSize(45, 45))
go term.RunLocalShell()
err := errors.New("NotYet")
for err != nil {
time.Sleep(50 * time.Millisecond)
_, err = term.Write([]byte(fmt.Sprintf("exit %d\n", n)))
}
for term.ExitCode() == -1 {
time.Sleep(50 * time.Millisecond)
}
assert.Equal(t, n, term.ExitCode())
}
func TestExitCode01(t *testing.T) {
testExitCodeN(t, 0)
testExitCodeN(t, 1)
}
func TestTerminal_Resize(t *testing.T) {
term := New()
term.Resize(fyne.NewSize(45, 45))
assert.Equal(t, uint(5), term.config.Columns)
assert.Equal(t, uint(2), term.config.Rows)
}
func TestTerminal_AddListener(t *testing.T) {
term := New()
listen := make(chan Config, 1)
term.AddListener(listen)
assert.Equal(t, 1, len(term.listeners))
go term.onConfigure()
select {
case <-listen: // passed
case <-time.After(time.Millisecond * 100):
t.Error("Failed waiting for configure callback")
}
term.RemoveListener(listen)
assert.Equal(t, 0, len(term.listeners))
}
func TestTerminal_SanitizePosition(t *testing.T) {
tests := []struct {
name string
pos fyne.Position
width int
height int
want fyne.Position
}{
{
name: "WithinBounds",
pos: fyne.NewPos(2, 3),
width: 45,
height: 45,
want: fyne.NewPos(2, 3),
},
{
name: "NegativeX",
pos: fyne.NewPos(-1, 2),
width: 45,
height: 45,
want: fyne.NewPos(0, 2),
},
{
name: "ExceedsWidth",
pos: fyne.NewPos(46, 2),
width: 45,
height: 45,
want: fyne.NewPos(45, 2),
},
{
name: "NegativeY",
pos: fyne.NewPos(3, -1),
width: 45,
height: 45,
want: fyne.NewPos(3, 0),
},
{
name: "ExceedsHeight",
pos: fyne.NewPos(3, 46),
width: 45,
height: 45,
want: fyne.NewPos(3, 45),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
term := New()
term.Resize(fyne.NewSize(float32(tt.width), float32(tt.height)))
got := term.sanitizePosition(tt.pos)
if *got != tt.want {
t.Errorf("got %v, want %v", *got, tt.want)
}
})
}
}