|
| 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 TestCompletion(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + ctx := t.Context() |
| 30 | + |
| 31 | + testProtoPath, err := filepath.Abs("testdata/completion/test.proto") |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + clientJSONConn, testURI := setupLSPServer(t, testProtoPath) |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + line uint32 |
| 39 | + character uint32 |
| 40 | + expectedContains []string |
| 41 | + expectedNotContains []string |
| 42 | + expectNoCompletions bool |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "complete_builtin_toplevel", |
| 46 | + line: 5, |
| 47 | + character: 1, // After the "m" |
| 48 | + expectedContains: []string{"message"}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "complete_message_field_types", |
| 52 | + line: 9, // Empty line in User message where field would go |
| 53 | + character: 2, // Indented position where field type would be |
| 54 | + expectedContains: []string{"string", "int32", "int64", "bool", "bytes", "User", "GetUserRequest", "GetUserResponse"}, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "complete_builtin_service", |
| 58 | + line: 14, |
| 59 | + character: 2, // Indented position where "rpc" would be |
| 60 | + expectedContains: []string{"rpc", "option"}, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "complete_rpc_request_type", |
| 64 | + line: 13, |
| 65 | + character: uint32(len(" rpc GetUser(Get") - 1), |
| 66 | + expectedContains: []string{"GetUserRequest", "GetUserResponse"}, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "complete_rpc_response_type", |
| 70 | + line: 13, |
| 71 | + character: uint32(len(" rpc GetUser(Get) returns (Get") - 1), |
| 72 | + expectedContains: []string{"GetUserRequest", "GetUserResponse"}, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + var completionList *protocol.CompletionList |
| 80 | + _, completionErr := clientJSONConn.Call(ctx, protocol.MethodTextDocumentCompletion, protocol.CompletionParams{ |
| 81 | + TextDocumentPositionParams: protocol.TextDocumentPositionParams{ |
| 82 | + TextDocument: protocol.TextDocumentIdentifier{ |
| 83 | + URI: testURI, |
| 84 | + }, |
| 85 | + Position: protocol.Position{ |
| 86 | + Line: tt.line, |
| 87 | + Character: tt.character, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, &completionList) |
| 91 | + require.NoError(t, completionErr) |
| 92 | + if tt.expectNoCompletions { |
| 93 | + assert.Nil(t, completionList, "expected no completions") |
| 94 | + return |
| 95 | + } |
| 96 | + require.NotNil(t, completionList, "expected completion list to be non-nil") |
| 97 | + labels := make([]string, 0, len(completionList.Items)) |
| 98 | + for _, item := range completionList.Items { |
| 99 | + labels = append(labels, item.Label) |
| 100 | + } |
| 101 | + for _, expected := range tt.expectedContains { |
| 102 | + assert.Contains(t, labels, expected, "expected completion list to contain %q", expected) |
| 103 | + } |
| 104 | + for _, notExpected := range tt.expectedNotContains { |
| 105 | + assert.NotContains(t, labels, notExpected, "expected completion list to not contain %q", notExpected) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments