|
| 1 | +package lsutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestUserConfig_GetPreferences(t *testing.T) { |
| 8 | + t.Parallel() |
| 9 | + defaultPref := NewDefaultUserPreferences() |
| 10 | + |
| 11 | + type expectedPreference int |
| 12 | + |
| 13 | + const ( |
| 14 | + expectedPreferenceTS expectedPreference = iota |
| 15 | + expectedPreferenceJS |
| 16 | + expectedPreferenceDefault |
| 17 | + ) |
| 18 | + |
| 19 | + doubleQuotePrefs := &UserPreferences{ |
| 20 | + QuotePreference: "double", |
| 21 | + } |
| 22 | + singleQuotePrefs := &UserPreferences{ |
| 23 | + QuotePreference: "single", |
| 24 | + } |
| 25 | + |
| 26 | + tsDoubleQuoteJsSingleQuoteConfig := &UserConfig{ |
| 27 | + ts: doubleQuotePrefs, |
| 28 | + js: singleQuotePrefs, |
| 29 | + } |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + config *UserConfig |
| 33 | + activeFile string |
| 34 | + expectedPref expectedPreference |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: ".ts file returns TS preferences", |
| 38 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 39 | + activeFile: "file.ts", |
| 40 | + expectedPref: expectedPreferenceTS, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: ".tsx file returns TS preferences", |
| 44 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 45 | + activeFile: "file.tsx", |
| 46 | + expectedPref: expectedPreferenceTS, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: ".d.ts file returns TS preferences", |
| 50 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 51 | + activeFile: "file.d.ts", |
| 52 | + expectedPref: expectedPreferenceTS, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: ".mts file returns TS preferences", |
| 56 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 57 | + activeFile: "file.mts", |
| 58 | + expectedPref: expectedPreferenceTS, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: ".cts file returns TS preferences", |
| 62 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 63 | + activeFile: "file.cts", |
| 64 | + expectedPref: expectedPreferenceTS, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: ".js file returns JS preferences", |
| 68 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 69 | + activeFile: "file.js", |
| 70 | + expectedPref: expectedPreferenceJS, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: ".jsx file returns JS preferences", |
| 74 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 75 | + activeFile: "file.jsx", |
| 76 | + expectedPref: expectedPreferenceJS, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: ".mjs file returns JS preferences", |
| 80 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 81 | + activeFile: "file.mjs", |
| 82 | + expectedPref: expectedPreferenceJS, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: ".cjs file returns JS preferences", |
| 86 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 87 | + activeFile: "file.cjs", |
| 88 | + expectedPref: expectedPreferenceJS, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "Empty file returns TS preferences", |
| 92 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 93 | + activeFile: "", |
| 94 | + expectedPref: expectedPreferenceTS, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Unknown file extension returns JS preferences", |
| 98 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 99 | + activeFile: "file.py", |
| 100 | + expectedPref: expectedPreferenceJS, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: ".ts file with nil TS preferences falls back to JS", |
| 104 | + config: &UserConfig{ |
| 105 | + ts: nil, |
| 106 | + js: singleQuotePrefs, |
| 107 | + }, |
| 108 | + activeFile: "file.ts", |
| 109 | + expectedPref: expectedPreferenceJS, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: ".js file with nil JS preferences falls back to TS", |
| 113 | + config: &UserConfig{ |
| 114 | + ts: doubleQuotePrefs, |
| 115 | + js: nil, |
| 116 | + }, |
| 117 | + activeFile: "file.js", |
| 118 | + expectedPref: expectedPreferenceTS, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: ".ts file with both nil preferences returns default", |
| 122 | + config: &UserConfig{ |
| 123 | + ts: nil, |
| 124 | + js: nil, |
| 125 | + }, |
| 126 | + activeFile: "file.ts", |
| 127 | + expectedPref: expectedPreferenceDefault, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: ".js file with both nil preferences returns default", |
| 131 | + config: &UserConfig{ |
| 132 | + ts: nil, |
| 133 | + js: nil, |
| 134 | + }, |
| 135 | + activeFile: "file.js", |
| 136 | + expectedPref: expectedPreferenceDefault, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: ".ts file with deeper path returns TS preferences", |
| 140 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 141 | + activeFile: "path/to/file.ts", |
| 142 | + expectedPref: expectedPreferenceTS, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: ".js file with deeper path returns JS preferences", |
| 146 | + config: tsDoubleQuoteJsSingleQuoteConfig, |
| 147 | + activeFile: "path/to/file.js", |
| 148 | + expectedPref: expectedPreferenceJS, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + for _, tt := range tests { |
| 153 | + t.Run(tt.name, func(t *testing.T) { |
| 154 | + t.Parallel() |
| 155 | + got := tt.config.GetPreferences(tt.activeFile) |
| 156 | + |
| 157 | + switch tt.expectedPref { |
| 158 | + case expectedPreferenceDefault: |
| 159 | + // Compare with default preferences |
| 160 | + if got.QuotePreference != defaultPref.QuotePreference { |
| 161 | + t.Errorf("GetPreferences().QuotePreference was '%v', expected default %v", got.QuotePreference, defaultPref.QuotePreference) |
| 162 | + } |
| 163 | + case expectedPreferenceTS: |
| 164 | + if got.QuotePreference != tt.config.ts.QuotePreference { |
| 165 | + t.Errorf("GetPreferences().QuotePreference was '%v', expected TS preference %v", got.QuotePreference, tt.config.ts.QuotePreference) |
| 166 | + } |
| 167 | + case expectedPreferenceJS: |
| 168 | + if got.QuotePreference != tt.config.js.QuotePreference { |
| 169 | + t.Errorf("GetPreferences().QuotePreference was '%v', expected JS preference %v", got.QuotePreference, tt.config.js.QuotePreference) |
| 170 | + } |
| 171 | + } |
| 172 | + }) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func TestUserConfig_GetPreferences_CodeLensAndInlayHints(t *testing.T) { |
| 177 | + t.Parallel() |
| 178 | + codeLensAndInlayHintsOn := &UserPreferences{ |
| 179 | + CodeLens: CodeLensUserPreferences{ |
| 180 | + ReferencesCodeLensEnabled: true, |
| 181 | + }, |
| 182 | + InlayHints: InlayHintsPreferences{ |
| 183 | + IncludeInlayVariableTypeHints: true, |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + codeLensAndInlayHintsOff := &UserPreferences{ |
| 188 | + CodeLens: CodeLensUserPreferences{ |
| 189 | + ReferencesCodeLensEnabled: false, |
| 190 | + }, |
| 191 | + InlayHints: InlayHintsPreferences{ |
| 192 | + IncludeInlayVariableTypeHints: false, |
| 193 | + }, |
| 194 | + } |
| 195 | + |
| 196 | + tests := []struct { |
| 197 | + name string |
| 198 | + config *UserConfig |
| 199 | + activeFile string |
| 200 | + expectedLensesAndHints bool |
| 201 | + }{ |
| 202 | + { |
| 203 | + name: ".ts file with CodeLens and InlayHints enabled", |
| 204 | + config: &UserConfig{ |
| 205 | + ts: codeLensAndInlayHintsOn, |
| 206 | + js: codeLensAndInlayHintsOff, |
| 207 | + }, |
| 208 | + activeFile: "file.ts", |
| 209 | + expectedLensesAndHints: true, |
| 210 | + }, |
| 211 | + { |
| 212 | + name: ".ts file with CodeLens and InlayHints disabled", |
| 213 | + config: &UserConfig{ |
| 214 | + ts: codeLensAndInlayHintsOn, |
| 215 | + js: codeLensAndInlayHintsOff, |
| 216 | + }, |
| 217 | + activeFile: "file.js", |
| 218 | + expectedLensesAndHints: false, |
| 219 | + }, |
| 220 | + { |
| 221 | + name: ".ts file with CodeLens and InlayHints disabled", |
| 222 | + config: &UserConfig{ |
| 223 | + ts: codeLensAndInlayHintsOff, |
| 224 | + js: codeLensAndInlayHintsOn, |
| 225 | + }, |
| 226 | + activeFile: "file.ts", |
| 227 | + expectedLensesAndHints: false, |
| 228 | + }, |
| 229 | + { |
| 230 | + name: ".js file with CodeLens and InlayHints disabled", |
| 231 | + config: &UserConfig{ |
| 232 | + ts: codeLensAndInlayHintsOn, |
| 233 | + js: codeLensAndInlayHintsOff, |
| 234 | + }, |
| 235 | + activeFile: "file.js", |
| 236 | + expectedLensesAndHints: false, |
| 237 | + }, |
| 238 | + } |
| 239 | + |
| 240 | + for _, tt := range tests { |
| 241 | + t.Run(tt.name, func(t *testing.T) { |
| 242 | + t.Parallel() |
| 243 | + got := tt.config.GetPreferences(tt.activeFile) |
| 244 | + |
| 245 | + if got.CodeLens.ReferencesCodeLensEnabled != tt.expectedLensesAndHints { |
| 246 | + t.Errorf("GetPreferences().CodeLens.ReferencesCodeLensEnabled was '%v', expected '%v'", got.CodeLens.ReferencesCodeLensEnabled, tt.expectedLensesAndHints) |
| 247 | + } |
| 248 | + if got.InlayHints.IncludeInlayVariableTypeHints != tt.expectedLensesAndHints { |
| 249 | + t.Errorf("GetPreferences().InlayHints.IncludeInlayVariableTypeHints was '%v', expected '%v'", got.InlayHints.IncludeInlayVariableTypeHints, tt.expectedLensesAndHints) |
| 250 | + } |
| 251 | + }) |
| 252 | + } |
| 253 | +} |
0 commit comments