fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Mildetest: cover internal packages (#535)fb46b4f

subvolume
3.1 KiB164 linesraw
1package testapp
2
3import (
4	"sync"
5	"testing"
6
7	"github.com/dundee/gdu/v5/internal/common"
8	"github.com/gdamore/tcell/v2"
9	"github.com/rivo/tview"
10	"github.com/stretchr/testify/assert"
11)
12
13// Compile-time check that MockedApp implements common.TermApplication
14var _ common.TermApplication = (*MockedApp)(nil)
15
16func TestCreateSimScreen(t *testing.T) {
17	screen := CreateSimScreen()
18
19	assert.NotNil(t, screen)
20}
21
22func TestCreateTestAppWithSimScreen(t *testing.T) {
23	app, screen := CreateTestAppWithSimScreen(120, 40)
24
25	assert.NotNil(t, app)
26	assert.NotNil(t, screen)
27
28	w, h := screen.Size()
29	assert.Equal(t, 120, w)
30	assert.Equal(t, 40, h)
31}
32
33func TestCreateMockedApp(t *testing.T) {
34	app := CreateMockedApp(false)
35
36	assert.NotNil(t, app)
37}
38
39func TestMockedAppRunSuccess(t *testing.T) {
40	app := CreateMockedApp(false)
41
42	err := app.Run()
43
44	assert.NoError(t, err)
45}
46
47func TestMockedAppRunFail(t *testing.T) {
48	app := CreateMockedApp(true)
49
50	err := app.Run()
51
52	assert.Error(t, err)
53	assert.Equal(t, "Fail", err.Error())
54}
55
56func TestMockedAppStop(t *testing.T) {
57	app := CreateMockedApp(false)
58
59	assert.NotPanics(t, func() {
60		app.Stop()
61	})
62}
63
64func TestMockedAppSuspend(t *testing.T) {
65	app := CreateMockedApp(false)
66
67	called := false
68	result := app.Suspend(func() {
69		called = true
70	})
71
72	assert.True(t, called)
73	assert.True(t, result)
74}
75
76func TestMockedAppSetRoot(t *testing.T) {
77	app := CreateMockedApp(false)
78
79	result := app.SetRoot(nil, true)
80
81	assert.Nil(t, result)
82}
83
84func TestMockedAppSetFocus(t *testing.T) {
85	app := CreateMockedApp(false)
86
87	result := app.SetFocus(nil)
88
89	assert.Nil(t, result)
90}
91
92func TestMockedAppSetInputCapture(t *testing.T) {
93	app := CreateMockedApp(false)
94
95	result := app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
96		return event
97	})
98
99	assert.Nil(t, result)
100}
101
102func TestMockedAppSetMouseCapture(t *testing.T) {
103	app := CreateMockedApp(false)
104
105	result := app.SetMouseCapture(
106		func(event *tcell.EventMouse, action tview.MouseAction) (*tcell.EventMouse, tview.MouseAction) {
107			return event, action
108		},
109	)
110
111	assert.Nil(t, result)
112}
113
114func TestMockedAppQueueUpdateDraw(t *testing.T) {
115	app := CreateMockedApp(false).(*MockedApp)
116
117	counter := 0
118	f1 := func() { counter++ }
119	f2 := func() { counter += 10 }
120
121	app.QueueUpdateDraw(f1)
122	app.QueueUpdateDraw(f2)
123
124	draws := app.GetUpdateDraws()
125	assert.Len(t, draws, 2)
126
127	// Execute the queued functions and verify they work
128	for _, f := range draws {
129		f()
130	}
131	assert.Equal(t, 11, counter)
132}
133
134func TestMockedAppSetBeforeDrawFunc(t *testing.T) {
135	app := CreateMockedApp(false).(*MockedApp)
136
137	assert.Empty(t, app.BeforeDraws)
138
139	app.SetBeforeDrawFunc(func(screen tcell.Screen) bool { return true })
140	assert.Len(t, app.BeforeDraws, 1)
141
142	app.SetBeforeDrawFunc(func(screen tcell.Screen) bool { return false })
143	assert.Len(t, app.BeforeDraws, 2)
144}
145
146func TestMockedAppConcurrentQueueUpdateDraw(t *testing.T) {
147	app := CreateMockedApp(false).(*MockedApp)
148
149	var wg sync.WaitGroup
150	n := 100
151	wg.Add(n)
152
153	for i := 0; i < n; i++ {
154		go func() {
155			defer wg.Done()
156			app.QueueUpdateDraw(func() {})
157		}()
158	}
159
160	wg.Wait()
161
162	draws := app.GetUpdateDraws()
163	assert.Len(t, draws, n)
164}