From fd746282599146198ce0896df834f4c37fc745fa Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sat, 15 Oct 2022 16:27:04 +0200 Subject: [PATCH] added unit test for descending order, fixed deduplication bug --- TODO.md | 2 -- lib/helpers.go | 8 ++++++-- lib/printer_test.go | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 7228d0c..fd04222 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,5 @@ ## Fixes to be implemented -- add unit test for descending sort order - ## Features to be implemented - sorting by: numerical, time, duration, string(default) diff --git a/lib/helpers.go b/lib/helpers.go index 35b47ea..1283292 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -23,6 +23,7 @@ import ( "github.com/gookit/color" "os" "regexp" + "sort" "strconv" "strings" ) @@ -71,8 +72,10 @@ func PrepareColumns(data *Tabdata) error { } } - // deduplicate - imap := make(map[int]int) + // deduplicate: put all values into a map (value gets map key) + // thereby removing duplicates, extract keys into new slice + // and sort it + imap := make(map[int]int, len(UseColumns)) for _, i := range UseColumns { imap[i] = 0 } @@ -80,6 +83,7 @@ func PrepareColumns(data *Tabdata) error { for k := range imap { UseColumns = append(UseColumns, k) } + sort.Ints(UseColumns) } return nil } diff --git a/lib/printer_test.go b/lib/printer_test.go index 342e2e9..7506e96 100644 --- a/lib/printer_test.go +++ b/lib/printer_test.go @@ -153,11 +153,13 @@ func TestSortPrinter(t *testing.T) { var tests = []struct { data Tabdata sortby int + desc bool expect string }{ { data: startdata, sortby: 1, + desc: false, expect: `ONE(1) TWO(2) THREE(3) abc 345 b1 bcd 234 a2 @@ -167,6 +169,7 @@ cde 123 c3`, { data: startdata, sortby: 2, + desc: false, expect: `ONE(1) TWO(2) THREE(3) cde 123 c3 bcd 234 a2 @@ -176,11 +179,21 @@ abc 345 b1`, { data: startdata, sortby: 3, + desc: false, expect: `ONE(1) TWO(2) THREE(3) bcd 234 a2 abc 345 b1 cde 123 c3`, }, + { + data: startdata, + sortby: 1, + desc: true, + expect: `ONE(1) TWO(2) THREE(3) +cde 123 c3 +bcd 234 a2 +abc 345 b1`, + }, } NoColor = true @@ -188,9 +201,10 @@ cde 123 c3`, origStdout, reader := stdout2pipe(t) for _, tt := range tests { - testname := fmt.Sprintf("print-sorted-table-%d", tt.sortby) + testname := fmt.Sprintf("print-sorted-table-by-%d-desc-%t", tt.sortby, tt.desc) t.Run(testname, func(t *testing.T) { SortByColumn = tt.sortby + SortDescending = tt.desc printData(&tt.data)