mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
increase test coverage
This commit is contained in:
parent
717dfca53b
commit
0ccff01ddd
|
|
@ -766,6 +766,51 @@ func TestGHIssue428_PgUpPgDnDefaultBindings(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultKeyBindings(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
rHub := &recordingHub{}
|
||||||
|
|
||||||
|
state := New()
|
||||||
|
state.hub = rHub
|
||||||
|
state.selection = selection.New()
|
||||||
|
state.currentLineBuffer = NewMemoryBuffer(0)
|
||||||
|
|
||||||
|
state.config.Keymap = map[string]string{}
|
||||||
|
state.config.Action = map[string][]string{}
|
||||||
|
require.NoError(t, state.populateKeymap(), "populateKeymap should succeed")
|
||||||
|
|
||||||
|
km := state.Keymap()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
key keyseq.KeyType
|
||||||
|
expected hub.PagingRequest
|
||||||
|
}{
|
||||||
|
{"ArrowUp triggers SelectUp", keyseq.KeyArrowUp, hub.ToLineAbove},
|
||||||
|
{"ArrowDown triggers SelectDown", keyseq.KeyArrowDown, hub.ToLineBelow},
|
||||||
|
{"ArrowRight triggers ScrollPageDown", keyseq.KeyArrowRight, hub.ToScrollPageDown},
|
||||||
|
{"ArrowLeft triggers ScrollPageUp", keyseq.KeyArrowLeft, hub.ToScrollPageUp},
|
||||||
|
{"PgDn triggers ScrollPageDown", keyseq.KeyPgdn, hub.ToScrollPageDown},
|
||||||
|
{"PgUp triggers ScrollPageUp", keyseq.KeyPgup, hub.ToScrollPageUp},
|
||||||
|
{"Home triggers ScrollFirstItem", keyseq.KeyHome, hub.ToScrollFirstItem},
|
||||||
|
{"End triggers ScrollLastItem", keyseq.KeyEnd, hub.ToScrollLastItem},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
rHub.reset()
|
||||||
|
|
||||||
|
ev := Event{Key: tt.key}
|
||||||
|
err := km.ExecuteAction(ctx, state, ev)
|
||||||
|
require.NoError(t, err, "%s should resolve to an action", tt.name)
|
||||||
|
|
||||||
|
pagingArgs := rHub.getPagingArgs()
|
||||||
|
require.Len(t, pagingArgs, 1, "expected one paging call")
|
||||||
|
require.Equal(t, tt.expected, pagingArgs[0])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestGHIssue455_RefreshScreenSendsForceSync verifies that doRefreshScreen
|
// TestGHIssue455_RefreshScreenSendsForceSync verifies that doRefreshScreen
|
||||||
// sends DrawOptions with both DisableCache and ForceSync set to true.
|
// sends DrawOptions with both DisableCache and ForceSync set to true.
|
||||||
func TestGHIssue455_RefreshScreenSendsForceSync(t *testing.T) {
|
func TestGHIssue455_RefreshScreenSendsForceSync(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -20,44 +20,32 @@ func TestBalance(t *testing.T) {
|
||||||
require.Equal(t, 15, trie.Size())
|
require.Equal(t, 15, trie.Size())
|
||||||
trie.Balance()
|
trie.Balance()
|
||||||
|
|
||||||
/*
|
// After balancing, all keys must still be retrievable with correct values.
|
||||||
n8 := trie.Root().(*TernaryNode).firstChild
|
for i, k := range list {
|
||||||
checkTrieNode(t, n8, '8', 7)
|
node := trie.Get(k)
|
||||||
n4 := n8.low
|
require.NotNil(t, node, "key %d should be found after Balance", i)
|
||||||
checkTrieNode(t, n4, '4', 3)
|
require.Equal(t, i, node.Value(), "value for key %d should be %d", i, i)
|
||||||
n12 := n8.high
|
}
|
||||||
checkTrieNode(t, n12, 'C', 11)
|
|
||||||
n2 := n4.low
|
// Size must be unchanged after balancing.
|
||||||
checkTrieNode(t, n2, '2', 1)
|
require.Equal(t, 15, trie.Size())
|
||||||
n6 := n4.high
|
}
|
||||||
checkTrieNode(t, n6, '6', 5)
|
|
||||||
n10 := n12.low
|
func TestBalancePreservesMultiKeySequences(t *testing.T) {
|
||||||
checkTrieNode(t, n10, 'A', 9)
|
trie := NewTernaryTrie()
|
||||||
n14 := n12.high
|
|
||||||
checkTrieNode(t, n14, 'E', 13)
|
// Insert multi-key sequences
|
||||||
n1 := n2.low
|
k1 := KeyList{{0, 0, 'a'}, {0, 0, 'b'}}
|
||||||
checkTrieNode(t, n1, '1', 0)
|
k2 := KeyList{{0, 0, 'a'}, {0, 0, 'c'}}
|
||||||
n3 := n2.high
|
k3 := KeyList{{0, 0, 'x'}}
|
||||||
checkTrieNode(t, n3, '3', 2)
|
|
||||||
n5 := n6.low
|
trie.Put(k1, "ab")
|
||||||
checkTrieNode(t, n5, '5', 4)
|
trie.Put(k2, "ac")
|
||||||
n7 := n6.high
|
trie.Put(k3, "x")
|
||||||
checkTrieNode(t, n7, '7', 6)
|
|
||||||
n9 := n10.low
|
trie.Balance()
|
||||||
checkTrieNode(t, n9, '9', 8)
|
|
||||||
n11 := n10.high
|
require.Equal(t, "ab", trie.GetList(k1).Value())
|
||||||
checkTrieNode(t, n11, 'B', 10)
|
require.Equal(t, "ac", trie.GetList(k2).Value())
|
||||||
n13 := n14.low
|
require.Equal(t, "x", trie.GetList(k3).Value())
|
||||||
checkTrieNode(t, n13, 'D', 12)
|
|
||||||
n15 := n14.high
|
|
||||||
checkTrieNode(t, n15, 'F', 14)
|
|
||||||
assertNilBoth(t, n1)
|
|
||||||
assertNilBoth(t, n3)
|
|
||||||
assertNilBoth(t, n5)
|
|
||||||
assertNilBoth(t, n7)
|
|
||||||
assertNilBoth(t, n9)
|
|
||||||
assertNilBoth(t, n11)
|
|
||||||
assertNilBoth(t, n13)
|
|
||||||
assertNilBoth(t, n15)
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
73
page_test.go
Normal file
73
page_test.go
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
package peco
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLocationSettersAndGetters(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
set func(*Location, int)
|
||||||
|
get func(*Location) int
|
||||||
|
values []int
|
||||||
|
}{
|
||||||
|
{"Column", (*Location).SetColumn, (*Location).Column, []int{0, 5, 100}},
|
||||||
|
{"LineNumber", (*Location).SetLineNumber, (*Location).LineNumber, []int{0, 10, 999}},
|
||||||
|
{"Offset", (*Location).SetOffset, (*Location).Offset, []int{0, 3, 42}},
|
||||||
|
{"PerPage", (*Location).SetPerPage, (*Location).PerPage, []int{0, 20, 50}},
|
||||||
|
{"Page", (*Location).SetPage, (*Location).Page, []int{0, 1, 7}},
|
||||||
|
{"Total", (*Location).SetTotal, (*Location).Total, []int{0, 100, 5000}},
|
||||||
|
{"MaxPage", (*Location).SetMaxPage, (*Location).MaxPage, []int{0, 5, 25}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
var loc Location
|
||||||
|
for _, v := range tt.values {
|
||||||
|
tt.set(&loc, v)
|
||||||
|
require.Equal(t, v, tt.get(&loc))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocationConcurrentAccess(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
var loc Location
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := range 100 {
|
||||||
|
wg.Add(2)
|
||||||
|
go func(n int) {
|
||||||
|
defer wg.Done()
|
||||||
|
loc.SetLineNumber(n)
|
||||||
|
}(i)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
_ = loc.LineNumber()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocationPageCropSnapshot(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
var loc Location
|
||||||
|
loc.SetPerPage(20)
|
||||||
|
loc.SetPage(3)
|
||||||
|
|
||||||
|
crop := loc.PageCrop()
|
||||||
|
|
||||||
|
// Mutating the Location after snapshot should not affect the crop
|
||||||
|
loc.SetPerPage(50)
|
||||||
|
loc.SetPage(0)
|
||||||
|
|
||||||
|
require.Equal(t, 20, crop.perPage)
|
||||||
|
require.Equal(t, 3, crop.currentPage)
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package selection
|
package selection
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/peco/peco/line"
|
"github.com/peco/peco/line"
|
||||||
|
|
@ -22,3 +23,90 @@ func TestSelection(t *testing.T) {
|
||||||
s.Remove(alice)
|
s.Remove(alice)
|
||||||
require.Equal(t, 1, s.Len())
|
require.Equal(t, 1, s.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSelectionHas(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
s := New()
|
||||||
|
alice := line.NewRaw(0, "Alice", false, false)
|
||||||
|
bob := line.NewRaw(1, "Bob", false, false)
|
||||||
|
|
||||||
|
s.Add(alice)
|
||||||
|
require.True(t, s.Has(alice))
|
||||||
|
require.False(t, s.Has(bob))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSelectionAscendOrder(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
s := New()
|
||||||
|
s.Add(line.NewRaw(3, "Charlie", false, false))
|
||||||
|
s.Add(line.NewRaw(1, "Alice", false, false))
|
||||||
|
s.Add(line.NewRaw(2, "Bob", false, false))
|
||||||
|
|
||||||
|
var ids []uint64
|
||||||
|
s.Ascend(func(l line.Line) bool {
|
||||||
|
ids = append(ids, l.ID())
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
require.Equal(t, []uint64{1, 2, 3}, ids, "Ascend should iterate in ID order")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSelectionReset(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
s := New()
|
||||||
|
s.Add(line.NewRaw(0, "Alice", false, false))
|
||||||
|
s.Add(line.NewRaw(1, "Bob", false, false))
|
||||||
|
require.Equal(t, 2, s.Len())
|
||||||
|
|
||||||
|
s.Reset()
|
||||||
|
require.Equal(t, 0, s.Len())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSelectionCopy(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
src := New()
|
||||||
|
src.Add(line.NewRaw(0, "Alice", false, false))
|
||||||
|
src.Add(line.NewRaw(1, "Bob", false, false))
|
||||||
|
|
||||||
|
dst := New()
|
||||||
|
src.Copy(dst)
|
||||||
|
|
||||||
|
require.Equal(t, 2, dst.Len())
|
||||||
|
require.True(t, dst.Has(line.NewRaw(0, "Alice", false, false)))
|
||||||
|
require.True(t, dst.Has(line.NewRaw(1, "Bob", false, false)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSelectionConcurrentAccess(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
s := New()
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := range 50 {
|
||||||
|
wg.Add(2)
|
||||||
|
go func(id uint64) {
|
||||||
|
defer wg.Done()
|
||||||
|
s.Add(line.NewRaw(id, "line", false, false))
|
||||||
|
}(uint64(i))
|
||||||
|
go func(id uint64) {
|
||||||
|
defer wg.Done()
|
||||||
|
s.Has(line.NewRaw(id, "line", false, false))
|
||||||
|
}(uint64(i))
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
require.Equal(t, 50, s.Len())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRangeStart(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
var rs RangeStart
|
||||||
|
|
||||||
|
require.False(t, rs.Valid())
|
||||||
|
|
||||||
|
rs.SetValue(5)
|
||||||
|
require.True(t, rs.Valid())
|
||||||
|
require.Equal(t, 5, rs.Value())
|
||||||
|
|
||||||
|
rs.Reset()
|
||||||
|
require.False(t, rs.Valid())
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue