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