Skip to content

Commit ae840f6

Browse files
refactor(tests): Use @ParameterizedTest instead of looping over test data (#1405)
1 parent cd2c369 commit ae840f6

2 files changed

Lines changed: 46 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: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.junit.jupiter.api.Assertions.assertFalse;
1919
import static org.junit.jupiter.api.Assertions.assertNotNull;
2020
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
import static org.junit.jupiter.params.provider.Arguments.arguments;
2122

2223
import java.net.URI;
2324
import java.util.ArrayList;
@@ -63,6 +64,9 @@
6364
import org.eclipse.swt.widgets.TableItem;
6465
import org.eclipse.ui.tests.harness.util.DisplayHelper;
6566
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;
6670

6771
import com.google.gson.JsonPrimitive;
6872

@@ -429,60 +433,55 @@ public void testDuplicateVariable() throws CoreException {
429433
assertEquals("a and a", viewer.getDocument().get());
430434
}
431435

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 {
468468
CompletionItem completionItem = createCompletionItem( //
469-
test.completion, //
469+
completion, //
470470
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)));
473473
completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
474474
MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(false, List.of(completionItem)));
475475

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);
478478

479479
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");
482482

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");
486485
}
487486

488487
@Test

0 commit comments

Comments
 (0)