Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace to tcell from termbox #32

Merged
merged 26 commits into from
Dec 25, 2020
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 300 ./...
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not a part of the changes PR's title describes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change related to #32 (comment)

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

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
28 changes: 9 additions & 19 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 @@ -69,31 +69,21 @@ func ExampleFindMulti() {
}

func ExampleTerminalMock() {
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
}

// 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})...)
term := fuzzyfinder.UseMockedTerminalV2()
keys := "foo"
for _, r := range keys {
term.InjectKey(tcell.KeyRune, r, tcell.ModNone)
}
term.InjectKey(tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone)

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

// Write out the execution result to a temp file.
// We can test it by the golden files testing pattern.
//
// See https://speakerdeck.com/mitchellh/advanced-testing-with-go?slide=19
res := term.GetResult()
ioutil.WriteFile("ui.out", []byte(res), 0644)
result := term.GetResultV2()
ioutil.WriteFile("ui.out", []byte(result), 0644)
}
113 changes: 60 additions & 53 deletions fuzzing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,71 @@ 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 termbox.Key
key tcell.Key
name string
}

var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789一花二乃三玖四葉五月")
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,
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,
}
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",
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",
}
)

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", 100, "number of events")
numEvents = flag.Int("numEvents", 10, "number of events")
)

// TestFuzz executes fuzzing tests.
//
// Example:
//
// go test -tags fuzz -run TestFuzz -numCases 1000 -numEvents 100
// go test -tags fuzz -run TestFuzz -numCases 10 -numEvents 10
//
func TestFuzz(t *testing.T) {
f, err := os.Create(*out)
Expand All @@ -87,25 +86,32 @@ 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++ {
n := rand.Intn(*numEvents) + 10
events := make([]termbox.Event, n)
// number of events in tcell.SimulationScreen is limited 10
n := rand.Intn(min(*numEvents, 10))
events := make([]tcell.Event, n)
for i := 0; i < n; i++ {
if rand.Intn(10) > 3 {
events[i] = ch(letters[rand.Intn(len(letters)-1)])
} else {
events[i] = key(tbkeys[rand.Intn(len(tbkeys)-1)])
k := tbkeys[rand.Intn(len(tbkeys)-1)]
events[i] = key(input{k, rune(k), tcell.ModNone})
}
}

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

Expand All @@ -121,9 +127,10 @@ func TestFuzz(t *testing.T) {
var mu sync.Mutex
tracks := tracks

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

term.SetEventsV2(events...)

var (
iface interface{}
Expand Down
Loading