From b222816d3b4d45090876f1555dfb9d0b71753ee5 Mon Sep 17 00:00:00 2001 From: ktr0731 Date: Sun, 23 Oct 2022 15:57:51 +0900 Subject: [PATCH] add missing tests --- fuzzyfinder_test.go | 202 +++++++++++------- .../testfind-cursor_begins_at_top.golden | 11 + testdata/fixtures/testfind-header_line.golden | 11 + ...er_line_which_exceeds_max_charaters.golden | 11 + testdata/fixtures/testfind-pg-dn.golden | 11 + testdata/fixtures/testfind-pg-dn_twice.golden | 11 + testdata/fixtures/testfind-pg-up.golden | 11 + testdata/fixtures/testfind-pg-up_twice.golden | 11 + 8 files changed, 207 insertions(+), 72 deletions(-) create mode 100644 testdata/fixtures/testfind-cursor_begins_at_top.golden create mode 100644 testdata/fixtures/testfind-header_line.golden create mode 100644 testdata/fixtures/testfind-header_line_which_exceeds_max_charaters.golden create mode 100644 testdata/fixtures/testfind-pg-dn.golden create mode 100644 testdata/fixtures/testfind-pg-dn_twice.golden create mode 100644 testdata/fixtures/testfind-pg-up.golden create mode 100644 testdata/fixtures/testfind-pg-up_twice.golden diff --git a/fuzzyfinder_test.go b/fuzzyfinder_test.go index 4c4f42c..2cdcda1 100644 --- a/fuzzyfinder_test.go +++ b/fuzzyfinder_test.go @@ -122,73 +122,126 @@ func TestFind(t *testing.T) { cases := map[string]struct { events []tcell.Event + opts []fuzzyfinder.Option }{ "initial": {}, - "input lo": {runes("lo")}, - "input glow": {runes("glow")}, - "arrow up-down": {keys([]input{ - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyDown, rune(tcell.KeyDown), tcell.ModNone}, - }...)}, - "arrow left-right": {append(runes("ゆるふわ樹海"), keys([]input{ - {tcell.KeyLeft, rune(tcell.KeyLeft), tcell.ModNone}, - {tcell.KeyLeft, rune(tcell.KeyLeft), tcell.ModNone}, - {tcell.KeyRight, rune(tcell.KeyRight), tcell.ModNone}, - }...)...)}, - "backspace": {append(runes("adr .-"), keys([]input{ - {tcell.KeyBackspace, rune(tcell.KeyBackspace), tcell.ModNone}, - {tcell.KeyBackspace, rune(tcell.KeyBackspace), tcell.ModNone}, - }...)...)}, - "backspace empty": {keys(input{tcell.KeyBackspace2, rune(tcell.KeyBackspace2), tcell.ModNone})}, - "backspace2": {append(runes("オレンジ"), keys([]input{ - {tcell.KeyBackspace2, rune(tcell.KeyBackspace2), tcell.ModNone}, - {tcell.KeyBackspace2, rune(tcell.KeyBackspace2), tcell.ModNone}, - }...)...)}, - "arrow left backspace": {append(runes("オレンジ"), keys([]input{ - {tcell.KeyLeft, rune(tcell.KeyLeft), tcell.ModNone}, - {tcell.KeyBackspace, rune(tcell.KeyBackspace), tcell.ModNone}, - }...)...)}, - "delete": {append(runes("オレンジ"), keys([]input{ - {tcell.KeyCtrlA, 'A', tcell.ModCtrl}, - {tcell.KeyDelete, rune(tcell.KeyDelete), tcell.ModNone}, - }...)...)}, - "delete empty": {keys([]input{ - {tcell.KeyCtrlA, 'A', tcell.ModCtrl}, - {tcell.KeyDelete, rune(tcell.KeyDelete), tcell.ModNone}, - }...)}, - "ctrl-e": {append(runes("恋をしたのは"), keys([]input{ - {tcell.KeyCtrlA, 'A', tcell.ModCtrl}, - {tcell.KeyCtrlE, 'E', tcell.ModCtrl}, - }...)...)}, - "ctrl-w": {append(runes("ハロ / ハワユ"), keys(input{tcell.KeyCtrlW, 'W', tcell.ModCtrl})...)}, - "ctrl-w empty": {keys(input{tcell.KeyCtrlW, 'W', tcell.ModCtrl})}, - "ctrl-u": {append(runes("恋をしたのは"), keys([]input{ - {tcell.KeyLeft, rune(tcell.KeyLeft), tcell.ModNone}, - {tcell.KeyCtrlU, 'U', tcell.ModCtrl}, - {tcell.KeyRight, rune(tcell.KeyRight), tcell.ModNone}, - }...)...)}, - "long item": {keys([]input{ - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - }...)}, - "paging": {keys([]input{ - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, - }...)}, - "tab doesn't work": {keys(input{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone})}, - "backspace doesnt change x if cursorX is 0": {append(runes("a"), keys([]input{ - {tcell.KeyCtrlA, 'A', tcell.ModCtrl}, - {tcell.KeyBackspace, rune(tcell.KeyBackspace), tcell.ModNone}, - {tcell.KeyCtrlF, 'F', tcell.ModCtrl}, - }...)...)}, + "input lo": {events: runes("lo")}, + "input glow": {events: runes("glow")}, + "arrow up-down": { + events: keys([]input{ + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyDown, rune(tcell.KeyDown), tcell.ModNone}, + }...)}, + "arrow left-right": { + events: append(runes("ゆるふわ樹海"), keys([]input{ + {tcell.KeyLeft, rune(tcell.KeyLeft), tcell.ModNone}, + {tcell.KeyLeft, rune(tcell.KeyLeft), tcell.ModNone}, + {tcell.KeyRight, rune(tcell.KeyRight), tcell.ModNone}, + }...)...), + }, + "backspace": { + events: append(runes("adr .-"), keys([]input{ + {tcell.KeyBackspace, rune(tcell.KeyBackspace), tcell.ModNone}, + {tcell.KeyBackspace, rune(tcell.KeyBackspace), tcell.ModNone}, + }...)...), + }, + "backspace empty": {events: keys(input{tcell.KeyBackspace2, rune(tcell.KeyBackspace2), tcell.ModNone})}, + "backspace2": { + events: append(runes("オレンジ"), keys([]input{ + {tcell.KeyBackspace2, rune(tcell.KeyBackspace2), tcell.ModNone}, + {tcell.KeyBackspace2, rune(tcell.KeyBackspace2), tcell.ModNone}, + }...)...), + }, + "arrow left backspace": { + events: append(runes("オレンジ"), keys([]input{ + {tcell.KeyLeft, rune(tcell.KeyLeft), tcell.ModNone}, + {tcell.KeyBackspace, rune(tcell.KeyBackspace), tcell.ModNone}, + }...)...), + }, + "delete": { + events: append(runes("オレンジ"), keys([]input{ + {tcell.KeyCtrlA, 'A', tcell.ModCtrl}, + {tcell.KeyDelete, rune(tcell.KeyDelete), tcell.ModNone}, + }...)...), + }, + "delete empty": { + events: keys([]input{ + {tcell.KeyCtrlA, 'A', tcell.ModCtrl}, + {tcell.KeyDelete, rune(tcell.KeyDelete), tcell.ModNone}, + }...), + }, + "ctrl-e": { + events: append(runes("恋をしたのは"), keys([]input{ + {tcell.KeyCtrlA, 'A', tcell.ModCtrl}, + {tcell.KeyCtrlE, 'E', tcell.ModCtrl}, + }...)...), + }, + "ctrl-w": {events: append(runes("ハロ / ハワユ"), keys(input{tcell.KeyCtrlW, 'W', tcell.ModCtrl})...)}, + "ctrl-w empty": {events: keys(input{tcell.KeyCtrlW, 'W', tcell.ModCtrl})}, + "ctrl-u": { + events: append(runes("恋をしたのは"), keys([]input{ + {tcell.KeyLeft, rune(tcell.KeyLeft), tcell.ModNone}, + {tcell.KeyCtrlU, 'U', tcell.ModCtrl}, + {tcell.KeyRight, rune(tcell.KeyRight), tcell.ModNone}, + }...)...), + }, + "pg-up": { + events: keys([]input{ + {tcell.KeyPgUp, rune(tcell.KeyPgUp), tcell.ModNone}, + }...), + }, + "pg-up twice": { + events: keys([]input{ + {tcell.KeyPgUp, rune(tcell.KeyPgUp), tcell.ModNone}, + {tcell.KeyPgUp, rune(tcell.KeyPgUp), tcell.ModNone}, + }...), + }, + "pg-dn": { + events: keys([]input{ + {tcell.KeyPgUp, rune(tcell.KeyPgUp), tcell.ModNone}, + {tcell.KeyPgUp, rune(tcell.KeyPgUp), tcell.ModNone}, + {tcell.KeyPgDn, rune(tcell.KeyPgDn), tcell.ModNone}, + }...), + }, + "pg-dn twice": { + events: keys([]input{ + {tcell.KeyPgUp, rune(tcell.KeyPgUp), tcell.ModNone}, + {tcell.KeyPgUp, rune(tcell.KeyPgUp), tcell.ModNone}, + {tcell.KeyPgDn, rune(tcell.KeyPgDn), tcell.ModNone}, + {tcell.KeyPgDn, rune(tcell.KeyPgDn), tcell.ModNone}, + }...), + }, + "long item": { + events: keys([]input{ + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + }...), + }, + "paging": { + events: keys([]input{ + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + {tcell.KeyUp, rune(tcell.KeyUp), tcell.ModNone}, + }...), + }, + "tab doesn't work": {events: keys(input{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone})}, + "backspace doesnt change x if cursorX is 0": { + events: append(runes("a"), keys([]input{ + {tcell.KeyCtrlA, 'A', tcell.ModCtrl}, + {tcell.KeyBackspace, rune(tcell.KeyBackspace), tcell.ModNone}, + {tcell.KeyCtrlF, 'F', tcell.ModCtrl}, + }...)...), + }, + "cursor begins at top": {opts: []fuzzyfinder.Option{fuzzyfinder.WithCursorPosition(fuzzyfinder.CursorPositionTop)}}, + "header line": {opts: []fuzzyfinder.Option{fuzzyfinder.WithHeader("Search?")}}, + "header line which exceeds max charaters": {opts: []fuzzyfinder.Option{fuzzyfinder.WithHeader("Waht do you want to search for?")}}, } for name, c := range cases { @@ -203,19 +256,24 @@ func TestFind(t *testing.T) { events = append(events, key(input{tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone})) term.SetEventsV2(events...) + opts := append( + c.opts, + fuzzyfinder.WithPreviewWindow(func(i, width, height int) string { + if i == -1 { + return "not found" + } + return "Name: " + tracks[i].Name + "\nArtist: " + tracks[i].Artist + }), + fuzzyfinder.WithMode(fuzzyfinder.ModeCaseSensitive), + ) + assertWithGolden(t, func(t *testing.T) string { _, err := f.Find( tracks, func(i int) string { return tracks[i].Name }, - fuzzyfinder.WithPreviewWindow(func(i, width, height int) string { - if i == -1 { - return "not found" - } - return "Name: " + tracks[i].Name + "\nArtist: " + tracks[i].Artist - }), - fuzzyfinder.WithMode(fuzzyfinder.ModeCaseSensitive), + opts..., ) if !errors.Is(err, fuzzyfinder.ErrAbort) { t.Fatalf("Find must return ErrAbort, but got '%s'", err) diff --git a/testdata/fixtures/testfind-cursor_begins_at_top.golden b/testdata/fixtures/testfind-cursor_begins_at_top.golden new file mode 100644 index 0000000..7e323fe --- /dev/null +++ b/testdata/fixtures/testfind-cursor_begins_at_top.golden @@ -0,0 +1,11 @@ + ICHIDAIJI ┌────────────────────────────┐ + メーベル │ Name: Catch the Moment │ + glow │ Artist: LiSA │ + closing │ │ + ソラニン │ │ + adrenaline!!! │ │ + ヒトリノ夜 │ │ + あの日自分が出て行ってや.. │ │ + 9/9 │ │ +> █ └────────────────────────────┘ + \ No newline at end of file diff --git a/testdata/fixtures/testfind-header_line.golden b/testdata/fixtures/testfind-header_line.golden new file mode 100644 index 0000000..d69628a --- /dev/null +++ b/testdata/fixtures/testfind-header_line.golden @@ -0,0 +1,11 @@ + メーベル ┌────────────────────────────┐ + glow │ Name: あの日自分が出て行.. │ + closing │ Artist: │ + ソラニン │ │ + adrenaline!!! │ │ + ヒトリノ夜 │ │ +> あの日自分が出て行ってや.. │ │ + 9/9 │ │ + Search? │ │ +> █ └────────────────────────────┘ + \ No newline at end of file diff --git a/testdata/fixtures/testfind-header_line_which_exceeds_max_charaters.golden b/testdata/fixtures/testfind-header_line_which_exceeds_max_charaters.golden new file mode 100644 index 0000000..d227ae2 --- /dev/null +++ b/testdata/fixtures/testfind-header_line_which_exceeds_max_charaters.golden @@ -0,0 +1,11 @@ + メーベル ┌────────────────────────────┐ + glow │ Name: あの日自分が出て行.. │ + closing │ Artist: │ + ソラニン │ │ + adrenaline!!! │ │ + ヒトリノ夜 │ │ +> あの日自分が出て行ってや.. │ │ + 9/9 │ │ + Waht do you want to searc.. │ │ +> █ └────────────────────────────┘ + \ No newline at end of file diff --git a/testdata/fixtures/testfind-pg-dn.golden b/testdata/fixtures/testfind-pg-dn.golden new file mode 100644 index 0000000..fec31c5 --- /dev/null +++ b/testdata/fixtures/testfind-pg-dn.golden @@ -0,0 +1,11 @@ + Catch the Moment ┌────────────────────────────┐ + ICHIDAIJI │ Name: ヒトリノ夜 │ + メーベル │ Artist: ポルノグラフィテ.. │ + glow │ │ + closing │ │ + ソラニン │ │ + adrenaline!!! │ │ +> ヒトリノ夜 │ │ + 9/9 │ │ +> █ └────────────────────────────┘ + \ No newline at end of file diff --git a/testdata/fixtures/testfind-pg-dn_twice.golden b/testdata/fixtures/testfind-pg-dn_twice.golden new file mode 100644 index 0000000..ac28a88 --- /dev/null +++ b/testdata/fixtures/testfind-pg-dn_twice.golden @@ -0,0 +1,11 @@ + ICHIDAIJI ┌────────────────────────────┐ + メーベル │ Name: あの日自分が出て行.. │ + glow │ Artist: │ + closing │ │ + ソラニン │ │ + adrenaline!!! │ │ + ヒトリノ夜 │ │ +> あの日自分が出て行ってや.. │ │ + 9/9 │ │ +> █ └────────────────────────────┘ + \ No newline at end of file diff --git a/testdata/fixtures/testfind-pg-up.golden b/testdata/fixtures/testfind-pg-up.golden new file mode 100644 index 0000000..2752fa9 --- /dev/null +++ b/testdata/fixtures/testfind-pg-up.golden @@ -0,0 +1,11 @@ +> ICHIDAIJI ┌────────────────────────────┐ + メーベル │ Name: ICHIDAIJI │ + glow │ Artist: ポルカドットステ.. │ + closing │ │ + ソラニン │ │ + adrenaline!!! │ │ + ヒトリノ夜 │ │ + あの日自分が出て行ってや.. │ │ + 9/9 │ │ +> █ └────────────────────────────┘ + \ No newline at end of file diff --git a/testdata/fixtures/testfind-pg-up_twice.golden b/testdata/fixtures/testfind-pg-up_twice.golden new file mode 100644 index 0000000..21d4684 --- /dev/null +++ b/testdata/fixtures/testfind-pg-up_twice.golden @@ -0,0 +1,11 @@ +> Catch the Moment ┌────────────────────────────┐ + ICHIDAIJI │ Name: Catch the Moment │ + メーベル │ Artist: LiSA │ + glow │ │ + closing │ │ + ソラニン │ │ + adrenaline!!! │ │ + ヒトリノ夜 │ │ + 9/9 │ │ +> █ └────────────────────────────┘ + \ No newline at end of file