|
| 1 | +// Copyright 2020-2025 Buf Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package buflsp_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "path/filepath" |
| 19 | + "slices" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "go.lsp.dev/protocol" |
| 25 | +) |
| 26 | + |
| 27 | +func TestDocumentSymbol(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + ctx := t.Context() |
| 31 | + |
| 32 | + testProtoPath, err := filepath.Abs("testdata/symbols/symbols.proto") |
| 33 | + require.NoError(t, err) |
| 34 | + |
| 35 | + clientJSONConn, testURI := setupLSPServer(t, testProtoPath) |
| 36 | + |
| 37 | + type symbolInfo struct { |
| 38 | + name string |
| 39 | + kind protocol.SymbolKind |
| 40 | + line uint32 |
| 41 | + deprecated bool |
| 42 | + } |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + expectedSymbols []symbolInfo |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "all_document_symbols", |
| 49 | + expectedSymbols: []symbolInfo{ |
| 50 | + {name: "symbols.v1.Document", kind: protocol.SymbolKindClass, line: 4}, // message Document |
| 51 | + {name: "symbols.v1.Document.id", kind: protocol.SymbolKindField, line: 5}, // string id |
| 52 | + {name: "symbols.v1.Document.title", kind: protocol.SymbolKindField, line: 6}, // string title |
| 53 | + {name: "symbols.v1.Document.status", kind: protocol.SymbolKindField, line: 7}, // Status status |
| 54 | + {name: "symbols.v1.Document.metadata", kind: protocol.SymbolKindField, line: 8}, // Metadata metadata |
| 55 | + {name: "symbols.v1.Document.Metadata", kind: protocol.SymbolKindClass, line: 9}, // message Metadata (nested) |
| 56 | + {name: "symbols.v1.Document.Metadata.author", kind: protocol.SymbolKindField, line: 10}, // string author |
| 57 | + {name: "symbols.v1.Document.Metadata.created_at", kind: protocol.SymbolKindField, line: 11}, // int64 created_at |
| 58 | + {name: "symbols.v1.Status", kind: protocol.SymbolKindEnum, line: 15}, // enum Status |
| 59 | + {name: "symbols.v1.STATUS_UNSPECIFIED", kind: protocol.SymbolKindEnumMember, line: 16}, // STATUS_UNSPECIFIED = 0 |
| 60 | + {name: "symbols.v1.STATUS_DRAFT", kind: protocol.SymbolKindEnumMember, line: 17}, // STATUS_DRAFT = 1 |
| 61 | + {name: "symbols.v1.STATUS_PUBLISHED", kind: protocol.SymbolKindEnumMember, line: 18}, // STATUS_PUBLISHED = 2 |
| 62 | + {name: "symbols.v1.DocumentService", kind: protocol.SymbolKindInterface, line: 21}, // service DocumentService |
| 63 | + {name: "symbols.v1.DocumentService.GetDocument", kind: protocol.SymbolKindMethod, line: 22}, // rpc GetDocument |
| 64 | + {name: "symbols.v1.DocumentService.CreateDocument", kind: protocol.SymbolKindMethod, line: 23}, // rpc CreateDocument |
| 65 | + {name: "symbols.v1.GetDocumentRequest", kind: protocol.SymbolKindClass, line: 26}, // message GetDocumentRequest |
| 66 | + {name: "symbols.v1.GetDocumentRequest.document_id", kind: protocol.SymbolKindField, line: 27}, // string document_id |
| 67 | + {name: "symbols.v1.GetDocumentResponse", kind: protocol.SymbolKindClass, line: 30}, // message GetDocumentResponse |
| 68 | + {name: "symbols.v1.GetDocumentResponse.document", kind: protocol.SymbolKindField, line: 31}, // Document document |
| 69 | + {name: "symbols.v1.CreateDocumentRequest", kind: protocol.SymbolKindClass, line: 34}, // message CreateDocumentRequest |
| 70 | + {name: "symbols.v1.CreateDocumentRequest.document", kind: protocol.SymbolKindField, line: 35}, // Document document |
| 71 | + {name: "symbols.v1.CreateDocumentResponse", kind: protocol.SymbolKindClass, line: 38}, // message CreateDocumentResponse |
| 72 | + {name: "symbols.v1.CreateDocumentResponse.document", kind: protocol.SymbolKindField, line: 39}, // Document document |
| 73 | + {name: "symbols.v1.LegacyDocument", kind: protocol.SymbolKindClass, line: 42, deprecated: true}, // message LegacyDocument (deprecated) |
| 74 | + {name: "symbols.v1.LegacyDocument.id", kind: protocol.SymbolKindField, line: 44}, // string id |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + |
| 83 | + var symbols []protocol.SymbolInformation |
| 84 | + _, symErr := clientJSONConn.Call(ctx, protocol.MethodTextDocumentDocumentSymbol, protocol.DocumentSymbolParams{ |
| 85 | + TextDocument: protocol.TextDocumentIdentifier{ |
| 86 | + URI: testURI, |
| 87 | + }, |
| 88 | + }, &symbols) |
| 89 | + require.NoError(t, symErr) |
| 90 | + |
| 91 | + require.Len(t, symbols, len(tt.expectedSymbols)) |
| 92 | + |
| 93 | + for _, expectedSymbol := range tt.expectedSymbols { |
| 94 | + idx := slices.IndexFunc(symbols, func(s protocol.SymbolInformation) bool { |
| 95 | + return s.Name == expectedSymbol.name |
| 96 | + }) |
| 97 | + require.NotEqual(t, -1, idx, "expected to find symbol %s", expectedSymbol.name) |
| 98 | + found := symbols[idx] |
| 99 | + assert.Equal(t, expectedSymbol.kind, found.Kind, "symbol %s has wrong kind", expectedSymbol.name) |
| 100 | + assert.Equal(t, testURI, found.Location.URI, "symbol %s has wrong URI", expectedSymbol.name) |
| 101 | + assert.Equal(t, expectedSymbol.line, found.Location.Range.Start.Line, "symbol %s has wrong line number", expectedSymbol.name) |
| 102 | + assert.Equal(t, expectedSymbol.deprecated, found.Deprecated, "symbol %s has wrong deprecated status", expectedSymbol.name) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
0 commit comments