From 46f56bfa3b9af33dd0aeb2518acdd13a0d2809f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kroi=C3=9F=2C=20Florian?= Date: Sun, 16 Nov 2025 19:47:11 +0100 Subject: [PATCH] refactor(tests): Use @ParameterizedTest instead of looping over test data --- org.eclipse.lsp4e.test/META-INF/MANIFEST.MF | 1 + .../completion/CompleteCompletionTest.java | 91 +++++++++---------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/org.eclipse.lsp4e.test/META-INF/MANIFEST.MF b/org.eclipse.lsp4e.test/META-INF/MANIFEST.MF index 6fc839b54..e6ca473f2 100644 --- a/org.eclipse.lsp4e.test/META-INF/MANIFEST.MF +++ b/org.eclipse.lsp4e.test/META-INF/MANIFEST.MF @@ -30,6 +30,7 @@ Require-Bundle: org.eclipse.core.runtime, org.eclipse.tm4e.ui, org.eclipse.core.filesystem, junit-jupiter-api, + junit-jupiter-params, org.hamcrest, org.opentest4j Automatic-Module-Name: org.eclipse.lsp4e.test diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/CompleteCompletionTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/CompleteCompletionTest.java index d35086b18..e9220780a 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/CompleteCompletionTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/CompleteCompletionTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.params.provider.Arguments.arguments; import java.net.URI; import java.util.ArrayList; @@ -63,6 +64,9 @@ import org.eclipse.swt.widgets.TableItem; import org.eclipse.ui.tests.harness.util.DisplayHelper; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.FieldSource; import com.google.gson.JsonPrimitive; @@ -429,60 +433,55 @@ public void testDuplicateVariable() throws CoreException { assertEquals("a and a", viewer.getDocument().get()); } - @Test - public void testComplexSnippets() throws CoreException { - record Test(String completion, String expected, String fileContent, int caretPos, int selectionLen) { - Test(String completion, String expected) { - this(completion, expected, "", 0, 0); - } - } - Test[] tests = { - // Variables and escaped dollars - new Test("$TM_LINE_NUMBER - \\$TM_LINE_NUMBER - ${TM_LINE_NUMBER} - \\${TM_LINE_NUMBER}", - "1 - $TM_LINE_NUMBER - 1 - ${TM_LINE_NUMBER}"), - // Default values for variables - new Test("${TM_SELECTED_TEXT:defaultval}", "defaultval"), - // Escaped dollars - new Test("\\$1 and \\$", "$1 and $"), - // Escaped escapes - new Test("\\\\$1 and ${3:foo}", "\\ and foo"), - // Escaped values in a choice - new Test("${2|a\\,b\\},c|}", "a,b}"), - // TM_CURRENT_WORD completion (caret after 'foo') - new Test("${1:$TM_CURRENT_WORD}", "xx abcabc yy", "xx abc yy", 3, 0), - new Test("${1:$TM_CURRENT_WORD}", "xx aabcbc yy", "xx abc yy", 4, 0), - new Test("${1:$TM_CURRENT_WORD}", "xx aabc yy", "xx abc yy", 4, 2), - // Snippets with syntax errors: - // Make sure they don't cause endless loops or crashes - new Test("$", "$"), // - new Test("${", "${"), // - new Test("$$", "$$"), // - new Test("$$TM_LINE_NUMBER", "$1"), // - new Test("${VARIABLE", "${VARIABLE"), // - new Test("${VARIABLE:", "${VARIABLE:"), // - new Test("${VARIABLE:foo", "${VARIABLE:foo"), // - new Test("${1|a", "${1|a"), // - new Test("${1|a,}", "${1|a,}"), // - }; - for (Test test : tests) { + public static List testComplexSnippets = List.of( + // Variables and escaped dollars + arguments("$TM_LINE_NUMBER - \\$TM_LINE_NUMBER - ${TM_LINE_NUMBER} - \\${TM_LINE_NUMBER}", + "1 - $TM_LINE_NUMBER - 1 - ${TM_LINE_NUMBER}", "", 0, 0), + // Default values for variables + arguments("${TM_SELECTED_TEXT:defaultval}", "defaultval", "", 0, 0), + // Escaped dollars + arguments("\\$1 and \\$", "$1 and $", "", 0, 0), + // Escaped escapes + arguments("\\\\$1 and ${3:foo}", "\\ and foo", "", 0, 0), + // Escaped values in a choice + arguments("${2|a\\,b\\},c|}", "a,b}", "", 0, 0), + // TM_CURRENT_WORD completion (caret after 'foo') + arguments("${1:$TM_CURRENT_WORD}", "xx abcabc yy", "xx abc yy", 3, 0), + arguments("${1:$TM_CURRENT_WORD}", "xx aabcbc yy", "xx abc yy", 4, 0), + arguments("${1:$TM_CURRENT_WORD}", "xx aabc yy", "xx abc yy", 4, 2), + // Snippets with syntax errors: + // Make sure they don't cause endless loops or crashes + arguments("$", "$", "", 0, 0), // + arguments("${", "${", "", 0, 0), // + arguments("$$", "$$", "", 0, 0), // + arguments("$$TM_LINE_NUMBER", "$1", "", 0, 0), // + arguments("${VARIABLE", "${VARIABLE", "", 0, 0), // + arguments("${VARIABLE:", "${VARIABLE:", "", 0, 0), // + arguments("${VARIABLE:foo", "${VARIABLE:foo", "", 0, 0), // + arguments("${1|a", "${1|a", "", 0, 0), // + arguments("${1|a,}", "${1|a,}", "", 0, 0) // + ); + + @ParameterizedTest + @FieldSource + public void testComplexSnippets(String completion, String expected, String fileContent, int caretPos, int selectionLen) throws CoreException { CompletionItem completionItem = createCompletionItem( // - test.completion, // + completion, // CompletionItemKind.Snippet, // - new Range(new Position(0, test.caretPos()), - new Position(0, test.caretPos() + test.selectionLen()))); + new Range(new Position(0, caretPos), + new Position(0, caretPos + selectionLen))); completionItem.setInsertTextFormat(InsertTextFormat.Snippet); MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(false, List.of(completionItem))); - ITextViewer viewer = TestUtils.openTextViewer(TestUtils.createUniqueTestFile(project, test.fileContent)); - viewer.setSelectedRange(test.caretPos(), test.selectionLen()); + ITextViewer viewer = TestUtils.openTextViewer(TestUtils.createUniqueTestFile(project, fileContent)); + viewer.setSelectedRange(caretPos, selectionLen); ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer, - test.caretPos()); - assertEquals(1, proposals.length, "Unexpected proposals length for " + test + " - "); + caretPos); + assertEquals(1, proposals.length, "Unexpected proposals length"); - ((LSCompletionProposal) proposals[0]).apply(viewer, '\n', 0, test.caretPos()); - assertEquals(test.expected, viewer.getDocument().get(), "Unexpected result for " + test + " - "); - } + ((LSCompletionProposal) proposals[0]).apply(viewer, '\n', 0, caretPos); + assertEquals(expected, viewer.getDocument().get(), "Unexpected result"); } @Test