Skip to content

Commit 592c736

Browse files
committed
add more tests to diff
1 parent 69f254b commit 592c736

File tree

2 files changed

+292
-21
lines changed

2 files changed

+292
-21
lines changed

packages/core/src/amazonq/commons/diff.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export async function computeDiff(leftPath: string, rightPath: string, tabId: st
3535
const leftFile = await vscode.workspace.openTextDocument(left)
3636
const rightFile = await vscode.workspace.openTextDocument(right)
3737

38-
const changes = diffLines(leftFile.getText(), rightFile.getText())
38+
const changes = diffLines(leftFile.getText(), rightFile.getText(), {
39+
ignoreWhitespace: true,
40+
})
3941

4042
let charsAdded = 0
4143
let charsRemoved = 0

packages/core/src/test/amazonq/common/diff.test.ts

Lines changed: 289 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,15 @@ describe('diff', () => {
122122
})
123123

124124
describe('computeDiff', () => {
125-
const mockLeftDocument = {
126-
getText: () => 'line1\nline2',
125+
const mockOriginalDocument = {
126+
getText: () => 'line1\nline2\nline3\n',
127127
uri: vscode.Uri.file('test.txt'),
128128
fileName: 'test.txt',
129129
}
130130

131-
const mockRightDocument = {
132-
getText: () => 'line1\nline3\nline4\nline5',
133-
uri: vscode.Uri.file('test.txt'),
134-
fileName: 'test.txt',
135-
}
136-
137-
it('returns 0 added or removed chars and lines for the same file', async () => {
131+
it('returns no addition or removal for the same file', async () => {
138132
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
139-
sandbox.stub(vscode.workspace, 'openTextDocument').resolves(mockLeftDocument as unknown as TextDocument)
133+
sandbox.stub(vscode.workspace, 'openTextDocument').resolves(mockOriginalDocument as unknown as TextDocument)
140134

141135
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
142136
filePath,
@@ -146,8 +140,8 @@ describe('diff', () => {
146140

147141
const expectedChanges = [
148142
{
149-
count: 2,
150-
value: 'line1\nline2',
143+
count: 3,
144+
value: 'line1\nline2\nline3\n',
151145
},
152146
]
153147

@@ -158,14 +152,19 @@ describe('diff', () => {
158152
assert.equal(linesRemoved, 0)
159153
})
160154

161-
it('returns expected added or removed chars and lines for different files', async () => {
155+
it('counts insertion of new line', async () => {
156+
const mockInsertionDocument = {
157+
getText: () => 'line1\ninserted\nline2\nline3\n',
158+
uri: vscode.Uri.file('test.txt'),
159+
fileName: 'test.txt',
160+
}
162161
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
163162
sandbox
164163
.stub(vscode.workspace, 'openTextDocument')
165164
.onFirstCall()
166-
.resolves(mockLeftDocument as unknown as TextDocument)
165+
.resolves(mockOriginalDocument as unknown as TextDocument)
167166
.onSecondCall()
168-
.resolves(mockRightDocument as unknown as TextDocument)
167+
.resolves(mockInsertionDocument as unknown as TextDocument)
169168

170169
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
171170
filePath,
@@ -179,24 +178,294 @@ describe('diff', () => {
179178
value: 'line1\n',
180179
},
181180
{
181+
added: true,
182182
count: 1,
183+
removed: undefined,
184+
value: 'inserted\n',
185+
},
186+
{
187+
count: 2,
188+
value: 'line2\nline3\n',
189+
},
190+
]
191+
192+
assert.deepEqual(changes, expectedChanges)
193+
assert.equal(charsAdded, 8)
194+
assert.equal(linesAdded, 1)
195+
assert.equal(charsRemoved, 0)
196+
assert.equal(linesRemoved, 0)
197+
})
198+
199+
it('counts insertion of multiple lines', async () => {
200+
const mockMultipleInsertionDocument = {
201+
getText: () => 'line1\ninserted1\ninserted2\nline2\nline3\ninserted3\n',
202+
uri: vscode.Uri.file('test.txt'),
203+
fileName: 'test.txt',
204+
}
205+
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
206+
sandbox
207+
.stub(vscode.workspace, 'openTextDocument')
208+
.onFirstCall()
209+
.resolves(mockOriginalDocument as unknown as TextDocument)
210+
.onSecondCall()
211+
.resolves(mockMultipleInsertionDocument as unknown as TextDocument)
212+
213+
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
214+
filePath,
215+
rightPath,
216+
tabId
217+
)
218+
219+
const expectedChanges = [
220+
{
221+
count: 1,
222+
value: 'line1\n',
223+
},
224+
{
225+
added: true,
226+
count: 2,
227+
removed: undefined,
228+
value: 'inserted1\ninserted2\n',
229+
},
230+
{
231+
count: 2,
232+
value: 'line2\nline3\n',
233+
},
234+
{
235+
added: true,
236+
count: 1,
237+
removed: undefined,
238+
value: 'inserted3\n',
239+
},
240+
]
241+
242+
assert.deepEqual(changes, expectedChanges)
243+
assert.equal(charsAdded, 27)
244+
assert.equal(linesAdded, 3)
245+
assert.equal(charsRemoved, 0)
246+
assert.equal(linesRemoved, 0)
247+
})
248+
249+
it('counts modification of existing line', async () => {
250+
const mockModificationDocument = {
251+
getText: () => 'line1\nmodified\nline3\n',
252+
uri: vscode.Uri.file('test.txt'),
253+
fileName: 'test.txt',
254+
}
255+
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
256+
sandbox
257+
.stub(vscode.workspace, 'openTextDocument')
258+
.onFirstCall()
259+
.resolves(mockOriginalDocument as unknown as TextDocument)
260+
.onSecondCall()
261+
.resolves(mockModificationDocument as unknown as TextDocument)
262+
263+
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
264+
filePath,
265+
rightPath,
266+
tabId
267+
)
268+
269+
const expectedChanges = [
270+
{
271+
count: 1,
272+
value: 'line1\n',
273+
},
274+
{
183275
added: undefined,
276+
count: 1,
184277
removed: true,
185-
value: 'line2',
278+
value: 'line2\n',
186279
},
187280
{
188-
count: 3,
189281
added: true,
282+
count: 1,
190283
removed: undefined,
191-
value: 'line3\nline4\nline5',
284+
value: 'modified\n',
285+
},
286+
{
287+
count: 1,
288+
value: 'line3\n',
192289
},
193290
]
194291

195292
assert.deepEqual(changes, expectedChanges)
196-
assert.equal(charsAdded, 15)
197-
assert.equal(linesAdded, 3)
293+
assert.equal(charsAdded, 8)
294+
assert.equal(linesAdded, 1)
295+
assert.equal(charsRemoved, 5)
296+
assert.equal(linesRemoved, 1)
297+
})
298+
299+
it('counts deletion of existing line', async () => {
300+
const mockDeletionDocument = {
301+
getText: () => 'line1\nline3\n',
302+
uri: vscode.Uri.file('test.txt'),
303+
fileName: 'test.txt',
304+
}
305+
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
306+
sandbox
307+
.stub(vscode.workspace, 'openTextDocument')
308+
.onFirstCall()
309+
.resolves(mockOriginalDocument as unknown as TextDocument)
310+
.onSecondCall()
311+
.resolves(mockDeletionDocument as unknown as TextDocument)
312+
313+
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
314+
filePath,
315+
rightPath,
316+
tabId
317+
)
318+
319+
const expectedChanges = [
320+
{
321+
count: 1,
322+
value: 'line1\n',
323+
},
324+
{
325+
added: undefined,
326+
count: 1,
327+
removed: true,
328+
value: 'line2\n',
329+
},
330+
{
331+
count: 1,
332+
value: 'line3\n',
333+
},
334+
]
335+
336+
assert.deepEqual(changes, expectedChanges)
337+
assert.equal(charsAdded, 0)
338+
assert.equal(linesAdded, 0)
198339
assert.equal(charsRemoved, 5)
199340
assert.equal(linesRemoved, 1)
200341
})
342+
343+
it('counts deletion of existing line and then adding a new line', async () => {
344+
const mockDeletionAndAdditionDocument = {
345+
getText: () => 'line1\nline3\nline4\n',
346+
uri: vscode.Uri.file('test.txt'),
347+
fileName: 'test.txt',
348+
}
349+
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
350+
sandbox
351+
.stub(vscode.workspace, 'openTextDocument')
352+
.onFirstCall()
353+
.resolves(mockOriginalDocument as unknown as TextDocument)
354+
.onSecondCall()
355+
.resolves(mockDeletionAndAdditionDocument as unknown as TextDocument)
356+
357+
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
358+
filePath,
359+
rightPath,
360+
tabId
361+
)
362+
363+
const expectedChanges = [
364+
{
365+
count: 1,
366+
value: 'line1\n',
367+
},
368+
{
369+
added: undefined,
370+
count: 1,
371+
removed: true,
372+
value: 'line2\n',
373+
},
374+
{
375+
count: 1,
376+
value: 'line3\n',
377+
},
378+
{
379+
added: true,
380+
count: 1,
381+
removed: undefined,
382+
value: 'line4\n',
383+
},
384+
]
385+
386+
assert.deepEqual(changes, expectedChanges)
387+
assert.equal(charsAdded, 5)
388+
assert.equal(linesAdded, 1)
389+
assert.equal(charsRemoved, 5)
390+
assert.equal(linesRemoved, 1)
391+
})
392+
393+
it('counts a new empty line', async () => {
394+
const mockEmptyLineDocument = {
395+
getText: () => 'line1\nline2\n\nline3\n',
396+
uri: vscode.Uri.file('test.txt'),
397+
fileName: 'test.txt',
398+
}
399+
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
400+
sandbox
401+
.stub(vscode.workspace, 'openTextDocument')
402+
.onFirstCall()
403+
.resolves(mockOriginalDocument as unknown as TextDocument)
404+
.onSecondCall()
405+
.resolves(mockEmptyLineDocument as unknown as TextDocument)
406+
407+
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
408+
filePath,
409+
rightPath,
410+
tabId
411+
)
412+
413+
const expectedChanges = [
414+
{
415+
count: 2,
416+
value: 'line1\nline2\n',
417+
},
418+
{
419+
added: true,
420+
count: 1,
421+
removed: undefined,
422+
value: '\n',
423+
},
424+
{
425+
count: 1,
426+
value: 'line3\n',
427+
},
428+
]
429+
430+
assert.deepEqual(changes, expectedChanges)
431+
assert.equal(charsAdded, 0)
432+
assert.equal(linesAdded, 1)
433+
assert.equal(charsRemoved, 0)
434+
assert.equal(linesRemoved, 0)
435+
})
436+
437+
it('ignores leading and trailing whitespaces', async () => {
438+
const mockWhitespaceDocument = {
439+
getText: () => ' line1 \n line2 \n line3 \n',
440+
uri: vscode.Uri.file('test.txt'),
441+
fileName: 'test.txt',
442+
}
443+
sandbox.stub(FileSystem.prototype, 'exists').resolves(true)
444+
sandbox
445+
.stub(vscode.workspace, 'openTextDocument')
446+
.onFirstCall()
447+
.resolves(mockOriginalDocument as unknown as TextDocument)
448+
.onSecondCall()
449+
.resolves(mockWhitespaceDocument as unknown as TextDocument)
450+
451+
const { changes, charsAdded, linesAdded, charsRemoved, linesRemoved } = await computeDiff(
452+
filePath,
453+
rightPath,
454+
tabId
455+
)
456+
457+
const expectedChanges = [
458+
{
459+
count: 3,
460+
value: 'line1\nline2\nline3\n',
461+
},
462+
]
463+
464+
assert.deepEqual(changes, expectedChanges)
465+
assert.equal(charsAdded, 0)
466+
assert.equal(linesAdded, 0)
467+
assert.equal(charsRemoved, 0)
468+
assert.equal(linesRemoved, 0)
469+
})
201470
})
202471
})

0 commit comments

Comments
 (0)