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

     1  package completion
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/u-root/u-root/cmds/elvish/parse"
     9  )
    10  
    11  func TestFindVariableComplContext(t *testing.T) {
    12  	testComplContextFinder(t, "findVariableComplContext", findVariableComplContext, []complContextFinderTest{
    13  		{"$", &variableComplContext{
    14  			complContextCommon{"", parse.Bareword, 1, 1}, "", ""}},
    15  		{"$a", &variableComplContext{
    16  			complContextCommon{"a", parse.Bareword, 1, 2}, "", ""}},
    17  		{"$a:", &variableComplContext{
    18  			complContextCommon{"", parse.Bareword, 3, 3}, "a", "a:"}},
    19  		{"$a:b", &variableComplContext{
    20  			complContextCommon{"b", parse.Bareword, 3, 4}, "a", "a:"}},
    21  		// Wrong contexts
    22  		{"", nil},
    23  		{"echo", nil},
    24  	})
    25  }
    26  
    27  type testEvalerScopes struct{}
    28  
    29  var testScopes = map[string]map[string]int{
    30  	"":        {"veni": 0, "vidi": 0, "vici": 0},
    31  	"foo":     {"lorem": 0, "ipsum": 0},
    32  	"foo:bar": {"lorem": 0, "dolor": 0},
    33  }
    34  
    35  func (testEvalerScopes) EachNsInTop(f func(string)) {
    36  	for ns := range testScopes {
    37  		if ns != "" {
    38  			f(ns)
    39  		}
    40  	}
    41  }
    42  
    43  func (testEvalerScopes) EachVariableInTop(ns string, f func(string)) {
    44  	for name := range testScopes[ns] {
    45  		f(name)
    46  	}
    47  }
    48  
    49  var complVariableTests = []struct {
    50  	ns     string
    51  	nsPart string
    52  	want   []rawCandidate
    53  }{
    54  	// No namespace: complete variables and namespaces
    55  	{"", "", []rawCandidate{
    56  		noQuoteCandidate("foo:"), noQuoteCandidate("foo:bar:"),
    57  		noQuoteCandidate("veni"), noQuoteCandidate("vici"), noQuoteCandidate("vidi"),
    58  	}},
    59  	// Nonempty namespace: complete variables in namespace and subnamespaces
    60  	// (but not variables in subnamespaces)
    61  	{"foo", "foo:", []rawCandidate{
    62  		noQuoteCandidate("bar:"),
    63  		noQuoteCandidate("ipsum"), noQuoteCandidate("lorem"),
    64  	}},
    65  	// Bad namespace
    66  	{"bad", "bad:", nil},
    67  }
    68  
    69  func TestComplVariable(t *testing.T) {
    70  	for _, test := range complVariableTests {
    71  		got := collectComplVariable(test.ns, test.nsPart, testEvalerScopes{})
    72  		if !reflect.DeepEqual(got, test.want) {
    73  			t.Errorf("complVariable(%q, %q, ...) => %v, want %v", test.ns, test.nsPart, got, test.want)
    74  		}
    75  	}
    76  }
    77  
    78  func collectComplVariable(ns, nsPart string, ev evalerScopes) []rawCandidate {
    79  	ch := make(chan rawCandidate)
    80  	go func() {
    81  		complVariable(ns, nsPart, ev, ch)
    82  		close(ch)
    83  	}()
    84  	var results []rawCandidate
    85  	for result := range ch {
    86  		results = append(results, result)
    87  	}
    88  	sort.Sort(rawCandidates(results))
    89  	return results
    90  }