forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreferencesUtilsTest.java
More file actions
89 lines (69 loc) · 3.64 KB
/
Copy pathPreferencesUtilsTest.java
File metadata and controls
89 lines (69 loc) · 3.64 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
83
84
85
86
87
88
89
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.utils;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceStore;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import com.microsoft.copilot.eclipse.core.Constants;
import com.microsoft.copilot.eclipse.core.chat.CustomInstructionsChatLoadScope;
class PreferencesUtilsTest {
private IPreferenceStore store;
@BeforeEach
void setUp() {
store = new PreferenceStore();
store.setDefault(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE,
CustomInstructionsChatLoadScope.DEFAULT_VALUE.getValue());
}
@AfterEach
void tearDown() {
store = null;
}
@ParameterizedTest
@EnumSource(CustomInstructionsChatLoadScope.class)
void testGetCustomInstructionsChatLoadScope_returnsStoredValue(CustomInstructionsChatLoadScope scope) {
store.setValue(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE, scope.getValue());
CustomInstructionsChatLoadScope result = PreferencesUtils.getCustomInstructionsChatLoadScope(store);
assertEquals(scope, result);
}
@Test
void testGetCustomInstructionsChatLoadScope_fallsBackToDefaultInCaseOfInvalidValueInStore() {
store.setValue(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE, "invalid_value");
CustomInstructionsChatLoadScope result = PreferencesUtils.getCustomInstructionsChatLoadScope(store);
assertEquals(CustomInstructionsChatLoadScope.DEFAULT_VALUE, result);
assertEquals(CustomInstructionsChatLoadScope.DEFAULT_VALUE.getValue(),
store.getString(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE),
"The invalid stored value should have been corrected to the default");
}
@Test
void testGetCustomInstructionsChatLoadScopeDefault_returnsConfiguredDefault() {
// explicitly store varying values for InstanceScope and DefaultScope
store.setValue(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE,
CustomInstructionsChatLoadScope.ALL_PROJECTS.getValue());
store.setDefault(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE,
CustomInstructionsChatLoadScope.REFERENCED_PROJECTS.getValue());
CustomInstructionsChatLoadScope result = PreferencesUtils.getCustomInstructionsChatLoadScopeDefault(store);
// Should return the default value, not the stored value
assertEquals(CustomInstructionsChatLoadScope.REFERENCED_PROJECTS, result);
}
@Test
void testGetCustomInstructionsChatLoadScopeDefault_fallsBackToValidDefaultInCaseOfInvalidValue() {
String invalidDefaultValue = "invalid_default_value";
String instanceScopValue = CustomInstructionsChatLoadScope.REFERENCED_PROJECTS.getValue();
store.setDefault(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE, invalidDefaultValue);
store.setValue(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE, instanceScopValue);
CustomInstructionsChatLoadScope result = PreferencesUtils.getCustomInstructionsChatLoadScopeDefault(store);
assertEquals(CustomInstructionsChatLoadScope.DEFAULT_VALUE, result);
assertEquals(invalidDefaultValue,
store.getDefaultString(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE),
"The stored DefaultScope value should not change, even if it's invalid. "
+ "That is repsonsibility of a PreferenceInitializer.");
assertEquals(instanceScopValue,
store.getString(Constants.CUSTOM_INSTRUCTIONS_CHAT_LOAD_SCOPE),
"The stored InstanceScope value should not change");
}
}