clerk/internal/lsp/config_test.go (view raw)
| 1 | package lsp |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "go.lsp.dev/protocol" |
| 7 | ) |
| 8 | |
| 9 | func TestConfigMerge(t *testing.T) { |
| 10 | tests := map[string]struct { |
| 11 | cfg Config |
| 12 | in string |
| 13 | want Config |
| 14 | }{ |
| 15 | "if not set, uses default": {Config{}, `{"semanticHighlighting": true}`, Config{SemanticHighlighting: true}}, |
| 16 | "empty object keeps defaults": {DefaultConfig, `{}`, Config{SemanticHighlighting: true}}, |
| 17 | "nil settings keep defaults": {DefaultConfig, "", Config{SemanticHighlighting: true}}, |
| 18 | |
| 19 | "disables provided option": {DefaultConfig, `{"semanticHighlighting": false}`, Config{SemanticHighlighting: false}}, |
| 20 | "snake_case key": {DefaultConfig, `{"semantic_highlighting": false}`, Config{SemanticHighlighting: false}}, |
| 21 | "kebab-case key": {DefaultConfig, `{"semantic-highlighting": false}`, Config{SemanticHighlighting: false}}, |
| 22 | |
| 23 | "malformed settings ignored": {Config{SemanticHighlighting: true}, `{`, Config{SemanticHighlighting: true}}, |
| 24 | "non-object settings ignored": {Config{SemanticHighlighting: true}, `"clerk"`, Config{SemanticHighlighting: true}}, |
| 25 | "unknown fields ignored": {Config{SemanticHighlighting: true}, `{"lint": true}`, Config{SemanticHighlighting: true}}, |
| 26 | } |
| 27 | for name, tt := range tests { |
| 28 | t.Run(name, func(t *testing.T) { |
| 29 | tt.cfg.merge(protocol.LSPAny(tt.in)) |
| 30 | if tt.cfg != tt.want { |
| 31 | t.Errorf("merge(%q) = %+v, want %+v", tt.in, tt.cfg, tt.want) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestInitialize_AppliesInitializationOptions(t *testing.T) { |
| 38 | srv := NewServer("test") |
| 39 | res, err := srv.server.Initialize(t.Context(), &protocol.InitializeParams{ |
| 40 | InitializationOptions: protocol.LSPAny(`{"semanticHighlighting": false}`), |
| 41 | }) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if srv.server.semanticHighlightingEnabled() { |
| 46 | t.Error("semanticHighlighting=false in initializationOptions not applied") |
| 47 | } |
| 48 | if res.Capabilities.SemanticTokensProvider == nil { |
| 49 | t.Error("SemanticTokensProvider must always be advertised") |
| 50 | } |
| 51 | } |