fork/gdu
git clone https://git.julia.blue/fork/gdu
272c209
master
1package tui 2 3import ( 4"bytes" 5"testing" 6 7"github.com/dundee/gdu/v5/internal/testanalyze" 8"github.com/dundee/gdu/v5/internal/testapp" 9"github.com/stretchr/testify/assert" 10) 11 12func TestAnalyzeByApparentSize (t * testing.T ) { 13ui := getAnalyzedPathWithSorting ("size" ,"desc" ,true ) 14 15assert .Equal (t ,4 ,ui .table .GetRowCount ()) 16assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"ddd" ) 17assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"ccc" ) 18assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"bbb" ) 19assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"aaa" ) 20} 21 22func TestSortByApparentSizeAsc (t * testing.T ) { 23ui := getAnalyzedPathWithSorting ("size" ,"asc" ,true ) 24 25assert .Equal (t ,4 ,ui .table .GetRowCount ()) 26assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"aaa" ) 27assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"bbb" ) 28assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"ccc" ) 29assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"ddd" ) 30} 31 32func TestAnalyzeBySize (t * testing.T ) { 33ui := getAnalyzedPathWithSorting ("size" ,"desc" ,false ) 34 35assert .Equal (t ,4 ,ui .table .GetRowCount ()) 36assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"ddd" ) 37assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"ccc" ) 38assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"bbb" ) 39assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"aaa" ) 40} 41 42func TestSortBySizeAsc (t * testing.T ) { 43ui := getAnalyzedPathWithSorting ("size" ,"asc" ,false ) 44 45assert .Equal (t ,4 ,ui .table .GetRowCount ()) 46assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"aaa" ) 47assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"bbb" ) 48assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"ccc" ) 49assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"ddd" ) 50} 51 52func TestAnalyzeByName (t * testing.T ) { 53ui := getAnalyzedPathWithSorting ("name" ,"desc" ,false ) 54 55assert .Equal (t ,4 ,ui .table .GetRowCount ()) 56assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"ddd" ) 57assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"ccc" ) 58assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"bbb" ) 59assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"aaa" ) 60} 61 62func TestAnalyzeByNameAsc (t * testing.T ) { 63ui := getAnalyzedPathWithSorting ("name" ,"asc" ,false ) 64 65assert .Equal (t ,4 ,ui .table .GetRowCount ()) 66assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"aaa" ) 67assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"bbb" ) 68assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"ccc" ) 69assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"ddd" ) 70} 71 72func TestAnalyzeByItemCount (t * testing.T ) { 73ui := getAnalyzedPathWithSorting ("itemCount" ,"desc" ,false ) 74 75assert .Equal (t ,4 ,ui .table .GetRowCount ()) 76assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"ddd" ) 77assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"ccc" ) 78assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"bbb" ) 79assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"aaa" ) 80} 81 82func TestAnalyzeByItemCountAsc (t * testing.T ) { 83ui := getAnalyzedPathWithSorting ("itemCount" ,"asc" ,false ) 84 85assert .Equal (t ,4 ,ui .table .GetRowCount ()) 86assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"aaa" ) 87assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"bbb" ) 88assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"ccc" ) 89assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"ddd" ) 90} 91 92func TestAnalyzeByMtime (t * testing.T ) { 93ui := getAnalyzedPathWithSorting ("mtime" ,"desc" ,false ) 94 95assert .Equal (t ,4 ,ui .table .GetRowCount ()) 96assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"aaa" ) 97assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"bbb" ) 98assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"ccc" ) 99assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"ddd" ) 100} 101 102func TestAnalyzeByMtimeAsc (t * testing.T ) { 103ui := getAnalyzedPathWithSorting ("mtime" ,"asc" ,false ) 104 105assert .Equal (t ,4 ,ui .table .GetRowCount ()) 106assert .Contains (t ,ui .table .GetCell (0 ,0 ).Text ,"ddd" ) 107assert .Contains (t ,ui .table .GetCell (1 ,0 ).Text ,"ccc" ) 108assert .Contains (t ,ui .table .GetCell (2 ,0 ).Text ,"bbb" ) 109assert .Contains (t ,ui .table .GetCell (3 ,0 ).Text ,"aaa" ) 110} 111 112func TestSetSorting (t * testing.T ) { 113ui := getAnalyzedPathWithSorting ("itemCount" ,"asc" ,false ) 114 115ui .setSorting ("name" ) 116assert .Equal (t ,"name" ,ui .sortBy ) 117assert .Equal (t ,"asc" ,ui .sortOrder ) 118ui .setSorting ("name" ) 119assert .Equal (t ,"name" ,ui .sortBy ) 120assert .Equal (t ,"desc" ,ui .sortOrder ) 121ui .setSorting ("name" ) 122assert .Equal (t ,"name" ,ui .sortBy ) 123assert .Equal (t ,"asc" ,ui .sortOrder ) 124} 125 126func TestSetDEfaultSorting (t * testing.T ) { 127simScreen := testapp .CreateSimScreen () 128defer simScreen .Fini () 129 130var opts []Option 131opts = append (opts ,func (ui * UI ) { 132ui .SetDefaultSorting ("name" ,"asc" ) 133 }) 134 135app := testapp .CreateMockedApp (true ) 136ui := CreateUI (app ,simScreen ,& bytes.Buffer {},false ,false ,false ,false ,opts ... ) 137ui .Analyzer = & testanalyze.MockedAnalyzer {} 138ui .done = make (chan struct {}) 139 140if err := ui .AnalyzePath ("test_dir" ,nil );err != nil { 141panic (err ) 142 } 143 144<- ui .done // wait for analyzer 145 146for _ ,f := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 147f () 148 } 149 150assert .Equal (t ,"name" ,ui .sortBy ) 151assert .Equal (t ,"asc" ,ui .sortOrder ) 152} 153 154func TestSortDevicesByName (t * testing.T ) { 155app ,simScreen := testapp .CreateTestAppWithSimScreen (50 ,50 ) 156defer simScreen .Fini () 157 158ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,true ,false ) 159err := ui .ListDevices (getDevicesInfoMock ()) 160 161assert .Nil (t ,err ) 162 163ui .setSorting ("name" )// sort by name asc 164assert .Equal (t ,"/dev/boot" ,ui .devices [0 ].Name ) 165 166ui .setSorting ("name" )// sort by name desc 167assert .Equal (t ,"/dev/root" ,ui .devices [0 ].Name ) 168} 169 170func TestSortDevicesByUsedSize (t * testing.T ) { 171app ,simScreen := testapp .CreateTestAppWithSimScreen (50 ,50 ) 172defer simScreen .Fini () 173 174ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,true ,false ) 175err := ui .ListDevices (getDevicesInfoMock ()) 176 177assert .Nil (t ,err ) 178 179ui .setSorting ("size" )// sort by used size asc 180assert .Equal (t ,"/dev/boot" ,ui .devices [0 ].Name ) 181 182ui .setSorting ("size" )// sort by used size desc 183assert .Equal (t ,"/dev/root" ,ui .devices [0 ].Name ) 184} 185 186func getAnalyzedPathWithSorting (sortBy string ,sortOrder string ,apparentSize bool )* UI { 187simScreen := testapp .CreateSimScreen () 188defer simScreen .Fini () 189 190app := testapp .CreateMockedApp (true ) 191ui := CreateUI (app ,simScreen ,& bytes.Buffer {},false ,apparentSize ,false ,false ) 192ui .Analyzer = & testanalyze.MockedAnalyzer {} 193ui .done = make (chan struct {}) 194ui .sortBy = sortBy 195ui .sortOrder = sortOrder 196if err := ui .AnalyzePath ("test_dir" ,nil );err != nil { 197panic (err ) 198 } 199 200<- ui .done // wait for analyzer 201 202for _ ,f := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 203f () 204 } 205 206return ui 207}