github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/edit/completion/completion_test.go (about)

     1  package completion
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/u-root/u-root/cmds/elvish/eval"
     8  	"github.com/u-root/u-root/cmds/elvish/parse"
     9  	"github.com/u-root/u-root/cmds/elvish/parse/parseutil"
    10  )
    11  
    12  type complContextFinderTest struct {
    13  	src  string
    14  	want complContext
    15  }
    16  
    17  func testComplContextFinder(t *testing.T, name string, finder complContextFinder, tests []complContextFinderTest) {
    18  	ev := eval.NewEvaler()
    19  	defer ev.Close()
    20  	for _, test := range tests {
    21  		n, err := parse.Parse("[test]", test.src)
    22  		// Ignore error as long is n is non-nil
    23  		if n == nil {
    24  			panic(err)
    25  		}
    26  		leaf := parseutil.FindLeafNode(n, len(test.src))
    27  		got := finder(leaf, ev)
    28  		if !reflect.DeepEqual(got, test.want) {
    29  			t.Errorf("For %q, %s(leaf) => %v, want %v", test.src, name, got, test.want)
    30  		}
    31  	}
    32  }
    33  
    34  type filterRawCandidatesTest struct {
    35  	name    string
    36  	matcher eval.Callable
    37  	seed    string
    38  	src     []string
    39  	want    []string
    40  }
    41  
    42  func testRawFilterCandidates(t *testing.T, tests []filterRawCandidatesTest) {
    43  	ev := eval.NewEvaler()
    44  	defer ev.Close()
    45  	for _, test := range tests {
    46  		input := make(chan rawCandidate, len(test.src))
    47  		for _, s := range test.src {
    48  			input <- plainCandidate(s)
    49  		}
    50  		close(input)
    51  
    52  		result, err := filterRawCandidates(ev, test.matcher, test.seed, input)
    53  		if err != nil {
    54  			t.Errorf("For %s, got unexpected error %v", test.name, err)
    55  		}
    56  
    57  		got := make([]string, len(result))
    58  		for i, r := range result {
    59  			got[i] = r.text()
    60  		}
    61  		if !reflect.DeepEqual(got, test.want) {
    62  			t.Errorf("For %s, got unexpected result => %v, want %v", test.name, got, test.want)
    63  		}
    64  	}
    65  }