github.com/ethereum/go-ethereum@v1.16.1/internal/jsre/completion_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package jsre
    18  
    19  import (
    20  	"os"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestCompleteKeywords(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	re := New("", os.Stdout)
    29  	re.Run(`
    30  		function theClass() {
    31  			this.foo = 3;
    32  			this.gazonk = {xyz: 4};
    33  		}
    34  		theClass.prototype.someMethod = function () {};
    35    		var x = new theClass();
    36    		var y = new theClass();
    37  		y.someMethod = function override() {};
    38  	`)
    39  
    40  	var tests = []struct {
    41  		input string
    42  		want  []string
    43  	}{
    44  		{
    45  			input: "St",
    46  			want:  []string{"String"},
    47  		},
    48  		{
    49  			input: "x",
    50  			want:  []string{"x."},
    51  		},
    52  		{
    53  			input: "x.someMethod",
    54  			want:  []string{"x.someMethod("},
    55  		},
    56  		{
    57  			input: "x.",
    58  			want: []string{
    59  				"x.constructor",
    60  				"x.foo",
    61  				"x.gazonk",
    62  				"x.someMethod",
    63  			},
    64  		},
    65  		{
    66  			input: "y.",
    67  			want: []string{
    68  				"y.constructor",
    69  				"y.foo",
    70  				"y.gazonk",
    71  				"y.someMethod",
    72  			},
    73  		},
    74  		{
    75  			input: "x.gazonk.",
    76  			want: []string{
    77  				"x.gazonk.__proto__",
    78  				"x.gazonk.constructor",
    79  				"x.gazonk.hasOwnProperty",
    80  				"x.gazonk.isPrototypeOf",
    81  				"x.gazonk.propertyIsEnumerable",
    82  				"x.gazonk.toLocaleString",
    83  				"x.gazonk.toString",
    84  				"x.gazonk.valueOf",
    85  				"x.gazonk.xyz",
    86  			},
    87  		},
    88  	}
    89  	for _, test := range tests {
    90  		t.Run(test.input, func(t *testing.T) {
    91  			t.Parallel()
    92  
    93  			cs := re.CompleteKeywords(test.input)
    94  			if !reflect.DeepEqual(cs, test.want) {
    95  				t.Errorf("wrong completions for %q\ngot  %v\nwant %v", test.input, cs, test.want)
    96  			}
    97  		})
    98  	}
    99  }