golang.org/x/tools/gopls@v0.15.3/internal/settings/settings_test.go (about)

     1  // Copyright 2020 The Go Authors. 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 settings
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestDefaultsEquivalence(t *testing.T) {
    14  	opts1 := DefaultOptions()
    15  	opts2 := DefaultOptions()
    16  	if !reflect.DeepEqual(opts1, opts2) {
    17  		t.Fatal("default options are not equivalent using reflect.DeepEqual")
    18  	}
    19  }
    20  
    21  func TestSetOption(t *testing.T) {
    22  	tests := []struct {
    23  		name      string
    24  		value     interface{}
    25  		wantError bool
    26  		check     func(Options) bool
    27  	}{
    28  		{
    29  			name:  "symbolStyle",
    30  			value: "Dynamic",
    31  			check: func(o Options) bool { return o.SymbolStyle == DynamicSymbols },
    32  		},
    33  		{
    34  			name:      "symbolStyle",
    35  			value:     "",
    36  			wantError: true,
    37  			check:     func(o Options) bool { return o.SymbolStyle == "" },
    38  		},
    39  		{
    40  			name:      "symbolStyle",
    41  			value:     false,
    42  			wantError: true,
    43  			check:     func(o Options) bool { return o.SymbolStyle == "" },
    44  		},
    45  		{
    46  			name:  "symbolMatcher",
    47  			value: "caseInsensitive",
    48  			check: func(o Options) bool { return o.SymbolMatcher == SymbolCaseInsensitive },
    49  		},
    50  		{
    51  			name:  "completionBudget",
    52  			value: "2s",
    53  			check: func(o Options) bool { return o.CompletionBudget == 2*time.Second },
    54  		},
    55  		{
    56  			name:      "staticcheck",
    57  			value:     true,
    58  			check:     func(o Options) bool { return o.Staticcheck == true },
    59  			wantError: true, // o.StaticcheckSupported is unset
    60  		},
    61  		{
    62  			name:  "codelenses",
    63  			value: map[string]interface{}{"generate": true},
    64  			check: func(o Options) bool { return o.Codelenses["generate"] },
    65  		},
    66  		{
    67  			name:  "allExperiments",
    68  			value: true,
    69  			check: func(o Options) bool {
    70  				return true // just confirm that we handle this setting
    71  			},
    72  		},
    73  		{
    74  			name:  "hoverKind",
    75  			value: "FullDocumentation",
    76  			check: func(o Options) bool {
    77  				return o.HoverKind == FullDocumentation
    78  			},
    79  		},
    80  		{
    81  			name:  "hoverKind",
    82  			value: "NoDocumentation",
    83  			check: func(o Options) bool {
    84  				return o.HoverKind == NoDocumentation
    85  			},
    86  		},
    87  		{
    88  			name:  "hoverKind",
    89  			value: "SingleLine",
    90  			check: func(o Options) bool {
    91  				return o.HoverKind == SingleLine
    92  			},
    93  		},
    94  		{
    95  			name:  "hoverKind",
    96  			value: "Structured",
    97  			check: func(o Options) bool {
    98  				return o.HoverKind == Structured
    99  			},
   100  		},
   101  		{
   102  			name:  "ui.documentation.hoverKind",
   103  			value: "Structured",
   104  			check: func(o Options) bool {
   105  				return o.HoverKind == Structured
   106  			},
   107  		},
   108  		{
   109  			name:  "matcher",
   110  			value: "Fuzzy",
   111  			check: func(o Options) bool {
   112  				return o.Matcher == Fuzzy
   113  			},
   114  		},
   115  		{
   116  			name:  "matcher",
   117  			value: "CaseSensitive",
   118  			check: func(o Options) bool {
   119  				return o.Matcher == CaseSensitive
   120  			},
   121  		},
   122  		{
   123  			name:  "matcher",
   124  			value: "CaseInsensitive",
   125  			check: func(o Options) bool {
   126  				return o.Matcher == CaseInsensitive
   127  			},
   128  		},
   129  		{
   130  			name:  "env",
   131  			value: map[string]interface{}{"testing": "true"},
   132  			check: func(o Options) bool {
   133  				v, found := o.Env["testing"]
   134  				return found && v == "true"
   135  			},
   136  		},
   137  		{
   138  			name:      "env",
   139  			value:     []string{"invalid", "input"},
   140  			wantError: true,
   141  			check: func(o Options) bool {
   142  				return o.Env == nil
   143  			},
   144  		},
   145  		{
   146  			name:  "directoryFilters",
   147  			value: []interface{}{"-node_modules", "+project_a"},
   148  			check: func(o Options) bool {
   149  				return len(o.DirectoryFilters) == 2
   150  			},
   151  		},
   152  		{
   153  			name:      "directoryFilters",
   154  			value:     []interface{}{"invalid"},
   155  			wantError: true,
   156  			check: func(o Options) bool {
   157  				return len(o.DirectoryFilters) == 0
   158  			},
   159  		},
   160  		{
   161  			name:      "directoryFilters",
   162  			value:     []string{"-invalid", "+type"},
   163  			wantError: true,
   164  			check: func(o Options) bool {
   165  				return len(o.DirectoryFilters) == 0
   166  			},
   167  		},
   168  		{
   169  			name: "annotations",
   170  			value: map[string]interface{}{
   171  				"Nil":      false,
   172  				"noBounds": true,
   173  			},
   174  			wantError: true,
   175  			check: func(o Options) bool {
   176  				return !o.Annotations[Nil] && !o.Annotations[Bounds]
   177  			},
   178  		},
   179  		{
   180  			name:      "vulncheck",
   181  			value:     []interface{}{"invalid"},
   182  			wantError: true,
   183  			check: func(o Options) bool {
   184  				return o.Vulncheck == "" // For invalid value, default to 'off'.
   185  			},
   186  		},
   187  		{
   188  			name:  "vulncheck",
   189  			value: "Imports",
   190  			check: func(o Options) bool {
   191  				return o.Vulncheck == ModeVulncheckImports // For invalid value, default to 'off'.
   192  			},
   193  		},
   194  		{
   195  			name:  "vulncheck",
   196  			value: "imports",
   197  			check: func(o Options) bool {
   198  				return o.Vulncheck == ModeVulncheckImports
   199  			},
   200  		},
   201  	}
   202  
   203  	for _, test := range tests {
   204  		var opts Options
   205  		result := opts.set(test.name, test.value, map[string]struct{}{})
   206  		if (result.Error != nil) != test.wantError {
   207  			t.Fatalf("Options.set(%q, %v): result.Error = %v, want error: %t", test.name, test.value, result.Error, test.wantError)
   208  		}
   209  		// TODO: this could be made much better using cmp.Diff, if that becomes
   210  		// available in this module.
   211  		if !test.check(opts) {
   212  			t.Errorf("Options.set(%q, %v): unexpected result %+v", test.name, test.value, opts)
   213  		}
   214  	}
   215  }