@@ -17,8 +17,10 @@ import org.mockito.kotlin.any
17
17
import org.mockito.kotlin.doReturn
18
18
import org.mockito.kotlin.spy
19
19
import org.mockito.kotlin.stub
20
+ import software.amazon.awssdk.services.codewhispererruntime.model.Completion
20
21
import software.aws.toolkits.core.utils.test.aString
21
22
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererRecommendationManager
23
+ import software.aws.toolkits.jetbrains.services.codewhisperer.service.RequestContext
22
24
import software.aws.toolkits.jetbrains.utils.rules.PythonCodeInsightTestFixtureRule
23
25
24
26
class CodeWhispererRecommendationManagerTest {
@@ -72,7 +74,7 @@ class CodeWhispererRecommendationManagerTest {
72
74
fun `test duplicated recommendation after truncation will be discarded` () {
73
75
val userInput = " "
74
76
sut.stub {
75
- onGeneric { findRightContextOverlap(any(), any()) } doReturn " }"
77
+ onGeneric { findRightContextOverlap(any< RequestContext > (), any< Completion > ()) } doReturn " }"
76
78
onGeneric { reformatReference(any(), any()) } doReturn aCompletion(" def" )
77
79
}
78
80
val detail = sut.buildDetailContext(
@@ -91,7 +93,7 @@ class CodeWhispererRecommendationManagerTest {
91
93
fun `test blank recommendation after truncation will be discarded` () {
92
94
val userInput = " "
93
95
sut.stub {
94
- onGeneric { findRightContextOverlap(any(), any()) } doReturn " }"
96
+ onGeneric { findRightContextOverlap(any< RequestContext > (), any< Completion > ()) } doReturn " }"
95
97
}
96
98
val detail = sut.buildDetailContext(
97
99
aRequestContext(project),
@@ -102,4 +104,104 @@ class CodeWhispererRecommendationManagerTest {
102
104
assertThat(detail[0 ].isDiscarded).isTrue
103
105
assertThat(detail[0 ].isTruncatedOnRight).isTrue
104
106
}
107
+
108
+ @Test
109
+ fun `overlap calculation should trim new line character starting from second character (index 1 of a string)` () {
110
+ // recommendation is wrapped inside |recommendationContent|
111
+ /* *
112
+ * public foo() {
113
+ * re|turn foo
114
+ *}|
115
+ * public bar() {
116
+ * return bar
117
+ * }
118
+ */
119
+ var overlap: String = sut.findRightContextOverlap(rightContext = " foo\n }\n\n\n public bar () {\n\t return bar\n }" , recommendationContent = " turn foo\n }" )
120
+ assertThat(overlap).isEqualTo(" foo\n }" )
121
+
122
+ /* *
123
+ * public foo() {
124
+ * |return foo
125
+ * }|
126
+ *
127
+ * public bar() {
128
+ * return bar
129
+ * }
130
+ */
131
+ overlap = sut.findRightContextOverlap(rightContext = " \n\n\n\n public bar() {\n\t return bar\n }" , recommendationContent = " return foo\n }" )
132
+ assertThat(overlap).isEqualTo(" " )
133
+
134
+ /* *
135
+ * println(|world)|;
136
+ * String foo = "foo";
137
+ */
138
+ overlap = sut.findRightContextOverlap(rightContext = " ld);\n String foo = \" foo\" ;" , recommendationContent = " world)" )
139
+ assertThat(overlap).isEqualTo(" ld)" )
140
+
141
+ /* *
142
+ * return |has_d_at_end|
143
+ *
144
+ * def foo:
145
+ * pass
146
+ */
147
+ overlap = sut.findRightContextOverlap(rightContext = " \n\n def foo():\n\t pass" , recommendationContent = " has_d_at_end" )
148
+ assertThat(overlap).isEqualTo(" " )
149
+
150
+ /* *
151
+ * {
152
+ * { foo: foo },
153
+ * { bar: bar },
154
+ * { |baz: baz }|
155
+ * }
156
+ *
157
+ */
158
+ overlap = sut.findRightContextOverlap(rightContext = " \n }" , recommendationContent = " baz: baz }" )
159
+ assertThat(overlap).isEqualTo(" " )
160
+
161
+ /* *
162
+ * |
163
+ *
164
+ * foo|
165
+ *
166
+ */
167
+ overlap = sut.findRightContextOverlap(rightContext = " \n\n\t foo}" , recommendationContent = " \n\t foo" )
168
+ assertThat(overlap).isEqualTo(" \n\t foo" )
169
+
170
+ /* * A case we can't cover
171
+ * def foo():
172
+ * |print(foo)|
173
+ *
174
+ *
175
+ * print(foo)
176
+ */
177
+ overlap = sut.findRightContextOverlap(rightContext = " \n\n\n\t print(foo)" , recommendationContent = " print(foo)" )
178
+ assertThat(overlap).isEqualTo(" " )
179
+ }
180
+
181
+ @Test
182
+ fun `trim extra prefixing new line character` () {
183
+ var actual: String = CodeWhispererRecommendationManager .trimExtraPrefixNewLine(" " )
184
+ assertThat(actual).isEqualTo(" " )
185
+
186
+ actual = CodeWhispererRecommendationManager .trimExtraPrefixNewLine(" f" )
187
+ assertThat(actual).isEqualTo(" f" )
188
+
189
+ actual = CodeWhispererRecommendationManager .trimExtraPrefixNewLine(" \n\n " )
190
+ assertThat(actual).isEqualTo(" \n " )
191
+
192
+ actual = CodeWhispererRecommendationManager .trimExtraPrefixNewLine(" foo" )
193
+ assertThat(actual).isEqualTo(" foo" )
194
+
195
+ actual = CodeWhispererRecommendationManager .trimExtraPrefixNewLine(" \n foo" )
196
+ assertThat(actual).isEqualTo(" \n foo" )
197
+
198
+ actual = CodeWhispererRecommendationManager .trimExtraPrefixNewLine(" \n\n\n foo\n bar" )
199
+ assertThat(actual).isEqualTo(" \n foo\n bar" )
200
+
201
+ actual = CodeWhispererRecommendationManager .trimExtraPrefixNewLine(" \n\n foo\n bar" )
202
+ assertThat(actual).isEqualTo(" \n foo\n bar" )
203
+
204
+ actual = CodeWhispererRecommendationManager .trimExtraPrefixNewLine(" \n\n\t foo\n bar" )
205
+ assertThat(actual).isEqualTo(" \n\t foo\n bar" )
206
+ }
105
207
}
0 commit comments