Skip to content

Commit 0408afc

Browse files
committed
test: use a unified getSuggestionsMock for all the autocomplete tests
1 parent 8b57a89 commit 0408afc

File tree

1 file changed

+105
-99
lines changed

1 file changed

+105
-99
lines changed

test/adapters/autocomplete-adapter.test.ts

Lines changed: 105 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ describe("AutoCompleteAdapter", () => {
233233
it("converts LSP CompletionItem array to AutoComplete Suggestions array", async () => {
234234
const customRequest = createRequest({ prefix: "", position: new Point(0, 10) })
235235
customRequest.editor.setText("foo #align bar")
236-
sinon.stub(server.connection, "completion").resolves(items)
237-
const results = await autoCompleteAdapter.getSuggestions(server, customRequest)
236+
const results = await getSuggestionsMock(items, customRequest)
238237

239238
expect(results.length).equals(items.length)
240239
expect(results[0].displayText).equals("align")
@@ -263,8 +262,7 @@ describe("AutoCompleteAdapter", () => {
263262
})
264263

265264
it("respects onDidConvertCompletionItem", async () => {
266-
sinon.stub(server.connection, "completion").resolves([{ label: "label" }] as CompletionItem[])
267-
const results = await autoCompleteAdapter.getSuggestions(server, createRequest({}), (c, a, r) => {
265+
const results = await getSuggestionsMock([{ label: "label" }], createRequest({}), (c, a, r) => {
268266
;(a as ac.TextSuggestion).text = c.label + " ok"
269267
a.displayText = r.scopeDescriptor.getScopesArray()[0]
270268
})
@@ -275,23 +273,26 @@ describe("AutoCompleteAdapter", () => {
275273
})
276274

277275
it("converts empty array into an empty AutoComplete Suggestions array", async () => {
278-
sinon.stub(server.connection, "completion").resolves([])
279-
const results = await autoCompleteAdapter.getSuggestions(server, createRequest({}))
276+
const results = await getSuggestionsMock([], createRequest({}))
280277
expect(results.length).equals(0)
281278
})
282279

283280
it("converts LSP CompletionItem to AutoComplete Suggestion without textEdit", async () => {
284-
sinon.stub(server.connection, "completion").resolves([
285-
{
286-
label: "label",
287-
insertText: "insert",
288-
filterText: "filter",
289-
kind: ls.CompletionItemKind.Keyword,
290-
detail: "keyword",
291-
documentation: "a truly useful keyword",
292-
},
293-
] as CompletionItem[])
294-
const result = (await autoCompleteAdapter.getSuggestions(server, createRequest({})))[0]
281+
const result = (
282+
await getSuggestionsMock(
283+
[
284+
{
285+
label: "label",
286+
insertText: "insert",
287+
filterText: "filter",
288+
kind: ls.CompletionItemKind.Keyword,
289+
detail: "keyword",
290+
documentation: "a truly useful keyword",
291+
},
292+
],
293+
createRequest({})
294+
)
295+
)[0]
295296
expect((result as TextSuggestion).text).equals("insert")
296297
expect(result.displayText).equals("label")
297298
expect(result.type).equals("keyword")
@@ -307,22 +308,26 @@ describe("AutoCompleteAdapter", () => {
307308
activatedManually: false,
308309
})
309310
customRequest.editor.setText("foo #label bar")
310-
sinon.stub(server.connection, "completion").resolves([
311-
{
312-
label: "label",
313-
insertText: "insert",
314-
filterText: "filter",
315-
kind: ls.CompletionItemKind.Variable,
316-
detail: "number",
317-
documentation: "a truly useful variable",
318-
textEdit: {
319-
range: { start: { line: 0, character: 4 }, end: { line: 0, character: 10 } },
320-
newText: "newText",
321-
},
322-
},
323-
] as CompletionItem[])
324311

325-
const result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
312+
const result = (
313+
await getSuggestionsMock(
314+
[
315+
{
316+
label: "label",
317+
insertText: "insert",
318+
filterText: "filter",
319+
kind: ls.CompletionItemKind.Variable,
320+
detail: "number",
321+
documentation: "a truly useful variable",
322+
textEdit: {
323+
range: { start: { line: 0, character: 4 }, end: { line: 0, character: 10 } },
324+
newText: "newText",
325+
},
326+
},
327+
],
328+
customRequest
329+
)
330+
)[0]
326331
expect(result.displayText).equals("label")
327332
expect(result.type).equals("variable")
328333
expect(result.rightLabel).equals("number")
@@ -333,19 +338,20 @@ describe("AutoCompleteAdapter", () => {
333338
})
334339

335340
it("converts LSP CompletionItem with insertText and filterText to AutoComplete Suggestion", async () => {
336-
sinon.stub(server.connection, "completion").resolves([
337-
{
338-
label: "label",
339-
insertText: "insert",
340-
filterText: "filter",
341-
kind: ls.CompletionItemKind.Keyword,
342-
detail: "detail",
343-
documentation: "a very exciting keyword",
344-
},
345-
{ label: "filteredOut", filterText: "nop" },
346-
] as CompletionItem[])
347-
348-
const results = await autoCompleteAdapter.getSuggestions(server, createRequest({ prefix: "fil" }))
341+
const results = await getSuggestionsMock(
342+
[
343+
{
344+
label: "label",
345+
insertText: "insert",
346+
filterText: "filter",
347+
kind: ls.CompletionItemKind.Keyword,
348+
detail: "detail",
349+
documentation: "a very exciting keyword",
350+
},
351+
{ label: "filteredOut", filterText: "nop" },
352+
],
353+
createRequest({ prefix: "fil" })
354+
)
349355
expect(results.length).equals(1)
350356

351357
const result = results[0]
@@ -358,51 +364,50 @@ describe("AutoCompleteAdapter", () => {
358364
})
359365

360366
it("converts LSP CompletionItem with missing documentation to AutoComplete Suggestion", async () => {
361-
sinon.stub(server.connection, "completion").resolves([{ label: "label", detail: "detail" }] as CompletionItem[])
362-
363-
const result = (await autoCompleteAdapter.getSuggestions(server, createRequest({})))[0]
367+
const result = (await getSuggestionsMock([{ label: "label", detail: "detail" }], createRequest({})))[0]
364368
expect(result.rightLabel).equals("detail")
365369
expect(result.description).equals(undefined)
366370
expect(result.descriptionMarkdown).equals(undefined)
367371
})
368372

369373
it("converts LSP CompletionItem with markdown documentation to AutoComplete Suggestion", async () => {
370-
sinon
371-
.stub(server.connection, "completion")
372-
.resolves([
373-
{ label: "label", detail: "detail", documentation: { value: "Some *markdown*", kind: "markdown" } },
374-
] as CompletionItem[])
375-
376-
const result = (await autoCompleteAdapter.getSuggestions(server, createRequest({})))[0]
374+
const result = (
375+
await getSuggestionsMock(
376+
[{ label: "label", detail: "detail", documentation: { value: "Some *markdown*", kind: "markdown" } }],
377+
createRequest({})
378+
)
379+
)[0]
377380
expect(result.rightLabel).equals("detail")
378381
expect(result.description).equals(undefined)
379382
expect(result.descriptionMarkdown).equals("Some *markdown*")
380383
})
381384

382385
it("converts LSP CompletionItem with plaintext documentation to AutoComplete Suggestion", async () => {
383-
sinon
384-
.stub(server.connection, "completion")
385-
.resolves([
386-
{ label: "label", detail: "detail", documentation: { value: "Some plain text", kind: "plaintext" } },
387-
] as CompletionItem[])
388-
389-
const result = (await autoCompleteAdapter.getSuggestions(server, createRequest({})))[0]
386+
const result = (
387+
await getSuggestionsMock(
388+
[{ label: "label", detail: "detail", documentation: { value: "Some plain text", kind: "plaintext" } }],
389+
createRequest({})
390+
)
391+
)[0]
390392
expect(result.rightLabel).equals("detail")
391393
expect(result.description).equals("Some plain text")
392394
expect(result.descriptionMarkdown).equals(undefined)
393395
})
394396

395397
it("converts LSP CompletionItem without insertText or filterText to AutoComplete Suggestion", async () => {
396-
sinon.stub(server.connection, "completion").resolves([
397-
{
398-
label: "label",
399-
kind: ls.CompletionItemKind.Keyword,
400-
detail: "detail",
401-
documentation: "A very useful keyword",
402-
},
403-
] as CompletionItem[])
404-
405-
const result = (await autoCompleteAdapter.getSuggestions(server, createRequest({})))[0]
398+
const result = (
399+
await getSuggestionsMock(
400+
[
401+
{
402+
label: "label",
403+
kind: ls.CompletionItemKind.Keyword,
404+
detail: "detail",
405+
documentation: "A very useful keyword",
406+
},
407+
],
408+
createRequest({})
409+
)
410+
)[0]
406411
expect((result as TextSuggestion).text).equals("label")
407412
expect(result.displayText).equals("label")
408413
expect(result.type).equals("keyword")
@@ -412,9 +417,7 @@ describe("AutoCompleteAdapter", () => {
412417
})
413418

414419
it("does not do anything if there is no textEdit", async () => {
415-
sinon.stub(server.connection, "completion").resolves([{ label: "", filterText: "rep" }] as CompletionItem[])
416-
417-
const result = (await autoCompleteAdapter.getSuggestions(server, createRequest({ prefix: "rep" })))[0]
420+
const result = (await getSuggestionsMock([{ label: "", filterText: "rep" }], createRequest({ prefix: "rep" })))[0]
418421
expect((result as TextSuggestion).text).equals("")
419422
expect(result.displayText).equals("")
420423
expect(result.replacementPrefix).equals("")
@@ -423,17 +426,20 @@ describe("AutoCompleteAdapter", () => {
423426
it("applies changes from TextEdit to text", async () => {
424427
const customRequest = createRequest({ prefix: "", position: new Point(0, 10) })
425428
customRequest.editor.setText("foo #align bar")
426-
sinon.stub(server.connection, "completion").resolves([
427-
{
428-
label: "align",
429-
sortText: "a",
430-
textEdit: {
431-
range: { start: { line: 0, character: 4 }, end: { line: 0, character: 10 } },
432-
newText: "hello world",
429+
430+
const results = await getSuggestionsMock(
431+
[
432+
{
433+
label: "align",
434+
sortText: "a",
435+
textEdit: {
436+
range: { start: { line: 0, character: 4 }, end: { line: 0, character: 10 } },
437+
newText: "hello world",
438+
},
433439
},
434-
},
435-
] as CompletionItem[])
436-
const results = await autoCompleteAdapter.getSuggestions(server, customRequest)
440+
],
441+
customRequest
442+
)
437443

438444
expect(results[0].displayText).equals("align")
439445
expect((results[0] as TextSuggestion).text).equals("hello world")
@@ -443,7 +449,7 @@ describe("AutoCompleteAdapter", () => {
443449
it("updates the replacementPrefix when the editor text changes", async () => {
444450
const customRequest = createRequest({ prefix: "", position: new Point(0, 8) })
445451
customRequest.editor.setText("foo #ali bar")
446-
sinon.stub(server.connection, "completion").resolves([
452+
const items = [
447453
{
448454
label: "align",
449455
sortText: "a",
@@ -452,9 +458,9 @@ describe("AutoCompleteAdapter", () => {
452458
newText: "hello world",
453459
},
454460
},
455-
] as CompletionItem[])
461+
]
456462

457-
let result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
463+
let result = (await getSuggestionsMock(items, customRequest))[0]
458464
expect(result.replacementPrefix).equals("#ali")
459465

460466
customRequest.editor.setTextInBufferRange(
@@ -465,7 +471,7 @@ describe("AutoCompleteAdapter", () => {
465471
"g"
466472
)
467473
customRequest.bufferPosition = new Point(0, 9)
468-
result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
474+
result = (await getSuggestionsMock(items, customRequest))[0]
469475
expect(result.replacementPrefix).equals("#alig")
470476

471477
customRequest.editor.setTextInBufferRange(
@@ -476,7 +482,7 @@ describe("AutoCompleteAdapter", () => {
476482
"n"
477483
)
478484
customRequest.bufferPosition = new Point(0, 10)
479-
result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
485+
result = (await getSuggestionsMock(items, customRequest))[0]
480486
expect(result.replacementPrefix).equals("#align")
481487

482488
customRequest.editor.setTextInBufferRange(
@@ -487,16 +493,16 @@ describe("AutoCompleteAdapter", () => {
487493
""
488494
)
489495
customRequest.bufferPosition = new Point(0, 7)
490-
result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
496+
result = (await getSuggestionsMock(items, customRequest))[0]
491497
expect(result.replacementPrefix).equals("#al")
492498
})
493499

494500
it("does not include the triggerChar in replacementPrefix", async () => {
495501
const customRequest = createRequest({ prefix: ".", position: new Point(0, 4) })
496502
customRequest.editor.setText("foo.")
497503
server.capabilities.completionProvider!.triggerCharacters = ["."]
498-
sinon.stub(server.connection, "completion").resolves([{ label: "bar" }] as CompletionItem[])
499-
let result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
504+
const items = [{ label: "bar" }]
505+
let result = (await getSuggestionsMock(items, customRequest))[0]
500506
expect(result.replacementPrefix).equals("")
501507
customRequest.editor.setTextInBufferRange(
502508
[
@@ -507,7 +513,7 @@ describe("AutoCompleteAdapter", () => {
507513
)
508514
customRequest.prefix = "b"
509515
customRequest.bufferPosition = new Point(0, 5)
510-
result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
516+
result = (await getSuggestionsMock(items, customRequest))[0]
511517
expect(result.replacementPrefix).equals("b")
512518
customRequest.editor.setTextInBufferRange(
513519
[
@@ -518,15 +524,15 @@ describe("AutoCompleteAdapter", () => {
518524
)
519525
customRequest.prefix = "ba"
520526
customRequest.bufferPosition = new Point(0, 6)
521-
result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
527+
result = (await getSuggestionsMock(items, customRequest))[0]
522528
expect(result.replacementPrefix).equals("ba")
523529
})
524530

525531
it("includes non trigger character prefix in replacementPrefix", async () => {
526532
const customRequest = createRequest({ prefix: "foo", position: new Point(0, 3) })
527533
customRequest.editor.setText("foo")
528-
sinon.stub(server.connection, "completion").resolves([{ label: "foobar" }] as CompletionItem[])
529-
let result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
534+
const items = [{ label: "foobar" }]
535+
let result = (await getSuggestionsMock(items, customRequest))[0]
530536

531537
expect(result.replacementPrefix).equals("foo")
532538
customRequest.editor.setTextInBufferRange(
@@ -538,7 +544,7 @@ describe("AutoCompleteAdapter", () => {
538544
)
539545
customRequest.prefix = "foob"
540546
customRequest.bufferPosition = new Point(0, 4)
541-
result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
547+
result = (await getSuggestionsMock(items, customRequest))[0]
542548
expect(result.replacementPrefix).equals("foob")
543549
customRequest.editor.setTextInBufferRange(
544550
[
@@ -549,7 +555,7 @@ describe("AutoCompleteAdapter", () => {
549555
)
550556
customRequest.prefix = "fooba"
551557
customRequest.bufferPosition = new Point(0, 5)
552-
result = (await autoCompleteAdapter.getSuggestions(server, customRequest))[0]
558+
result = (await getSuggestionsMock(items, customRequest))[0]
553559
expect(result.replacementPrefix).equals("fooba")
554560
})
555561
})

0 commit comments

Comments
 (0)