github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/internal/jsre/completion_test.go (about)

     1  package jsre
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestCompleteKeywords(t *testing.T) {
    10  	re := New("", os.Stdout)
    11  	re.Run(`
    12  		function theClass() {
    13  			this.foo = 3;
    14  			this.gazonk = {xyz: 4};
    15  		}
    16  		theClass.prototype.someMethod = function () {};
    17    		var x = new theClass();
    18    		var y = new theClass();
    19  		y.someMethod = function override() {};
    20  	`)
    21  
    22  	var tests = []struct {
    23  		input string
    24  		want  []string
    25  	}{
    26  		{
    27  			input: "x",
    28  			want:  []string{"x."},
    29  		},
    30  		{
    31  			input: "x.someMethod",
    32  			want:  []string{"x.someMethod("},
    33  		},
    34  		{
    35  			input: "x.",
    36  			want: []string{
    37  				"x.constructor",
    38  				"x.foo",
    39  				"x.gazonk",
    40  				"x.someMethod",
    41  			},
    42  		},
    43  		{
    44  			input: "y.",
    45  			want: []string{
    46  				"y.constructor",
    47  				"y.foo",
    48  				"y.gazonk",
    49  				"y.someMethod",
    50  			},
    51  		},
    52  		{
    53  			input: "x.gazonk.",
    54  			want: []string{
    55  				"x.gazonk.constructor",
    56  				"x.gazonk.hasOwnProperty",
    57  				"x.gazonk.isPrototypeOf",
    58  				"x.gazonk.propertyIsEnumerable",
    59  				"x.gazonk.toLocaleString",
    60  				"x.gazonk.toString",
    61  				"x.gazonk.valueOf",
    62  				"x.gazonk.xyz",
    63  			},
    64  		},
    65  	}
    66  	for _, test := range tests {
    67  		cs := re.CompleteKeywords(test.input)
    68  		if !reflect.DeepEqual(cs, test.want) {
    69  			t.Errorf("wrong completions for %q\ngot  %v\nwant %v", test.input, cs, test.want)
    70  		}
    71  	}
    72  }