forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcceptFullSuggestionHandlerTests.java
More file actions
82 lines (67 loc) · 3.83 KB
/
Copy pathAcceptFullSuggestionHandlerTests.java
File metadata and controls
82 lines (67 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.handler;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.eclipse.core.commands.ExecutionException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import com.microsoft.copilot.eclipse.core.CopilotCore;
import com.microsoft.copilot.eclipse.core.completion.SuggestionUpdateManager;
import com.microsoft.copilot.eclipse.core.lsp.CopilotLanguageServerConnection;
import com.microsoft.copilot.eclipse.core.lsp.protocol.CompletionItem;
import com.microsoft.copilot.eclipse.ui.CopilotUi;
import com.microsoft.copilot.eclipse.ui.completion.CompletionManager;
import com.microsoft.copilot.eclipse.ui.completion.EditorsManager;
import com.microsoft.copilot.eclipse.ui.handlers.AcceptFullSuggestionHandler;
@ExtendWith(MockitoExtension.class)
class AcceptFullSuggestionHandlerTests {
@Test
void testIsNotEnabledWhenNoCompletionIsAvailable() {
CopilotUi mockedUi = mock(CopilotUi.class);
EditorsManager mockedManager = mock(EditorsManager.class);
CompletionManager mockedCompletionManager = mock(CompletionManager.class);
SuggestionUpdateManager mockedSuggestionUpdateManager = mock(SuggestionUpdateManager.class);
when(mockedCompletionManager.getSuggestionUpdateManager()).thenReturn(mockedSuggestionUpdateManager);
when(mockedUi.getEditorsManager()).thenReturn(mockedManager);
when(mockedManager.getActiveCompletionManager()).thenReturn(mockedCompletionManager);
when(mockedSuggestionUpdateManager.getCurrentItem()).thenReturn(null);
AcceptFullSuggestionHandler handler = new AcceptFullSuggestionHandler();
try (MockedStatic<CopilotUi> mockedStatic = mockStatic(CopilotUi.class)) {
mockedStatic.when(CopilotUi::getPlugin).thenReturn(mockedUi);
assertFalse(handler.isEnabled());
}
}
@Test
void testAcceptionNotifiedWhenCompletionIsAccepted() throws ExecutionException {
CopilotLanguageServerConnection mockedConnection = mock(CopilotLanguageServerConnection.class);
when(mockedConnection.notifyAccepted(any())).thenReturn(null);
CopilotCore mockedCore = mock(CopilotCore.class);
when(mockedCore.getCopilotLanguageServer()).thenReturn(mockedConnection);
CompletionItem item = new CompletionItem("uuid", "text", null, "displayText", null, 0);
CompletionManager mockedCompletionManager = mock(CompletionManager.class);
doNothing().when(mockedCompletionManager).acceptSuggestion(any());
SuggestionUpdateManager mockedSuggestionUpdateManager = mock(SuggestionUpdateManager.class);
when(mockedSuggestionUpdateManager.getCurrentItem()).thenReturn(item);
when(mockedCompletionManager.getSuggestionUpdateManager()).thenReturn(mockedSuggestionUpdateManager);
EditorsManager mockedManager = mock(EditorsManager.class);
when(mockedManager.getActiveCompletionManager()).thenReturn(mockedCompletionManager);
CopilotUi mockedUi = mock(CopilotUi.class);
when(mockedUi.getEditorsManager()).thenReturn(mockedManager);
AcceptFullSuggestionHandler handler = new AcceptFullSuggestionHandler();
try (MockedStatic<CopilotUi> mockedStatic = mockStatic(CopilotUi.class);
MockedStatic<CopilotCore> mockedStaticCore = mockStatic(CopilotCore.class)) {
mockedStatic.when(CopilotUi::getPlugin).thenReturn(mockedUi);
mockedStaticCore.when(CopilotCore::getPlugin).thenReturn(mockedCore);
handler.execute(null);
verify(mockedConnection).notifyAccepted(any());
}
}
}