|
| 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 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "go.lsp.dev/protocol" |
| 24 | +) |
| 25 | + |
| 26 | +func TestHover(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + // if runtime.GOOS == "windows" { |
| 30 | + // t.Skip("Skipping on Windows") |
| 31 | + // } |
| 32 | + |
| 33 | + ctx := t.Context() |
| 34 | + |
| 35 | + testProtoPath, err := filepath.Abs("testdata/hover/test.proto") |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + clientJSONConn, testURI := setupLSPServer(t, testProtoPath) |
| 39 | + |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + line uint32 |
| 43 | + character uint32 |
| 44 | + expectedContains string |
| 45 | + expectNoHover bool |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "hover_on_user_message", |
| 49 | + line: 7, // Line with "message User {" |
| 50 | + character: 8, // On the word "User" |
| 51 | + expectedContains: "User represents a user in the system", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "hover_on_id_field", |
| 55 | + line: 9, // Line with "string id = 1;" |
| 56 | + character: 10, // On the word "id" |
| 57 | + expectedContains: "The unique identifier for the user", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "hover_on_status_enum", |
| 61 | + line: 19, // Line with "enum Status {" |
| 62 | + character: 5, // On the word "Status" |
| 63 | + expectedContains: "Status represents the current state of a user", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "hover_on_status_active", |
| 67 | + line: 24, // Line with "STATUS_ACTIVE = 1;" |
| 68 | + character: 2, // On "STATUS_ACTIVE" |
| 69 | + expectedContains: "The user is active", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "hover_on_user_service", |
| 73 | + line: 31, // Line with "service UserService {" |
| 74 | + character: 8, // On "UserService" |
| 75 | + expectedContains: "UserService provides operations for managing users", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "hover_on_get_user_rpc", |
| 79 | + line: 33, // Line with "rpc GetUser" |
| 80 | + character: 6, // On "GetUser" |
| 81 | + expectedContains: "GetUser retrieves a user by their ID", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "hover_on_status_type_reference", |
| 85 | + line: 15, // Line with "Status status = 3;" |
| 86 | + character: 2, // On "Status" type |
| 87 | + expectedContains: "Status represents the current state of a user", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "hover_on_user_type_reference", |
| 91 | + line: 45, // Line with "User user = 1;" |
| 92 | + character: 2, // On "User" type |
| 93 | + expectedContains: "User represents a user in the system", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "hover_on_rpc_request_type", |
| 97 | + line: 33, // Line with "rpc GetUser(GetUserRequest)" |
| 98 | + character: 14, // On "GetUserRequest" |
| 99 | + expectedContains: "GetUserRequest is the request message for GetUser", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "hover_on_rpc_response_type", |
| 103 | + line: 33, // Line with "returns (GetUserResponse)" |
| 104 | + character: 39, // On "GetUserResponse" |
| 105 | + expectedContains: "GetUserResponse is the response message for GetUser", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "hover_on_syntax_keyword", |
| 109 | + line: 0, // Line with "syntax = "proto3";" |
| 110 | + character: 0, // On "syntax" |
| 111 | + expectNoHover: true, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "hover_on_proto3_string", |
| 115 | + line: 0, // Line with "syntax = "proto3";" |
| 116 | + character: 10, // On "proto3" |
| 117 | + expectNoHover: true, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "hover_on_package_keyword", |
| 121 | + line: 2, // Line with "package example.v1;" |
| 122 | + character: 0, // On "package" |
| 123 | + expectNoHover: true, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "hover_on_package_name", |
| 127 | + line: 2, // Line with "package example.v1;" |
| 128 | + character: 8, // On "example" |
| 129 | + expectNoHover: true, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tt := range tests { |
| 134 | + t.Run(tt.name, func(t *testing.T) { |
| 135 | + t.Parallel() |
| 136 | + var hover *protocol.Hover |
| 137 | + _, hoverErr := clientJSONConn.Call(ctx, protocol.MethodTextDocumentHover, protocol.HoverParams{ |
| 138 | + TextDocumentPositionParams: protocol.TextDocumentPositionParams{ |
| 139 | + TextDocument: protocol.TextDocumentIdentifier{ |
| 140 | + URI: testURI, |
| 141 | + }, |
| 142 | + Position: protocol.Position{ |
| 143 | + Line: tt.line, |
| 144 | + Character: tt.character, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, &hover) |
| 148 | + require.NoError(t, hoverErr) |
| 149 | + |
| 150 | + if tt.expectNoHover { |
| 151 | + assert.Nil(t, hover, "expected no hover information") |
| 152 | + } else if tt.expectedContains != "" { |
| 153 | + require.NotNil(t, hover, "expected hover to be non-nil") |
| 154 | + assert.Equal(t, protocol.Markdown, hover.Contents.Kind) |
| 155 | + assert.Contains(t, hover.Contents.Value, tt.expectedContains) |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
0 commit comments