Skip to content

Commit 74f0d9d

Browse files
committed
refactor(tests): Use @ParameterizedTest instead of looping over test data
1 parent 4721b99 commit 74f0d9d

2 files changed

Lines changed: 45 additions & 46 deletions

File tree

org.eclipse.lsp4e.test/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Require-Bundle: org.eclipse.core.runtime,
3030
org.eclipse.tm4e.ui,
3131
org.eclipse.core.filesystem,
3232
junit-jupiter-api,
33+
junit-jupiter-params,
3334
org.hamcrest,
3435
org.opentest4j
3536
Automatic-Module-Name: org.eclipse.lsp4e.test

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/CompleteCompletionTest.java

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
import org.eclipse.swt.widgets.TableItem;
6464
import org.eclipse.ui.tests.harness.util.DisplayHelper;
6565
import org.junit.jupiter.api.Test;
66+
import org.junit.jupiter.params.ParameterizedTest;
67+
import org.junit.jupiter.params.provider.Arguments;
68+
import org.junit.jupiter.params.provider.FieldSource;
6669

6770
import com.google.gson.JsonPrimitive;
6871

@@ -429,60 +432,55 @@ public void testDuplicateVariable() throws CoreException {
429432
assertEquals("a and a", viewer.getDocument().get());
430433
}
431434

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) {
435+
public static List<Arguments> testComplexSnippets = List.of(
436+
// Variables and escaped dollars
437+
Arguments.arguments("$TM_LINE_NUMBER - \\$TM_LINE_NUMBER - ${TM_LINE_NUMBER} - \\${TM_LINE_NUMBER}",
438+
"1 - $TM_LINE_NUMBER - 1 - ${TM_LINE_NUMBER}", "", 0, 0),
439+
// Default values for variables
440+
Arguments.arguments("${TM_SELECTED_TEXT:defaultval}", "defaultval", "", 0, 0),
441+
// Escaped dollars
442+
Arguments.arguments("\\$1 and \\$", "$1 and $", "", 0, 0),
443+
// Escaped escapes
444+
Arguments.arguments("\\\\$1 and ${3:foo}", "\\ and foo", "", 0, 0),
445+
// Escaped values in a choice
446+
Arguments.arguments("${2|a\\,b\\},c|}", "a,b}", "", 0, 0),
447+
// TM_CURRENT_WORD completion (caret after 'foo')
448+
Arguments.arguments("${1:$TM_CURRENT_WORD}", "xx abcabc yy", "xx abc yy", 3, 0),
449+
Arguments.arguments("${1:$TM_CURRENT_WORD}", "xx aabcbc yy", "xx abc yy", 4, 0),
450+
Arguments.arguments("${1:$TM_CURRENT_WORD}", "xx aabc yy", "xx abc yy", 4, 2),
451+
// Snippets with syntax errors:
452+
// Make sure they don't cause endless loops or crashes
453+
Arguments.arguments("$", "$", "", 0, 0), //
454+
Arguments.arguments("${", "${", "", 0, 0), //
455+
Arguments.arguments("$$", "$$", "", 0, 0), //
456+
Arguments.arguments("$$TM_LINE_NUMBER", "$1", "", 0, 0), //
457+
Arguments.arguments("${VARIABLE", "${VARIABLE", "", 0, 0), //
458+
Arguments.arguments("${VARIABLE:", "${VARIABLE:", "", 0, 0), //
459+
Arguments.arguments("${VARIABLE:foo", "${VARIABLE:foo", "", 0, 0), //
460+
Arguments.arguments("${1|a", "${1|a", "", 0, 0), //
461+
Arguments.arguments("${1|a,}", "${1|a,}", "", 0, 0) //
462+
);
463+
464+
@ParameterizedTest
465+
@FieldSource
466+
public void testComplexSnippets(String completion, String expected, String fileContent, int caretPos, int selectionLen) throws CoreException {
468467
CompletionItem completionItem = createCompletionItem( //
469-
test.completion, //
468+
completion, //
470469
CompletionItemKind.Snippet, //
471-
new Range(new Position(0, test.caretPos()),
472-
new Position(0, test.caretPos() + test.selectionLen())));
470+
new Range(new Position(0, caretPos),
471+
new Position(0, caretPos + selectionLen)));
473472
completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
474473
MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(false, List.of(completionItem)));
475474

476-
ITextViewer viewer = TestUtils.openTextViewer(TestUtils.createUniqueTestFile(project, test.fileContent));
477-
viewer.setSelectedRange(test.caretPos(), test.selectionLen());
475+
ITextViewer viewer = TestUtils.openTextViewer(TestUtils.createUniqueTestFile(project, fileContent));
476+
viewer.setSelectedRange(caretPos, selectionLen);
478477

479478
ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer,
480-
test.caretPos());
481-
assertEquals(1, proposals.length, "Unexpected proposals length for " + test + " - ");
479+
caretPos);
480+
assertEquals(1, proposals.length, "Unexpected proposals length");
482481

483-
((LSCompletionProposal) proposals[0]).apply(viewer, '\n', 0, test.caretPos());
484-
assertEquals(test.expected, viewer.getDocument().get(), "Unexpected result for " + test + " - ");
485-
}
482+
((LSCompletionProposal) proposals[0]).apply(viewer, '\n', 0, caretPos);
483+
assertEquals(expected, viewer.getDocument().get(), "Unexpected result");
486484
}
487485

488486
@Test

0 commit comments

Comments
 (0)