Skip to content

Commit

Permalink
Added search input history test
Browse files Browse the repository at this point in the history
Added test for candidate list.
searchCandidates has been changed to an input method.
  • Loading branch information
noborus committed Oct 20, 2023
1 parent b3fd1b4 commit c25a8a6
Show file tree
Hide file tree
Showing 4 changed files with 421 additions and 6 deletions.
2 changes: 1 addition & 1 deletion oviewer/input_multicolor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (root *Root) setMultiColorMode() {
input.value = ""
input.cursorX = 0

searches := root.searchCandidates(searchCandidateListLen)
searches := root.input.searchCandidates(searchCandidateListLen)
input.MultiColorCandidate.toLast(strings.Join(searches, " "))
old := root.Doc.MultiColorWords
input.MultiColorCandidate.toLast(strings.Join(old, " "))
Expand Down
10 changes: 5 additions & 5 deletions oviewer/input_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func (e *eventInputBackSearch) Down(str string) string {
}

// searchCandidates returns the list of search candidates.
func (root *Root) searchCandidates(n int) []string {
root.input.SearchCandidate.mux.Lock()
defer root.input.SearchCandidate.mux.Unlock()
func (input *Input) searchCandidates(n int) []string {
input.SearchCandidate.mux.Lock()
defer input.SearchCandidate.mux.Unlock()

listLen := len(root.input.SearchCandidate.list)
listLen := len(input.SearchCandidate.list)
start := max(0, listLen-n)
return root.input.SearchCandidate.list[start:listLen]
return input.SearchCandidate.list[start:listLen]
}
330 changes: 330 additions & 0 deletions oviewer/input_search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
package oviewer

import (
"reflect"
"testing"

"github.com/gdamore/tcell/v2"
)

func Test_eventInputSearch_Prompt(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "searchPrompt",
want: "/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := NewInput()
e := newSearchEvent(input.SearchCandidate)
if got := e.Prompt(); got != tt.want {
t.Errorf("eventInputSearch.Prompt() = %v, want %v", got, tt.want)
}
})
}
}

func Test_eventInputSearch_Confirm(t *testing.T) {
type fields struct {
EventTime tcell.EventTime
clist *candidate
value string
}
type args struct {
str string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "key enter",
fields: fields{
EventTime: tcell.EventTime{},
clist: searchCandidate(),
value: "",
},
args: args{
str: "test",
},
want: "test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &eventInputSearch{
EventTime: tt.fields.EventTime,
clist: tt.fields.clist,
value: tt.fields.value,
}
if got := e.Confirm(tt.args.str); got.(*eventInputSearch).value != tt.want {
t.Errorf("eventInputSearch.Confirm() = %v, want %v", got, tt.want)
}
})
}
}

func Test_eventInputSearch_Up(t *testing.T) {
type fields struct {
list []string
}
type args struct {
str string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "key up",
fields: fields{
list: []string{"a", "b", "c"},
},
args: args{
str: "testLast",
},
want: "testLast",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := NewInput()
for _, v := range tt.fields.list {
input.SearchCandidate.toAddLast(v)
}
e := newSearchEvent(input.SearchCandidate)
if got := e.Up(tt.args.str); got != tt.want {
t.Errorf("eventInputSearch.Up() = %v, want %v", got, tt.want)
}
})
}
}

func Test_eventInputSearch_Down(t *testing.T) {
type fields struct {
list []string
}
type args struct {
str string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "key down",
fields: fields{
list: []string{"a", "b", "c"},
},
args: args{
str: "testTop",
},
want: "a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := NewInput()
for _, v := range tt.fields.list {
input.SearchCandidate.toAddLast(v)
}
e := newSearchEvent(input.SearchCandidate)
if got := e.Down(tt.args.str); got != tt.want {
t.Logf("%v\n", e.clist.list)
t.Errorf("eventInputSearch.Down() = %v, want %v", got, tt.want)
}
})
}
}

// Test_eventInputBackSearch_Prompt.
func Test_eventInputBackSearch_Prompt(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "backSearchPrompt",
want: "?",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := NewInput()
e := newBackSearchEvent(input.SearchCandidate)
if got := e.Prompt(); got != tt.want {
t.Errorf("eventInputBackSearch.Prompt() = %v, want %v", got, tt.want)
}
})
}
}

// Test_eventInputBackSearch_Confirm.
func Test_eventInputBackSearch_C(t *testing.T) {
type fields struct {
EventTime tcell.EventTime
clist *candidate
value string
}
type args struct {
str string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "key enter",
fields: fields{
EventTime: tcell.EventTime{},
clist: searchCandidate(),
value: "",
},
args: args{
str: "test",
},
want: "test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &eventInputBackSearch{
EventTime: tt.fields.EventTime,
clist: tt.fields.clist,
value: tt.fields.value,
}
if got := e.Confirm(tt.args.str); got.(*eventInputBackSearch).value != tt.want {
t.Errorf("eventInputBackSearch.Confirm() = %v, want %v", got, tt.want)
}
})
}
}

// Test_eventInputBackSearch_Up.
func Test_eventInputBackSearch_Up(t *testing.T) {
type fields struct {
list []string
}
type args struct {
str string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "key up",
fields: fields{
list: []string{"a", "b", "c"},
},
args: args{
str: "testLast",
},
want: "testLast",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := NewInput()
for _, v := range tt.fields.list {
input.SearchCandidate.toAddLast(v)
}
e := newBackSearchEvent(input.SearchCandidate)
if got := e.Up(tt.args.str); got != tt.want {
t.Errorf("eventInputBackSearch.Up() = %v, want %v", got, tt.want)
}
})
}
}

// Test_eventInputBackSearch_Down.
func Test_eventInputBackSearch_Down(t *testing.T) {
type fields struct {
list []string
}
type args struct {
str string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "key down",
fields: fields{
list: []string{"a", "b", "c"},
},
args: args{
str: "testTop",
},
want: "a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := NewInput()
for _, v := range tt.fields.list {
input.SearchCandidate.toAddLast(v)
}
e := newBackSearchEvent(input.SearchCandidate)
if got := e.Down(tt.args.str); got != tt.want {
t.Logf("%v\n", e.clist.list)
t.Errorf("eventInputBackSearch.Down() = %v, want %v", got, tt.want)
}
})
}
}

func TestInput_searchCandidates(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
fields struct {
list []string
}
args args
want []string
}{
{
name: "testSearchCandidates",
fields: struct {
list []string
}{
list: []string{"a", "b", "c"},
},
args: args{
n: 10,
},
want: []string{"a", "b", "c"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := NewInput()
for _, v := range tt.fields.list {
input.SearchCandidate.toAddLast(v)
}
if got := input.searchCandidates(tt.args.n); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Input.searchCandidates() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit c25a8a6

Please sign in to comment.