src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/cli/tk/listbox_window_test.go (about) 1 package tk 2 3 import ( 4 "testing" 5 6 "src.elv.sh/pkg/tt" 7 ) 8 9 func TestGetVerticalWindow(t *testing.T) { 10 tt.Test(t, getVerticalWindow, 11 // selected = 0: always show a widow starting from 0, regardless of 12 // the value of oldFirst 13 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 0, First: 0}, 6).Rets(0, 0), 14 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 0, First: 1}, 6).Rets(0, 0), 15 // selected < 0 is treated as if = 0. 16 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: -1, First: 0}, 6).Rets(0, 0), 17 // selected = n-1: always show a window ending at n-1, regardless of the 18 // value of oldFirst 19 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 9, First: 0}, 6).Rets(4, 0), 20 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 9, First: 8}, 6).Rets(4, 0), 21 // selected >= n is treated as if = n-1. 22 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 10, First: 0}, 6).Rets(4, 0), 23 // selected = 3, oldFirst = 2 (likely because previous selected = 4). 24 // Adjust first -> 1 to satisfy the upward respect distance of 2. 25 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 3, First: 2}, 6).Rets(1, 0), 26 // selected = 6, oldFirst = 2 (likely because previous selected = 7). 27 // Adjust first -> 3 to satisfy the downward respect distance of 2. 28 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 6, First: 2}, 6).Rets(3, 0), 29 30 // There is not enough budget to achieve respect distance on both sides. 31 // Split the budget in half. 32 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 3, First: 1}, 3).Rets(2, 0), 33 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 3, First: 0}, 3).Rets(2, 0), 34 35 // There is just enough distance to fit the selected item. Only show the 36 // selected item. 37 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 2, First: 0}, 1).Rets(2, 0), 38 ) 39 } 40 41 func TestGetHorizontalWindow(t *testing.T) { 42 tt.Test(t, getHorizontalWindow, 43 // All items fit in a single column. Item width is 6 ("item 0"). 44 Args(ListBoxState{Items: TestItems{NItems: 10}, Selected: 4, First: 0}, 0, 6, 10).Rets(0, 10), 45 // All items fit in multiple columns. Item width is 2 ("x0"). 46 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 10}, Selected: 4, First: 0}, 0, 6, 5).Rets(0, 5), 47 // All items cannot fit, selected = 0; show a window from 0. Height 48 // reduced to make room for scrollbar. 49 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 11}, Selected: 0, First: 0}, 0, 6, 5).Rets(0, 4), 50 // All items cannot fit. Columns are 0-3, 4-7, 8-10 (height reduced from 51 // 5 to 4 for scrollbar). Selecting last item, and showing last two 52 // columns; height reduced to make room for scrollbar. 53 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 11}, Selected: 10, First: 0}, 0, 7, 5).Rets(4, 4), 54 // Items are wider than terminal, and there is a single column. Show 55 // them all. 56 Args(ListBoxState{Items: TestItems{Prefix: "long prefix", NItems: 10}, Selected: 9, First: 0}, 0, 57 6, 10).Rets(0, 10), 58 // Items are wider than terminal, and there are multiple columns. Treat 59 // them as if each column occupies a full width. Columns are 0-4, 5-9. 60 Args(ListBoxState{Items: TestItems{Prefix: "long prefix", NItems: 10}, Selected: 9, First: 0}, 0, 61 6, 6).Rets(5, 5), 62 63 // The following cases only differ in State.First and shows that the 64 // algorithm respects it. In all cases, the columns are 0-4, 5-9, 65 // 10-14, 15-19, item 10 is selected, and the terminal can fit 2 columns. 66 67 // First = 0. Try to reach as far as possible to that, ending up showing 68 // columns 5-9 and 10-14. 69 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 20}, Selected: 10, First: 0}, 0, 8, 6).Rets(5, 5), 70 // First = 2. Ditto. 71 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 20}, Selected: 10, First: 2}, 0, 8, 6).Rets(5, 5), 72 // First = 5. Show columns 5-9 and 10-14. 73 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 20}, Selected: 10, First: 5}, 0, 8, 6).Rets(5, 5), 74 // First = 7. Ditto. 75 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 20}, Selected: 10, First: 7}, 0, 8, 6).Rets(5, 5), 76 // First = 10. No need to any columns to the left. 77 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 20}, Selected: 10, First: 10}, 0, 8, 6).Rets(10, 5), 78 // First = 12. Ditto. 79 Args(ListBoxState{Items: TestItems{Prefix: "x", NItems: 20}, Selected: 10, First: 12}, 0, 8, 6).Rets(10, 5), 80 ) 81 }