result_test.go
This file
Path
src/result_test.go
299 lines · 10.3 KiB · Go
Last changed by
Fix --tiebreak=pathname with non-ASCII textJunegunn Choibefore adoptionb395cfb
History of this fileTake it
The walk stopped after 200 commits: lines older than 76efddd point at it.
bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd
lines 1–5why, one step back1package fzf23import (4 "math"5 "math/rand"Replace sort.Sort(ByOrder(...)) with slices.SortFuncJunegunn Choibefore adoptionbd4a18d
line 6why, one step back6 "slices"bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd
lines 7–22why, one step back7 "sort"8 "testing"910 "github.com/junegunn/fzf/src/tui"11 "github.com/junegunn/fzf/src/util"12)1314func withIndex(i *Item, index int) *Item {15 (*i).text.Index = int32(index)16 return i17}1819func TestOffsetSort(t *testing.T) {20 offsets := []Offset{21 {3, 5}, {2, 7},22 {1, 3}, {2, 9}}Replace sort.Sort(ByOrder(...)) with slices.SortFuncJunegunn Choibefore adoptionbd4a18d
line 23why, one step back23 slices.SortFunc(offsets, compareOffsets)bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd
lines 24–274why, one step back2425 if offsets[0][0] != 1 || offsets[0][1] != 3 ||26 offsets[1][0] != 2 || offsets[1][1] != 7 ||27 offsets[2][0] != 2 || offsets[2][1] != 9 ||28 offsets[3][0] != 3 || offsets[3][1] != 5 {29 t.Error("Invalid order:", offsets)30 }31}3233func TestRankComparison(t *testing.T) {34 rank := func(vals ...uint16) Result {35 return Result{36 points: [4]uint16{vals[0], vals[1], vals[2], vals[3]},37 item: &Item{text: util.Chars{Index: int32(vals[4])}}}38 }39 if compareRanks(rank(3, 0, 0, 0, 5), rank(2, 0, 0, 0, 7), false) ||40 !compareRanks(rank(3, 0, 0, 0, 5), rank(3, 0, 0, 0, 6), false) ||41 !compareRanks(rank(1, 2, 0, 0, 3), rank(1, 3, 0, 0, 2), false) ||42 !compareRanks(rank(0, 0, 0, 0, 0), rank(0, 0, 0, 0, 0), false) {43 t.Error("Invalid order")44 }4546 if compareRanks(rank(3, 0, 0, 0, 5), rank(2, 0, 0, 0, 7), true) ||47 !compareRanks(rank(3, 0, 0, 0, 5), rank(3, 0, 0, 0, 6), false) ||48 !compareRanks(rank(1, 2, 0, 0, 3), rank(1, 3, 0, 0, 2), true) ||49 !compareRanks(rank(0, 0, 0, 0, 0), rank(0, 0, 0, 0, 0), false) {50 t.Error("Invalid order (tac)")51 }52}5354// Match length, string length, index55func TestResultRank(t *testing.T) {56 // FIXME global57 sortCriteria = []criterion{byScore, byLength}5859 str := []rune("foo")60 item1 := buildResult(61 withIndex(&Item{text: util.RunesToChars(str)}, 1), []Offset{}, 2)62 if item1.points[3] != math.MaxUint16-2 || // Bonus63 item1.points[2] != 3 || // Length64 item1.points[1] != 0 || // Unused65 item1.points[0] != 0 || // Unused66 item1.item.Index() != 1 {67 t.Error(item1)68 }69 // Only differ in index70 item2 := buildResult(&Item{text: util.RunesToChars(str)}, []Offset{}, 2)7172 items := []Result{item1, item2}73 sort.Sort(ByRelevance(items))74 if items[0] != item2 || items[1] != item1 {75 t.Error(items)76 }7778 items = []Result{item2, item1, item1, item2}79 sort.Sort(ByRelevance(items))80 if items[0] != item2 || items[1] != item2 ||81 items[2] != item1 || items[3] != item1 {82 t.Error(items, item1, item1.item.Index(), item2, item2.item.Index())83 }8485 // Sort by relevance86 item3 := buildResult(87 withIndex(&Item{}, 2), []Offset{{1, 3}, {5, 7}}, 3)88 item4 := buildResult(89 withIndex(&Item{}, 2), []Offset{{1, 2}, {6, 7}}, 4)90 item5 := buildResult(91 withIndex(&Item{}, 2), []Offset{{1, 3}, {5, 7}}, 5)92 item6 := buildResult(93 withIndex(&Item{}, 2), []Offset{{1, 2}, {6, 7}}, 6)94 items = []Result{item1, item2, item3, item4, item5, item6}95 sort.Sort(ByRelevance(items))96 if !(items[0] == item6 && items[1] == item5 &&97 items[2] == item4 && items[3] == item3 &&98 items[4] == item2 && items[5] == item1) {99 t.Error(items, item1, item2, item3, item4, item5, item6)100 }101}102103func TestChunkTiebreak(t *testing.T) {104 // FIXME global105 sortCriteria = []criterion{byScore, byChunk}106107 score := 100108 test := func(input string, offset Offset, chunk string) {109 item := buildResult(withIndex(&Item{text: util.RunesToChars([]rune(input))}, 1), []Offset{offset}, score)110 if !(item.points[3] == math.MaxUint16-uint16(score) && item.points[2] == uint16(len(chunk))) {111 t.Error(item.points)112 }113 }114 test("hello foobar goodbye", Offset{8, 9}, "foobar")115 test("hello foobar goodbye", Offset{7, 18}, "foobar goodbye")116 test("hello foobar goodbye", Offset{0, 1}, "hello")117 test("hello foobar goodbye", Offset{5, 7}, "hello foobar") // TBD118}119120func TestColorOffset(t *testing.T) {121 // ------------ 20 ---- -- ----122 // ++++++++ ++++++++++123 // --++++++++-- --++++++++++---124125 offsets := []Offset{{5, 15}, {10, 12}, {25, 35}}126 item := Result{127 item: &Item{128 colors: &[]ansiOffset{129 {[2]int32{0, 20}, ansiState{1, 5, -1, 0, -1, nil}},130 {[2]int32{22, 27}, ansiState{2, 6, -1, tui.Bold, -1, nil}},131 {[2]int32{30, 32}, ansiState{3, 7, -1, 0, -1, nil}},132 {[2]int32{33, 40}, ansiState{4, 8, -1, tui.Bold, -1, nil}}}}}133134 colBase := tui.NewColorPair(89, 189, tui.AttrUndefined)135 colMatch := tui.NewColorPair(99, 199, tui.AttrUndefined)136 colors := item.colorOffsets(offsets, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined, 0, false)137 assert := func(idx int, b int32, e int32, c tui.ColorPair) {138 o := colors[idx]139 if o.offset[0] != b || o.offset[1] != e || o.color != c {140 t.Error(o, b, e, c)141 }142 }143 // [{[0 5] {1 5 0}} {[5 15] {99 199 0}} {[15 20] {1 5 0}}144 // {[22 25] {2 6 1}} {[25 27] {99 199 1}} {[27 30] {99 199 0}}145 // {[30 32] {99 199 0}} {[32 33] {99 199 0}} {[33 35] {99 199 1}}146 // {[35 40] {4 8 1}}]147 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))148 assert(1, 5, 15, colMatch)149 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))150 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))151 assert(4, 25, 27, colMatch.WithAttr(tui.Bold))152 assert(5, 27, 30, colMatch)153 assert(6, 30, 32, colMatch)154 assert(7, 32, 33, colMatch) // TODO: Should we merge consecutive blocks?155 assert(8, 33, 35, colMatch.WithAttr(tui.Bold))156 assert(9, 35, 40, tui.NewColorPair(4, 8, tui.Bold))157158 colRegular := tui.NewColorPair(-1, -1, tui.AttrUndefined)159 colUnderline := tui.NewColorPair(-1, -1, tui.Underline)160161 nthOffsets := []Offset{{37, 39}, {42, 45}}162 for _, attr := range []tui.Attr{tui.AttrRegular, tui.StrikeThrough} {163 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, attr, 0, false)164165 // [{[0 5] {1 5 0}} {[5 15] {1 5 8}} {[15 20] {1 5 0}}166 // {[22 25] {2 6 1}} {[25 27] {2 6 9}} {[27 30] {-1 -1 8}}167 // {[30 32] {3 7 8}} {[32 33] {-1 -1 8}} {[33 35] {4 8 9}}168 // {[35 37] {4 8 1}} {[37 39] {4 8 x|1}} {[39 40] {4 8 x|1}}]169 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))170 assert(1, 5, 15, tui.NewColorPair(1, 5, tui.Underline))171 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))172 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))173 assert(4, 25, 27, tui.NewColorPair(2, 6, tui.Bold|tui.Underline))174 assert(5, 27, 30, colUnderline)175 assert(6, 30, 32, tui.NewColorPair(3, 7, tui.Underline))176 assert(7, 32, 33, colUnderline)177 assert(8, 33, 35, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))178 assert(9, 35, 37, tui.NewColorPair(4, 8, tui.Bold))179 expected := tui.Bold | attr180 if attr == tui.AttrRegular {181 expected = tui.Bold182 }183 assert(10, 37, 39, tui.NewColorPair(4, 8, expected))184 assert(11, 39, 40, tui.NewColorPair(4, 8, tui.Bold))185 }186187 // Test nthOverlay: simulates nth:regular with current-fg:underline188 // The overlay (underline) should survive even though nth:regular clears attrs.189 // Precedence: fg < nth < current-fg190 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, tui.AttrRegular, tui.Underline, false)191192 // nth regions should have Underline (from overlay), not cleared by AttrRegular193 // Non-nth regions keep colBase attrs (AttrUndefined)194 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))195 assert(1, 5, 15, tui.NewColorPair(1, 5, tui.Underline))196 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))197 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))198 assert(4, 25, 27, tui.NewColorPair(2, 6, tui.Bold|tui.Underline))199 assert(5, 27, 30, colUnderline)200 assert(6, 30, 32, tui.NewColorPair(3, 7, tui.Underline))201 assert(7, 32, 33, colUnderline)202 assert(8, 33, 35, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))203 assert(9, 35, 37, tui.NewColorPair(4, 8, tui.Bold))204 // nth region within ANSI bold: AttrRegular clears, overlay adds Underline back205 assert(10, 37, 39, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))206 assert(11, 39, 40, tui.NewColorPair(4, 8, tui.Bold))207208 // Test nthOverlay with additive attrs: nth:strikethrough with selected-fg:bold209 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, tui.StrikeThrough, tui.Bold, false)210211 // Non-nth entries unchanged from overlay=0 case212 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))213 assert(5, 27, 30, colUnderline) // match only, no nth214 assert(7, 32, 33, colUnderline) // match only, no nth215 // nth region within ANSI bold: StrikeThrough|Bold merged with ANSI Bold216 assert(10, 37, 39, tui.NewColorPair(4, 8, tui.Bold|tui.StrikeThrough))217}218219func TestRadixSortResults(t *testing.T) {220 sortCriteria = []criterion{byScore, byLength}221222 rng := rand.New(rand.NewSource(42))223224 for _, n := range []int{128, 256, 500, 1000} {225 for _, tac := range []bool{false, true} {226 // Build items with random points and indices227 items := make([]*Item, n)228 for i := range items {229 items[i] = &Item{text: util.Chars{Index: int32(i)}}230 }231232 results := make([]Result, n)233 for i := range results {234 results[i] = Result{235 item: items[i],236 points: [4]uint16{237 uint16(rng.Intn(256)),238 uint16(rng.Intn(256)),239 uint16(rng.Intn(256)),240 uint16(rng.Intn(256)),241 },242 }243 }244245 // Make some duplicates to test stability246 for i := 0; i < n/4; i++ {247 j := rng.Intn(n)248 k := rng.Intn(n)249 results[j].points = results[k].points250 }251252 // Copy for reference sort253 expected := make([]Result, n)254 copy(expected, results)255 if tac {256 sort.Sort(ByRelevanceTac(expected))257 } else {258 sort.Sort(ByRelevance(expected))259 }260261 // Radix sort262 var scratch []Result263 scratch = radixSortResults(results, tac, scratch)264265 for i := range results {266 if results[i] != expected[i] {267 t.Errorf("n=%d tac=%v: mismatch at index %d: got item %d, want item %d",268 n, tac, i, results[i].item.Index(), expected[i].item.Index())269 break270 }271 }272 }273 }274}Fix --tiebreak=pathname with non-ASCII textJunegunn Choibefore adoptionb395cfb
lines 275–299why, one step back275276func TestPathnameTiebreak(t *testing.T) {277 // FIXME global278 sortCriteria = []criterion{byScore, byPathname}279280 score := 100281 test := func(input string, offset Offset, expected uint16) {282 for _, chars := range []util.Chars{util.ToChars([]byte(input)), util.RunesToChars([]rune(input))} {283 item := buildResult(withIndex(&Item{text: chars}, 1), []Offset{offset}, score)284 if item.points[3] != math.MaxUint16-uint16(score) || item.points[2] != expected {285 t.Error(input, item.points, expected)286 }287 }288 }289290 // Match in the file name291 test("x/foo/foo.txt", Offset{6, 9}, 1)292 // Match in the directory path293 test("x/foo/aa.txt", Offset{2, 5}, math.MaxUint16)294295 // Offsets are rune indexes, so a multi-byte character before the last296 // delimiter must not shift the delimiter position297 test("一x/foo/foo.txt", Offset{7, 10}, 1)298 test("一x/foo/aa.txt", Offset{3, 6}, math.MaxUint16)299}