|
18 | 18 | import static org.junit.jupiter.api.Assertions.assertFalse; |
19 | 19 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
20 | 20 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
21 | 22 |
|
22 | 23 | import java.net.URI; |
23 | 24 | import java.util.ArrayList; |
|
63 | 64 | import org.eclipse.swt.widgets.TableItem; |
64 | 65 | import org.eclipse.ui.tests.harness.util.DisplayHelper; |
65 | 66 | import org.junit.jupiter.api.Test; |
| 67 | +import org.junit.jupiter.params.ParameterizedTest; |
| 68 | +import org.junit.jupiter.params.provider.Arguments; |
| 69 | +import org.junit.jupiter.params.provider.FieldSource; |
66 | 70 |
|
67 | 71 | import com.google.gson.JsonPrimitive; |
68 | 72 |
|
@@ -429,60 +433,55 @@ public void testDuplicateVariable() throws CoreException { |
429 | 433 | assertEquals("a and a", viewer.getDocument().get()); |
430 | 434 | } |
431 | 435 |
|
432 | | - @Test |
433 | | - public void testComplexSnippets() throws CoreException { |
434 | | - record Test(String completion, String expected, String fileContent, int caretPos, int selectionLen) { |
435 | | - Test(String completion, String expected) { |
436 | | - this(completion, expected, "", 0, 0); |
437 | | - } |
438 | | - } |
439 | | - Test[] tests = { |
440 | | - // Variables and escaped dollars |
441 | | - new Test("$TM_LINE_NUMBER - \\$TM_LINE_NUMBER - ${TM_LINE_NUMBER} - \\${TM_LINE_NUMBER}", |
442 | | - "1 - $TM_LINE_NUMBER - 1 - ${TM_LINE_NUMBER}"), |
443 | | - // Default values for variables |
444 | | - new Test("${TM_SELECTED_TEXT:defaultval}", "defaultval"), |
445 | | - // Escaped dollars |
446 | | - new Test("\\$1 and \\$", "$1 and $"), |
447 | | - // Escaped escapes |
448 | | - new Test("\\\\$1 and ${3:foo}", "\\ and foo"), |
449 | | - // Escaped values in a choice |
450 | | - new Test("${2|a\\,b\\},c|}", "a,b}"), |
451 | | - // TM_CURRENT_WORD completion (caret after 'foo') |
452 | | - new Test("${1:$TM_CURRENT_WORD}", "xx abcabc yy", "xx abc yy", 3, 0), |
453 | | - new Test("${1:$TM_CURRENT_WORD}", "xx aabcbc yy", "xx abc yy", 4, 0), |
454 | | - new Test("${1:$TM_CURRENT_WORD}", "xx aabc yy", "xx abc yy", 4, 2), |
455 | | - // Snippets with syntax errors: |
456 | | - // Make sure they don't cause endless loops or crashes |
457 | | - new Test("$", "$"), // |
458 | | - new Test("${", "${"), // |
459 | | - new Test("$$", "$$"), // |
460 | | - new Test("$$TM_LINE_NUMBER", "$1"), // |
461 | | - new Test("${VARIABLE", "${VARIABLE"), // |
462 | | - new Test("${VARIABLE:", "${VARIABLE:"), // |
463 | | - new Test("${VARIABLE:foo", "${VARIABLE:foo"), // |
464 | | - new Test("${1|a", "${1|a"), // |
465 | | - new Test("${1|a,}", "${1|a,}"), // |
466 | | - }; |
467 | | - for (Test test : tests) { |
| 436 | + public static List<Arguments> testComplexSnippets = List.of( |
| 437 | + // Variables and escaped dollars |
| 438 | + arguments("$TM_LINE_NUMBER - \\$TM_LINE_NUMBER - ${TM_LINE_NUMBER} - \\${TM_LINE_NUMBER}", |
| 439 | + "1 - $TM_LINE_NUMBER - 1 - ${TM_LINE_NUMBER}", "", 0, 0), |
| 440 | + // Default values for variables |
| 441 | + arguments("${TM_SELECTED_TEXT:defaultval}", "defaultval", "", 0, 0), |
| 442 | + // Escaped dollars |
| 443 | + arguments("\\$1 and \\$", "$1 and $", "", 0, 0), |
| 444 | + // Escaped escapes |
| 445 | + arguments("\\\\$1 and ${3:foo}", "\\ and foo", "", 0, 0), |
| 446 | + // Escaped values in a choice |
| 447 | + arguments("${2|a\\,b\\},c|}", "a,b}", "", 0, 0), |
| 448 | + // TM_CURRENT_WORD completion (caret after 'foo') |
| 449 | + arguments("${1:$TM_CURRENT_WORD}", "xx abcabc yy", "xx abc yy", 3, 0), |
| 450 | + arguments("${1:$TM_CURRENT_WORD}", "xx aabcbc yy", "xx abc yy", 4, 0), |
| 451 | + arguments("${1:$TM_CURRENT_WORD}", "xx aabc yy", "xx abc yy", 4, 2), |
| 452 | + // Snippets with syntax errors: |
| 453 | + // Make sure they don't cause endless loops or crashes |
| 454 | + arguments("$", "$", "", 0, 0), // |
| 455 | + arguments("${", "${", "", 0, 0), // |
| 456 | + arguments("$$", "$$", "", 0, 0), // |
| 457 | + arguments("$$TM_LINE_NUMBER", "$1", "", 0, 0), // |
| 458 | + arguments("${VARIABLE", "${VARIABLE", "", 0, 0), // |
| 459 | + arguments("${VARIABLE:", "${VARIABLE:", "", 0, 0), // |
| 460 | + arguments("${VARIABLE:foo", "${VARIABLE:foo", "", 0, 0), // |
| 461 | + arguments("${1|a", "${1|a", "", 0, 0), // |
| 462 | + arguments("${1|a,}", "${1|a,}", "", 0, 0) // |
| 463 | + ); |
| 464 | + |
| 465 | + @ParameterizedTest |
| 466 | + @FieldSource |
| 467 | + public void testComplexSnippets(String completion, String expected, String fileContent, int caretPos, int selectionLen) throws CoreException { |
468 | 468 | CompletionItem completionItem = createCompletionItem( // |
469 | | - test.completion, // |
| 469 | + completion, // |
470 | 470 | CompletionItemKind.Snippet, // |
471 | | - new Range(new Position(0, test.caretPos()), |
472 | | - new Position(0, test.caretPos() + test.selectionLen()))); |
| 471 | + new Range(new Position(0, caretPos), |
| 472 | + new Position(0, caretPos + selectionLen))); |
473 | 473 | completionItem.setInsertTextFormat(InsertTextFormat.Snippet); |
474 | 474 | MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(false, List.of(completionItem))); |
475 | 475 |
|
476 | | - ITextViewer viewer = TestUtils.openTextViewer(TestUtils.createUniqueTestFile(project, test.fileContent)); |
477 | | - viewer.setSelectedRange(test.caretPos(), test.selectionLen()); |
| 476 | + ITextViewer viewer = TestUtils.openTextViewer(TestUtils.createUniqueTestFile(project, fileContent)); |
| 477 | + viewer.setSelectedRange(caretPos, selectionLen); |
478 | 478 |
|
479 | 479 | ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer, |
480 | | - test.caretPos()); |
481 | | - assertEquals(1, proposals.length, "Unexpected proposals length for " + test + " - "); |
| 480 | + caretPos); |
| 481 | + assertEquals(1, proposals.length, "Unexpected proposals length"); |
482 | 482 |
|
483 | | - ((LSCompletionProposal) proposals[0]).apply(viewer, '\n', 0, test.caretPos()); |
484 | | - assertEquals(test.expected, viewer.getDocument().get(), "Unexpected result for " + test + " - "); |
485 | | - } |
| 483 | + ((LSCompletionProposal) proposals[0]).apply(viewer, '\n', 0, caretPos); |
| 484 | + assertEquals(expected, viewer.getDocument().get(), "Unexpected result"); |
486 | 485 | } |
487 | 486 |
|
488 | 487 | @Test |
|
0 commit comments