github.com/jd-ly/tools@v0.5.7/internal/lsp/source/options_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 source
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestSetOption(t *testing.T) {
    13  	tests := []struct {
    14  		name      string
    15  		value     interface{}
    16  		wantError bool
    17  		check     func(Options) bool
    18  	}{
    19  		{
    20  			name:  "symbolStyle",
    21  			value: "dynamic",
    22  			check: func(o Options) bool { return o.SymbolStyle == DynamicSymbols },
    23  		},
    24  		{
    25  			name:      "symbolStyle",
    26  			value:     "",
    27  			wantError: true,
    28  			check:     func(o Options) bool { return o.SymbolStyle == "" },
    29  		},
    30  		{
    31  			name:      "symbolStyle",
    32  			value:     false,
    33  			wantError: true,
    34  			check:     func(o Options) bool { return o.SymbolStyle == "" },
    35  		},
    36  		{
    37  			name:  "symbolMatcher",
    38  			value: "caseInsensitive",
    39  			check: func(o Options) bool { return o.SymbolMatcher == SymbolCaseInsensitive },
    40  		},
    41  		{
    42  			name:  "completionBudget",
    43  			value: "2s",
    44  			check: func(o Options) bool { return o.CompletionBudget == 2*time.Second },
    45  		},
    46  		{
    47  			name:  "staticcheck",
    48  			value: true,
    49  			check: func(o Options) bool { return o.Staticcheck == true },
    50  		},
    51  		{
    52  			name:  "codelenses",
    53  			value: map[string]interface{}{"generate": true},
    54  			check: func(o Options) bool { return o.Codelenses["generate"] },
    55  		},
    56  		{
    57  			name:  "allExperiments",
    58  			value: true,
    59  			check: func(o Options) bool {
    60  				return true // just confirm that we handle this setting
    61  			},
    62  		},
    63  	}
    64  
    65  	for _, test := range tests {
    66  		var opts Options
    67  		result := opts.set(test.name, test.value)
    68  		if (result.Error != nil) != test.wantError {
    69  			t.Fatalf("Options.set(%q, %v): result.Error = %v, want error: %t", test.name, test.value, result.Error, test.wantError)
    70  		}
    71  		// TODO: this could be made much better using cmp.Diff, if that becomes
    72  		// available in this module.
    73  		if !test.check(opts) {
    74  			t.Errorf("Options.set(%q, %v): unexpected result %+v", test.name, test.value, opts)
    75  		}
    76  	}
    77  }