github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/completions_test.go (about)

     1  // Copyright 2022 Edward McFarlane. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package starlib
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"go.starlark.net/starlark"
    12  	"go.starlark.net/starlarkstruct"
    13  )
    14  
    15  type testCallableArgs struct {
    16  	args []string
    17  }
    18  
    19  func (c testCallableArgs) String() string        { return "callable_kwargs" }
    20  func (c testCallableArgs) Type() string          { return "callable_kwargs" }
    21  func (c testCallableArgs) Freeze()               {}
    22  func (c testCallableArgs) Truth() starlark.Bool  { return true }
    23  func (c testCallableArgs) Hash() (uint32, error) { return 0, nil }
    24  func (c testCallableArgs) Name() string          { return "callable_kwargs" }
    25  func (c testCallableArgs) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    26  	return starlark.None, nil
    27  }
    28  func (c testCallableArgs) ArgNames() []string { return c.args }
    29  
    30  func TestAutoComplete(t *testing.T) {
    31  	d := starlark.NewDict(2)
    32  	if err := d.SetKey(starlark.String("key"), starlark.String("value")); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	mod := &starlarkstruct.Module{
    36  		Name: "hello",
    37  		Members: starlark.StringDict{
    38  			"world": starlark.String("world"),
    39  			"dict":  d,
    40  			"func": testCallableArgs{[]string{
    41  				"kwarg",
    42  			}},
    43  		},
    44  	}
    45  
    46  	for _, tt := range []struct {
    47  		name    string
    48  		globals starlark.StringDict
    49  		line    string
    50  		want    []string
    51  	}{{
    52  		name: "simple",
    53  		globals: map[string]starlark.Value{
    54  			"abc": starlark.String("hello"),
    55  		},
    56  		line: "a",
    57  		want: []string{"abc", "abs", "all", "any"},
    58  	}, {
    59  		name: "simple_semi",
    60  		globals: map[string]starlark.Value{
    61  			"abc": starlark.String("hello"),
    62  		},
    63  		line: "abc = \"hello\"; a",
    64  		want: []string{
    65  			"abc = \"hello\"; abc",
    66  			"abc = \"hello\"; abs",
    67  			"abc = \"hello\"; all",
    68  			"abc = \"hello\"; any",
    69  		},
    70  	}, {
    71  		name: "assignment",
    72  		globals: map[string]starlark.Value{
    73  			"abc": starlark.String("hello"),
    74  		},
    75  		line: "abc = a",
    76  		want: []string{
    77  			"abc = abc",
    78  			"abc = abs",
    79  			"abc = all",
    80  			"abc = any",
    81  		},
    82  	}, {
    83  		name: "nest",
    84  		globals: map[string]starlark.Value{
    85  			"hello": mod,
    86  		},
    87  		line: "hello.wo",
    88  		want: []string{"hello.world"},
    89  	}, {
    90  		name: "dict",
    91  		globals: map[string]starlark.Value{
    92  			"abc":   starlark.String("hello"),
    93  			"hello": mod,
    94  		},
    95  		line: "hello.dict[ab",
    96  		want: []string{
    97  			"hello.dict[abc",
    98  			"hello.dict[abs",
    99  		},
   100  	}, {
   101  		name: "dict_string",
   102  		globals: map[string]starlark.Value{
   103  			"hello": mod,
   104  		},
   105  		line: "hello.dict[\"",
   106  		want: []string{"hello.dict[\"key\"]"},
   107  	}, {
   108  		name: "call",
   109  		globals: map[string]starlark.Value{
   110  			"func": testCallableArgs{[]string{
   111  				"arg_one", "arg_two",
   112  			}},
   113  		},
   114  		line: "func(arg_",
   115  		want: []string{"func(arg_one = ", "func(arg_two = "},
   116  	}, {
   117  		name: "call_multi",
   118  		globals: map[string]starlark.Value{
   119  			"func": testCallableArgs{[]string{
   120  				"arg_one", "arg_two",
   121  			}},
   122  		},
   123  		line: "func(arg_one = func(), arg_",
   124  		want: []string{
   125  			"func(arg_one = func(), arg_one = ",
   126  			"func(arg_one = func(), arg_two = ",
   127  		},
   128  	}} {
   129  		t.Run(tt.name, func(t *testing.T) {
   130  			c := Completer{tt.globals}
   131  			got := c.Complete(tt.line)
   132  
   133  			if !reflect.DeepEqual(tt.want, got) {
   134  				t.Errorf("%v != %v", tt.want, got)
   135  			}
   136  		})
   137  	}
   138  }