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