| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package settings_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"reflect" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/google/go-cmp/cmp" |
| 13 |
"golang.org/x/tools/gopls/internal/clonetest" |
| 14 |
. "golang.org/x/tools/gopls/internal/settings" |
| 15 |
) |
| 16 |
|
| 17 |
func TestDefaultsEquivalence(t *testing.T) { |
| 18 |
opts1 := DefaultOptions() |
| 19 |
opts2 := DefaultOptions() |
| 20 |
if !reflect.DeepEqual(opts1, opts2) { |
| 21 |
t.Fatal("default options are not equivalent using reflect.DeepEqual") |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func TestOptions_Set(t *testing.T) { |
| 26 |
type testCase struct { |
| 27 |
name string |
| 28 |
value any |
| 29 |
wantError bool |
| 30 |
check func(Options) bool |
| 31 |
} |
| 32 |
tests := []testCase{ |
| 33 |
{ |
| 34 |
name: "symbolStyle", |
| 35 |
value: "Dynamic", |
| 36 |
check: func(o Options) bool { return o.SymbolStyle == DynamicSymbols }, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "symbolStyle", |
| 40 |
value: "", |
| 41 |
wantError: true, |
| 42 |
check: func(o Options) bool { return o.SymbolStyle == "" }, |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "symbolStyle", |
| 46 |
value: false, |
| 47 |
wantError: true, |
| 48 |
check: func(o Options) bool { return o.SymbolStyle == "" }, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "symbolMatcher", |
| 52 |
value: "caseInsensitive", |
| 53 |
check: func(o Options) bool { return o.SymbolMatcher == SymbolCaseInsensitive }, |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "completionBudget", |
| 57 |
value: "2s", |
| 58 |
check: func(o Options) bool { return o.CompletionBudget == 2*time.Second }, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "codelenses", |
| 62 |
value: map[string]any{"generate": true}, |
| 63 |
check: func(o Options) bool { return o.Codelenses["generate"] }, |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "allExperiments", |
| 67 |
value: true, |
| 68 |
check: func(o Options) bool { |
| 69 |
return true |
| 70 |
}, |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: "hoverKind", |
| 74 |
value: "FullDocumentation", |
| 75 |
check: func(o Options) bool { |
| 76 |
return o.HoverKind == FullDocumentation |
| 77 |
}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "hoverKind", |
| 81 |
value: "NoDocumentation", |
| 82 |
check: func(o Options) bool { |
| 83 |
return o.HoverKind == NoDocumentation |
| 84 |
}, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "hoverKind", |
| 88 |
value: "SingleLine", |
| 89 |
check: func(o Options) bool { |
| 90 |
return o.HoverKind == SingleLine |
| 91 |
}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "hoverKind", |
| 95 |
value: "Structured", |
| 96 |
|
| 97 |
check: func(o Options) bool { |
| 98 |
return o.HoverKind == Structured |
| 99 |
}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "ui.documentation.hoverKind", |
| 103 |
value: "Structured", |
| 104 |
|
| 105 |
check: func(o Options) bool { |
| 106 |
return o.HoverKind == Structured |
| 107 |
}, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "hoverKind", |
| 111 |
value: "FullDocumentation", |
| 112 |
check: func(o Options) bool { |
| 113 |
return o.HoverKind == FullDocumentation |
| 114 |
}, |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "ui.documentation.hoverKind", |
| 118 |
value: "FullDocumentation", |
| 119 |
check: func(o Options) bool { |
| 120 |
return o.HoverKind == FullDocumentation |
| 121 |
}, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "matcher", |
| 125 |
value: "Fuzzy", |
| 126 |
check: func(o Options) bool { |
| 127 |
return o.Matcher == Fuzzy |
| 128 |
}, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "matcher", |
| 132 |
value: "CaseSensitive", |
| 133 |
check: func(o Options) bool { |
| 134 |
return o.Matcher == CaseSensitive |
| 135 |
}, |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "matcher", |
| 139 |
value: "CaseInsensitive", |
| 140 |
check: func(o Options) bool { |
| 141 |
return o.Matcher == CaseInsensitive |
| 142 |
}, |
| 143 |
}, |
| 144 |
{ |
| 145 |
name: "env", |
| 146 |
value: map[string]any{"testing": "true"}, |
| 147 |
check: func(o Options) bool { |
| 148 |
v, found := o.Env["testing"] |
| 149 |
return found && v == "true" |
| 150 |
}, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "env", |
| 154 |
value: []string{"invalid", "input"}, |
| 155 |
wantError: true, |
| 156 |
check: func(o Options) bool { |
| 157 |
return o.Env == nil |
| 158 |
}, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "directoryFilters", |
| 162 |
value: []any{"-node_modules", "+project_a"}, |
| 163 |
check: func(o Options) bool { |
| 164 |
return len(o.DirectoryFilters) == 2 |
| 165 |
}, |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: "directoryFilters", |
| 169 |
value: []any{"invalid"}, |
| 170 |
wantError: true, |
| 171 |
check: func(o Options) bool { |
| 172 |
return len(o.DirectoryFilters) == 0 |
| 173 |
}, |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "directoryFilters", |
| 177 |
value: []string{"-invalid", "+type"}, |
| 178 |
wantError: true, |
| 179 |
check: func(o Options) bool { |
| 180 |
return len(o.DirectoryFilters) == 0 |
| 181 |
}, |
| 182 |
}, |
| 183 |
{ |
| 184 |
name: "annotations", |
| 185 |
value: map[string]any{ |
| 186 |
"Nil": false, |
| 187 |
"noBounds": true, |
| 188 |
}, |
| 189 |
wantError: true, |
| 190 |
check: func(o Options) bool { |
| 191 |
return !o.Annotations[Nil] && !o.Annotations[Bounds] |
| 192 |
}, |
| 193 |
}, |
| 194 |
{ |
| 195 |
name: "vulncheck", |
| 196 |
value: []any{"invalid"}, |
| 197 |
wantError: true, |
| 198 |
check: func(o Options) bool { |
| 199 |
return o.Vulncheck == "" |
| 200 |
}, |
| 201 |
}, |
| 202 |
{ |
| 203 |
name: "vulncheck", |
| 204 |
value: "Imports", |
| 205 |
check: func(o Options) bool { |
| 206 |
return o.Vulncheck == ModeVulncheckImports |
| 207 |
}, |
| 208 |
}, |
| 209 |
{ |
| 210 |
name: "vulncheck", |
| 211 |
value: "imports", |
| 212 |
check: func(o Options) bool { |
| 213 |
return o.Vulncheck == ModeVulncheckImports |
| 214 |
}, |
| 215 |
}, |
| 216 |
{ |
| 217 |
name: "vulncheck", |
| 218 |
value: "Prompt", |
| 219 |
check: func(o Options) bool { |
| 220 |
return o.Vulncheck == ModeVulncheckPrompt |
| 221 |
}, |
| 222 |
}, |
| 223 |
} |
| 224 |
|
| 225 |
for _, test := range tests { |
| 226 |
var opts Options |
| 227 |
_, err := opts.Set(map[string]any{test.name: test.value}) |
| 228 |
if err != nil { |
| 229 |
if !test.wantError { |
| 230 |
t.Errorf("Options.set(%q, %v) failed: %v", |
| 231 |
test.name, test.value, err) |
| 232 |
} |
| 233 |
continue |
| 234 |
} else if test.wantError { |
| 235 |
t.Fatalf("Options.set(%q, %v) succeeded unexpectedly", |
| 236 |
test.name, test.value) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
if !test.check(opts) { |
| 242 |
t.Errorf("Options.set(%q, %v): unexpected result %+v", test.name, test.value, opts) |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func TestOptions_Clone(t *testing.T) { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
golden := clonetest.NonZero[*Options]() |
| 252 |
opts := clonetest.NonZero[*Options]() |
| 253 |
opts2 := opts.Clone() |
| 254 |
|
| 255 |
|
| 256 |
if diff := cmp.Diff(golden, opts2); diff != "" { |
| 257 |
t.Errorf("Clone() does not match original (-want +got):\n%s", diff) |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
clonetest.ZeroOut(opts2) |
| 262 |
if diff := cmp.Diff(golden, opts); diff != "" { |
| 263 |
t.Errorf("Mutating clone mutated the original (-want +got):\n%s", diff) |
| 264 |
} |
| 265 |
} |