fork/gdu

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

bingoohuangfeat: add --archive-browsing flag to show zip/jar files as dirs, add --collapse-path flag to for collapsing single-child directory chains (#434)516c421

subvolume
2.5 KiB105 linesraw
1package testapp
2
3import (
4	"errors"
5	"sync"
6
7	"github.com/dundee/gdu/v5/internal/common"
8	"github.com/gdamore/tcell/v2"
9	"github.com/rivo/tview"
10)
11
12// CreateSimScreen returns tcell.SimulationScreen
13func CreateSimScreen() tcell.SimulationScreen {
14	screen := tcell.NewSimulationScreen("UTF-8")
15	return screen
16}
17
18// CreateTestAppWithSimScreen returns app with simulation screen for tests
19func CreateTestAppWithSimScreen(width, height int) (app *tview.Application, screen tcell.SimulationScreen) {
20	app = tview.NewApplication()
21	screen = CreateSimScreen()
22	app.SetScreen(screen)
23	screen.SetSize(width, height)
24	return app, screen
25}
26
27// MockedApp is tview.Application with mocked methods
28type MockedApp struct {
29	mutex       *sync.Mutex
30	updateDraws []func()
31	BeforeDraws []func(screen tcell.Screen) bool
32	FailRun     bool
33}
34
35// CreateMockedApp returns app with simulation screen for tests
36func CreateMockedApp(failRun bool) common.TermApplication {
37	app := &MockedApp{
38		FailRun:     failRun,
39		updateDraws: make([]func(), 0, 1),
40		BeforeDraws: make([]func(screen tcell.Screen) bool, 0, 1),
41		mutex:       &sync.Mutex{},
42	}
43	return app
44}
45
46// Run does nothing
47func (app *MockedApp) Run() error {
48	if app.FailRun {
49		return errors.New("Fail")
50	}
51
52	return nil
53}
54
55// Stop does nothing
56func (app *MockedApp) Stop() {}
57
58// Suspend runs given function
59func (app *MockedApp) Suspend(f func()) bool {
60	f()
61	return true
62}
63
64// SetRoot does nothing
65func (app *MockedApp) SetRoot(root tview.Primitive, fullscreen bool) *tview.Application {
66	return nil
67}
68
69// SetFocus does nothing
70func (app *MockedApp) SetFocus(p tview.Primitive) *tview.Application {
71	return nil
72}
73
74// SetInputCapture does nothing
75func (app *MockedApp) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *tview.Application {
76	return nil
77}
78
79// SetMouseCapture does nothing
80func (app *MockedApp) SetMouseCapture(
81	capture func(event *tcell.EventMouse, action tview.MouseAction) (*tcell.EventMouse, tview.MouseAction),
82) *tview.Application {
83	return nil
84}
85
86// QueueUpdateDraw does nothing
87func (app *MockedApp) QueueUpdateDraw(f func()) *tview.Application {
88	app.mutex.Lock()
89	app.updateDraws = append(app.updateDraws, f)
90	app.mutex.Unlock()
91	return nil
92}
93
94// QueueUpdateDraw does nothing
95func (app *MockedApp) GetUpdateDraws() []func() {
96	app.mutex.Lock()
97	defer app.mutex.Unlock()
98	return app.updateDraws
99}
100
101// SetBeforeDrawFunc does nothing
102func (app *MockedApp) SetBeforeDrawFunc(f func(screen tcell.Screen) bool) *tview.Application {
103	app.BeforeDraws = append(app.BeforeDraws, f)
104	return nil
105}