Skip to content

Commit

Permalink
revert the chages which migrates terminal library from nsf/termbox-go…
Browse files Browse the repository at this point in the history
… to gdamore/tcell (#47)
  • Loading branch information
ktr0731 authored Jan 17, 2021
1 parent 646e0e7 commit f3bcd19
Show file tree
Hide file tree
Showing 32 changed files with 361 additions and 736 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: go build

- name: Test
run: go test -v -coverpkg ./... -covermode atomic -coverprofile coverage.txt -tags fuzz -numCases 3000 -numEvents 10 ./...
run: go test -v -coverpkg ./... -covermode atomic -coverprofile coverage.txt -tags fuzz -numCases 3000 -numEvents 300 ./...

- name: Lint
uses: golangci/golangci-lint-action@v2
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ linters:
- nolintlint
- rowserrcheck
- errorlint
- exhaustivestruct
- exportloopref
- gci
disable:
- gosimple
- unused
- scopelint
- exhaustivestruct

issues:
exclude:
Expand Down
24 changes: 17 additions & 7 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"io/ioutil"

"github.com/gdamore/tcell/v2"
fuzzyfinder "github.com/ktr0731/go-fuzzyfinder"
"github.com/nsf/termbox-go"
)

func ExampleFind() {
Expand Down Expand Up @@ -70,14 +70,24 @@ func ExampleFindMulti() {
}

func ExampleTerminalMock() {
// Initialize a mocked terminal.
term := fuzzyfinder.UseMockedTerminalV2()
keys := "foo"
for _, r := range keys {
term.InjectKey(tcell.KeyRune, r, tcell.ModNone)
keys := func(str string) []termbox.Event {
s := []rune(str)
e := make([]termbox.Event, 0, len(s))
for _, r := range s {
e = append(e, termbox.Event{Type: termbox.EventKey, Ch: r})
}
return e
}
term.InjectKey(tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone)

// Initialize a mocked terminal.
term := fuzzyfinder.UseMockedTerminal()
// Set the window size and events.
term.SetSize(60, 10)
term.SetEvents(append(
keys("foo"),
termbox.Event{Type: termbox.EventKey, Key: termbox.KeyEsc})...)

// Call fuzzyfinder.Find.
slice := []string{"foo", "bar", "baz"}
_, _ = fuzzyfinder.Find(slice, func(i int) string { return slice[i] })

Expand Down
111 changes: 52 additions & 59 deletions fuzzing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,72 @@ import (
"sync"
"testing"

"github.com/gdamore/tcell/v2"

fuzz "github.com/google/gofuzz"
fuzzyfinder "github.com/ktr0731/go-fuzzyfinder"
"github.com/nsf/termbox-go"
)

type fuzzKey struct {
key tcell.Key
key termbox.Key
name string
}

var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789一花二乃三玖四葉五月")
tbkeys = []tcell.Key{
tcell.KeyCtrlA,
tcell.KeyCtrlB,
tcell.KeyCtrlE,
tcell.KeyCtrlF,
tcell.KeyBackspace,
tcell.KeyTab,
tcell.KeyCtrlJ,
tcell.KeyCtrlK,
tcell.KeyCtrlN,
tcell.KeyCtrlP,
tcell.KeyCtrlU,
tcell.KeyCtrlW,
tcell.KeyBackspace2,
tcell.KeyUp,
tcell.KeyDown,
tcell.KeyLeft,
tcell.KeyRight,
tbkeys = []termbox.Key{
termbox.KeyCtrlA,
termbox.KeyCtrlB,
termbox.KeyCtrlE,
termbox.KeyCtrlF,
termbox.KeyBackspace,
termbox.KeyTab,
termbox.KeyCtrlJ,
termbox.KeyCtrlK,
termbox.KeyCtrlN,
termbox.KeyCtrlP,
termbox.KeyCtrlU,
termbox.KeyCtrlW,
termbox.KeySpace,
termbox.KeyBackspace2,
termbox.KeyArrowUp,
termbox.KeyArrowDown,
termbox.KeyArrowLeft,
termbox.KeyArrowRight,
}
keyMap = map[tcell.Key]string{
tcell.KeyCtrlA: "A",
tcell.KeyCtrlB: "B",
tcell.KeyCtrlE: "E",
tcell.KeyCtrlF: "F",
tcell.KeyBackspace: "backspace",
tcell.KeyTab: "tab",
tcell.KeyCtrlJ: "J",
tcell.KeyCtrlK: "K",
tcell.KeyCtrlN: "N",
tcell.KeyCtrlP: "P",
tcell.KeyCtrlU: "U",
tcell.KeyCtrlW: "W",
tcell.KeyBackspace2: "backspace2",
tcell.KeyUp: "up",
tcell.KeyDown: "down",
tcell.KeyLeft: "left",
tcell.KeyRight: "right",
keyMap = map[termbox.Key]string{
termbox.KeyCtrlA: "A",
termbox.KeyCtrlB: "B",
termbox.KeyCtrlE: "E",
termbox.KeyCtrlF: "F",
termbox.KeyBackspace: "backspace",
termbox.KeyTab: "tab",
termbox.KeyCtrlJ: "J",
termbox.KeyCtrlK: "K",
termbox.KeyCtrlN: "N",
termbox.KeyCtrlP: "P",
termbox.KeyCtrlU: "U",
termbox.KeyCtrlW: "W",
termbox.KeySpace: "space",
termbox.KeyBackspace2: "backspace2",
termbox.KeyArrowUp: "up",
termbox.KeyArrowDown: "down",
termbox.KeyArrowLeft: "left",
termbox.KeyArrowRight: "right",
}
)

var (
out = flag.String("fuzzout", "fuzz.out", "fuzzing error cases")
hotReload = flag.Bool("hotreload", false, "enable hot-reloading")
numCases = flag.Int("numCases", 30, "number of test cases")
numEvents = flag.Int("numEvents", 10, "number of events")
numEvents = flag.Int("numEvents", 100, "number of events")
)

// TestFuzz executes fuzzing tests.
//
// Example:
//
// go test -tags fuzz -run TestFuzz -numCases 10 -numEvents 10
// go test -tags fuzz -run TestFuzz -numCases 1000 -numEvents 100
//
func TestFuzz(t *testing.T) {
f, err := os.Create(*out)
Expand All @@ -86,32 +87,25 @@ func TestFuzz(t *testing.T) {

fuzz := fuzz.New()

min := func(a, b int) int {
if a < b {
return a
}
return b
}

for i := 0; i < rand.Intn(*numCases)+10; i++ {
// number of events in tcell.SimulationScreen is limited 10
n := rand.Intn(min(*numEvents, 10))
events := make([]tcell.Event, n)
n := rand.Intn(*numEvents) + 10
events := make([]termbox.Event, n)
for i := 0; i < n; i++ {
if rand.Intn(10) > 3 {
events[i] = ch(letters[rand.Intn(len(letters)-1)])
} else {
k := tbkeys[rand.Intn(len(tbkeys)-1)]
events[i] = key(input{k, rune(k), tcell.ModNone})
events[i] = key(tbkeys[rand.Intn(len(tbkeys)-1)])
}
}

var name string
for _, e := range events {
if e.(*tcell.EventKey).Rune() != 0 {
name += string(e.(*tcell.EventKey).Rune())
if e.Key == termbox.KeySpace {
name += " "
} else if e.Ch != 0 {
name += string(e.Ch)
} else {
name += "[" + keyMap[e.(*tcell.EventKey).Key()] + "]"
name += "[" + keyMap[e.Key] + "]"
}
}

Expand All @@ -128,9 +122,8 @@ func TestFuzz(t *testing.T) {
tracks := tracks

f, term := fuzzyfinder.NewWithMockedTerminal()
events = append(events, key(input{tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone}))

term.SetEventsV2(events...)
events = append(events, key(termbox.KeyEsc))
term.SetEvents(events...)

var (
iface interface{}
Expand Down
Loading

0 comments on commit f3bcd19

Please sign in to comment.