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