-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sorting by name, item count and size
- Loading branch information
Daniel Milde
committed
Jan 1, 2021
1 parent
9336457
commit 699affc
Showing
4 changed files
with
337 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package analyze | ||
|
||
func (f Files) Len() int { return len(f) } | ||
func (f Files) Swap(i, j int) { f[i], f[j] = f[j], f[i] } | ||
func (f Files) Less(i, j int) bool { return f[i].Size > f[j].Size } | ||
|
||
// ByItemCount sorts files by item count | ||
type ByItemCount Files | ||
|
||
func (f ByItemCount) Len() int { return len(f) } | ||
func (f ByItemCount) Swap(i, j int) { f[i], f[j] = f[j], f[i] } | ||
func (f ByItemCount) Less(i, j int) bool { return f[i].ItemCount > f[j].ItemCount } | ||
|
||
// ByName sorts files by name | ||
type ByName Files | ||
|
||
func (f ByName) Len() int { return len(f) } | ||
func (f ByName) Swap(i, j int) { f[i], f[j] = f[j], f[i] } | ||
func (f ByName) Less(i, j int) bool { return f[i].Name > f[j].Name } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package analyze | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSortBySize(t *testing.T) { | ||
files := Files{ | ||
&File{ | ||
Size: 1, | ||
}, | ||
&File{ | ||
Size: 2, | ||
}, | ||
&File{ | ||
Size: 3, | ||
}, | ||
} | ||
|
||
sort.Sort(files) | ||
|
||
assert.Equal(t, int64(3), files[0].Size) | ||
assert.Equal(t, int64(2), files[1].Size) | ||
assert.Equal(t, int64(1), files[2].Size) | ||
} | ||
|
||
func TestSortBySizeAsc(t *testing.T) { | ||
files := Files{ | ||
&File{ | ||
Size: 1, | ||
}, | ||
&File{ | ||
Size: 2, | ||
}, | ||
&File{ | ||
Size: 3, | ||
}, | ||
} | ||
|
||
sort.Sort(sort.Reverse(files)) | ||
|
||
assert.Equal(t, int64(1), files[0].Size) | ||
assert.Equal(t, int64(2), files[1].Size) | ||
assert.Equal(t, int64(3), files[2].Size) | ||
} | ||
|
||
func TestSortByItemCount(t *testing.T) { | ||
files := Files{ | ||
&File{ | ||
ItemCount: 1, | ||
}, | ||
&File{ | ||
ItemCount: 2, | ||
}, | ||
&File{ | ||
ItemCount: 3, | ||
}, | ||
} | ||
|
||
sort.Sort(ByItemCount(files)) | ||
|
||
assert.Equal(t, 3, files[0].ItemCount) | ||
assert.Equal(t, 2, files[1].ItemCount) | ||
assert.Equal(t, 1, files[2].ItemCount) | ||
} | ||
|
||
func TestSortByName(t *testing.T) { | ||
files := Files{ | ||
&File{ | ||
Name: "aa", | ||
}, | ||
&File{ | ||
Name: "bb", | ||
}, | ||
&File{ | ||
Name: "cc", | ||
}, | ||
} | ||
|
||
sort.Sort(ByName(files)) | ||
|
||
assert.Equal(t, "cc", files[0].Name) | ||
assert.Equal(t, "bb", files[1].Name) | ||
assert.Equal(t, "aa", files[2].Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/dundee/gdu/analyze" | ||
"github.com/gdamore/tcell/v2" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSortBySizeAsc(t *testing.T) { | ||
fin := analyze.CreateTestDir() | ||
defer fin() | ||
|
||
simScreen := tcell.NewSimulationScreen("UTF-8") | ||
simScreen.Init() | ||
simScreen.SetSize(50, 50) | ||
|
||
ui := CreateUI(simScreen) | ||
|
||
ui.AnalyzePath("test_dir") | ||
|
||
go func() { | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'j', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'l', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 's', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'q', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
}() | ||
|
||
ui.StartUILoop() | ||
|
||
dir := ui.currentDir | ||
|
||
assert.Equal(t, "file2", dir.Files[0].Name) | ||
} | ||
|
||
func TestSortByName(t *testing.T) { | ||
fin := analyze.CreateTestDir() | ||
defer fin() | ||
|
||
simScreen := tcell.NewSimulationScreen("UTF-8") | ||
simScreen.Init() | ||
simScreen.SetSize(50, 50) | ||
|
||
ui := CreateUI(simScreen) | ||
|
||
ui.AnalyzePath("test_dir") | ||
|
||
go func() { | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'j', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'l', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'n', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'q', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
}() | ||
|
||
ui.StartUILoop() | ||
|
||
dir := ui.currentDir | ||
|
||
assert.Equal(t, "file2", dir.Files[0].Name) | ||
} | ||
|
||
func TestSortByNameDesc(t *testing.T) { | ||
fin := analyze.CreateTestDir() | ||
defer fin() | ||
|
||
simScreen := tcell.NewSimulationScreen("UTF-8") | ||
simScreen.Init() | ||
simScreen.SetSize(50, 50) | ||
|
||
ui := CreateUI(simScreen) | ||
|
||
ui.AnalyzePath("test_dir") | ||
|
||
go func() { | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'j', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'l', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'n', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'n', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'q', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
}() | ||
|
||
ui.StartUILoop() | ||
|
||
dir := ui.currentDir | ||
|
||
assert.Equal(t, "subnested", dir.Files[0].Name) | ||
} | ||
|
||
func TestSortByItemCount(t *testing.T) { | ||
fin := analyze.CreateTestDir() | ||
defer fin() | ||
|
||
simScreen := tcell.NewSimulationScreen("UTF-8") | ||
simScreen.Init() | ||
simScreen.SetSize(50, 50) | ||
|
||
ui := CreateUI(simScreen) | ||
|
||
ui.AnalyzePath("test_dir") | ||
|
||
go func() { | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'j', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'l', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'c', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'q', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
}() | ||
|
||
ui.StartUILoop() | ||
|
||
dir := ui.currentDir | ||
|
||
assert.Equal(t, "file2", dir.Files[0].Name) | ||
} | ||
|
||
func TestSortByItemCountDesc(t *testing.T) { | ||
// fin := analyze.CreateTestDir() | ||
// defer fin() | ||
analyze.CreateTestDir() | ||
|
||
simScreen := tcell.NewSimulationScreen("UTF-8") | ||
simScreen.Init() | ||
simScreen.SetSize(50, 50) | ||
|
||
ui := CreateUI(simScreen) | ||
|
||
ui.AnalyzePath("test_dir") | ||
|
||
go func() { | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'j', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'l', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'c', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'c', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
simScreen.InjectKey(tcell.KeyRune, 'q', 1) | ||
time.Sleep(100 * time.Millisecond) | ||
}() | ||
|
||
ui.StartUILoop() | ||
|
||
dir := ui.currentDir | ||
|
||
assert.Equal(t, "subnested", dir.Files[0].Name) | ||
} |