Skip to content

Commit

Permalink
Fix for #1549: update editor templates and rework import rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 15, 2024
1 parent 6fd609d commit 0df7d17
Show file tree
Hide file tree
Showing 29 changed files with 457 additions and 374 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,7 +49,7 @@ final class GroovyTemplatesCompletionTests extends QuickFixTestSuite {
// find proposal
def proposals = new TemplateProposalComputer().computeCompletionProposals(context, null)
assert proposals != null && !proposals.empty : 'Expected some proposals, but got none'
def matches = proposals.findAll { it.displayString.startsWith(which + ' - ') }
def matches = proposals.findAll { it.displayString.startsWith(which) }
assert matches.size() == 1 : 'Expected a match, but got ' + matches.size()

// apply template
Expand Down Expand Up @@ -98,7 +98,7 @@ final class GroovyTemplatesCompletionTests extends QuickFixTestSuite {
String output = '''\
|try {
| #
|} catch (Exception e) {
|} catch (e) {
| e.printStackTrace()
|}
|'''.stripMargin()
Expand All @@ -110,44 +110,210 @@ final class GroovyTemplatesCompletionTests extends QuickFixTestSuite {
void testJUnitBefore() {
//@formatter:off
String input = '''\
|final class SomeTest {
|final class Spec {
| Bef
|}
|'''.stripMargin()
String output = '''\
|import org.junit.Before
|
|final class SomeTest {
|final class Spec {
| @Before
| void before() {
| void setUp() {
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'Bef', 'Before')
runTest(input, output, 'Bef', 'Before ')
}

@Test
void testJUnitBeforeEach() {
//@formatter:off
String input = '''\
|final class Spec {
| Bef
|}
|'''.stripMargin()
String output = '''\
|import org.junit.jupiter.api.BeforeEach
|
|final class Spec {
| @BeforeEach
| void setUp() {
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'Bef', 'BeforeEach')
}

@Test
void testJUnitAfter() {
//@formatter:off
String input = '''\
|final class SomeTest {
|final class Spec {
| Aft
|}
|'''.stripMargin()
String output = '''\
|import org.junit.After
|
|final class SomeTest {
|final class Spec {
| @After
| void after() {
| void tearDown() {
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'Aft', 'After ')
}

@Test
void testJUnitAfterEach() {
//@formatter:off
String input = '''\
|final class Spec {
| Aft
|}
|'''.stripMargin()
String output = '''\
|import org.junit.jupiter.api.AfterEach
|
|final class Spec {
| @AfterEach
| void tearDown() {
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'Aft', 'AfterEach')
}

@Test
void testJUnit3TestCase() {
//@formatter:off
String input = '''\
|final class Spec {
| tes
|}
|'''.stripMargin()
String output = '''\
|final class Spec {
| void testName() {
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'tes', 'test')
}

@Test
void testJUnit4TestCase() {
//@formatter:off
String input = '''\
|final class Spec {
| Tes
|}
|'''.stripMargin()
String output = '''\
|import static org.junit.Assert.*
|import static org.junit.Assume.*
|
|import org.junit.Test
|
|final class Spec {
| @Test
| void testName() {
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'Tes', 'Test - test method (JUnit 4)')
}

@Test
void testJUnit5TestCase() {
//@formatter:off
String input = '''\
|final class Spec {
| Tes
|}
|'''.stripMargin()
String output = '''\
|import static org.junit.jupiter.api.Assertions.*
|import static org.junit.jupiter.api.Assumptions.*
|
|import org.junit.jupiter.api.DisplayName
|import org.junit.jupiter.api.Test
|
|final class Spec {
| @Test @DisplayName('scenario_description')
| void testName() {
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'Tes', 'Test - test method (JUnit 5)')
}

@Test
void testJUnit5TestCases() {
//@formatter:off
String input = '''\
|final class Spec {
| Tes
|}
|'''.stripMargin()
String output = '''\
|import org.junit.jupiter.params.ParameterizedTest
|import org.junit.jupiter.params.provider.*
|
|final class Spec {
| @ParameterizedTest @MethodSource()
| void testName(input) {
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'Tes', 'Test - tests method (JUnit 5)')
}

@Test
void testJUnit5TestFactory() {
//@formatter:off
String input = '''\
|final class Spec {
| Tes
|}
|'''.stripMargin()
String output = '''\
|import static org.junit.jupiter.api.Assertions.*
|import static org.junit.jupiter.api.Assumptions.*
|import static org.junit.jupiter.api.DynamicContainer.*
|import static org.junit.jupiter.api.DynamicTest.*
|
|import org.junit.jupiter.api.DynamicNode
|import org.junit.jupiter.api.TestFactory
|
|final class Spec {
| @TestFactory
| DynamicNode testFactoryName() {
| // TODO: generate dynamic tests
| #
| }
|}
|'''.stripMargin()
//@formatter:on
runTest(input, output, 'Aft', 'After')
runTest(input, output, 'Tes', 'Test - test factory method (JUnit 5)')
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2019 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,9 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.internal.corext.template.java.AbstractJavaContextType;
import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext;
import org.eclipse.jdt.internal.corext.template.java.ImportsResolver;
import org.eclipse.jdt.internal.corext.template.java.JavaContext;
import org.eclipse.jdt.internal.corext.template.java.LinkResolver;
import org.eclipse.jdt.internal.corext.template.java.StaticImportResolver;
import org.eclipse.jdt.internal.corext.template.java.TypeResolver;
import org.eclipse.jface.text.IDocument;
Expand All @@ -30,7 +32,7 @@ public class GroovyContextType extends AbstractJavaContextType {

public GroovyContextType() {
setId(GroovyQuickFixPlugin.GROOVY_CONTEXT_TYPE);
setName("Groovy surround-with templates");
setName("Groovy code templates");
initializeContextTypeResolvers();
}

Expand All @@ -56,7 +58,8 @@ public void initializeContextTypeResolvers() {
// global
addResolver(new GlobalTemplateVariables.Cursor());
addResolver(new GlobalTemplateVariables.WordSelection());
addResolver(new GlobalTemplateVariables.Selection(GlobalTemplateVariables.LineSelection.NAME, /*org.eclipse.jdt.internal.corext.template.java.JavaTemplateMessages.CompilationUnitContextType_variable_description_line_selection:*/
addResolver(new GlobalTemplateVariables.Selection(GlobalTemplateVariables.LineSelection.NAME,
//org.eclipse.jdt.internal.corext.template.java.JavaTemplateMessages.CompilationUnitContextType_variable_description_line_selection:
"<b>${id\\:line_selection[(default)]}</b><br>Evaluates to the selected text for multiple lines. 'default' is an optional parameter, which specifies the text if the selected text is empty.<br><br>Templates that contain this variable will also be shown in the 'Source &gt; Surround With > ...' menu.<br><br><b>Examples:</b><br><code>${line_selection}</code><br><code>${currentLine:line_selection(myStringVariable)}</code><br><code>${currentLine:line_selection('\"A default text\"')}</code>"));
addResolver(new GlobalTemplateVariables.Dollar());
addResolver(new GlobalTemplateVariables.Date());
Expand All @@ -66,13 +69,13 @@ public void initializeContextTypeResolvers() {

// compilation unit
addResolver(new File());
//addResolver(new PrimaryTypeName());
//addResolver(new ReturnType());
//addResolver(new Method());
addResolver(new PrimaryTypeName());
addResolver(new ReturnType());
addResolver(new Method());
addResolver(new Type());
addResolver(new Package());
addResolver(new Project());
//addResolver(new Arguments());
addResolver(new Arguments());

// java
addResolver(new Array());
Expand All @@ -87,7 +90,9 @@ public void initializeContextTypeResolvers() {
addResolver(new Todo());

// groovy
addResolver(new StaticImportResolver("importStatic", "adds a static import"));
addResolver(new StaticImportResolver("importStatic", "adds static import(s)"));
addResolver(new ImportsResolver("import", "adds type import(s)"));
addResolver(new LinkResolver("link", "list of choices"));
TypeResolver resolver = new TypeResolver();
resolver.setType("newType");
addResolver(resolver);
Expand Down
Loading

0 comments on commit 0df7d17

Please sign in to comment.