github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/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  			wantError: true, // o.StaticcheckSupported is unset
    51  		},
    52  		{
    53  			name:  "codelenses",
    54  			value: map[string]interface{}{"generate": true},
    55  			check: func(o Options) bool { return o.Codelenses["generate"] },
    56  		},
    57  		{
    58  			name:  "allExperiments",
    59  			value: true,
    60  			check: func(o Options) bool {
    61  				return true // just confirm that we handle this setting
    62  			},
    63  		},
    64  		{
    65  			name:  "hoverKind",
    66  			value: "FullDocumentation",
    67  			check: func(o Options) bool {
    68  				return o.HoverKind == FullDocumentation
    69  			},
    70  		},
    71  		{
    72  			name:  "hoverKind",
    73  			value: "NoDocumentation",
    74  			check: func(o Options) bool {
    75  				return o.HoverKind == NoDocumentation
    76  			},
    77  		},
    78  		{
    79  			name:  "hoverKind",
    80  			value: "SingleLine",
    81  			check: func(o Options) bool {
    82  				return o.HoverKind == SingleLine
    83  			},
    84  		},
    85  		{
    86  			name:  "hoverKind",
    87  			value: "Structured",
    88  			check: func(o Options) bool {
    89  				return o.HoverKind == Structured
    90  			},
    91  		},
    92  		{
    93  			name:  "ui.documentation.hoverKind",
    94  			value: "Structured",
    95  			check: func(o Options) bool {
    96  				return o.HoverKind == Structured
    97  			},
    98  		},
    99  		{
   100  			name:  "matcher",
   101  			value: "Fuzzy",
   102  			check: func(o Options) bool {
   103  				return o.Matcher == Fuzzy
   104  			},
   105  		},
   106  		{
   107  			name:  "matcher",
   108  			value: "CaseSensitive",
   109  			check: func(o Options) bool {
   110  				return o.Matcher == CaseSensitive
   111  			},
   112  		},
   113  		{
   114  			name:  "matcher",
   115  			value: "CaseInsensitive",
   116  			check: func(o Options) bool {
   117  				return o.Matcher == CaseInsensitive
   118  			},
   119  		},
   120  		{
   121  			name:  "env",
   122  			value: map[string]interface{}{"testing": "true"},
   123  			check: func(o Options) bool {
   124  				v, found := o.Env["testing"]
   125  				return found && v == "true"
   126  			},
   127  		},
   128  		{
   129  			name:      "env",
   130  			value:     []string{"invalid", "input"},
   131  			wantError: true,
   132  			check: func(o Options) bool {
   133  				return o.Env == nil
   134  			},
   135  		},
   136  		{
   137  			name:  "directoryFilters",
   138  			value: []interface{}{"-node_modules", "+project_a"},
   139  			check: func(o Options) bool {
   140  				return len(o.DirectoryFilters) == 2
   141  			},
   142  		},
   143  		{
   144  			name:      "directoryFilters",
   145  			value:     []interface{}{"invalid"},
   146  			wantError: true,
   147  			check: func(o Options) bool {
   148  				return len(o.DirectoryFilters) == 0
   149  			},
   150  		},
   151  		{
   152  			name:      "directoryFilters",
   153  			value:     []string{"-invalid", "+type"},
   154  			wantError: true,
   155  			check: func(o Options) bool {
   156  				return len(o.DirectoryFilters) == 0
   157  			},
   158  		},
   159  		{
   160  			name: "annotations",
   161  			value: map[string]interface{}{
   162  				"Nil":      false,
   163  				"noBounds": true,
   164  			},
   165  			wantError: true,
   166  			check: func(o Options) bool {
   167  				return !o.Annotations[Nil] && !o.Annotations[Bounds]
   168  			},
   169  		},
   170  	}
   171  
   172  	for _, test := range tests {
   173  		var opts Options
   174  		result := opts.set(test.name, test.value, map[string]struct{}{})
   175  		if (result.Error != nil) != test.wantError {
   176  			t.Fatalf("Options.set(%q, %v): result.Error = %v, want error: %t", test.name, test.value, result.Error, test.wantError)
   177  		}
   178  		// TODO: this could be made much better using cmp.Diff, if that becomes
   179  		// available in this module.
   180  		if !test.check(opts) {
   181  			t.Errorf("Options.set(%q, %v): unexpected result %+v", test.name, test.value, opts)
   182  		}
   183  	}
   184  }