diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyEditingTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyEditingTest.java index 60e2f8b6e1a7..b1c8a98dc867 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyEditingTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyEditingTest.java @@ -1,275 +1,271 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package org.jetbrains.plugins.groovy.lang +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.plugins.groovy.lang; -import com.intellij.openapi.editor.Document -import com.intellij.openapi.editor.ex.EditorSettingsExternalizable -import com.intellij.openapi.fileEditor.FileDocumentManager -import com.intellij.psi.PsiDocumentManager -import com.intellij.testFramework.EditorTestUtil -import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase -import org.jetbrains.annotations.Nullable -import org.jetbrains.plugins.groovy.util.TestUtils +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.ex.EditorSettingsExternalizable; +import com.intellij.openapi.fileEditor.FileDocumentManager; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.testFramework.EditorTestUtil; +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.groovy.util.TestUtils; -class GroovyEditingTest extends LightJavaCodeInsightFixtureTestCase { - final String basePath = TestUtils.testDataPath + "editing/" - - private void doTest(@Nullable String before = null, final String chars, @Nullable String after = null) { +public class GroovyEditingTest extends LightJavaCodeInsightFixtureTestCase { + private void doTest(@Nullable String before, final String chars, @Nullable String after) { if (before != null) { - myFixture.configureByText(getTestName(false) + '.groovy', before) + myFixture.configureByText(getTestName(false) + ".groovy", before); } else { - myFixture.configureByFile(getTestName(false) + ".groovy") + myFixture.configureByFile(getTestName(false) + ".groovy"); } - myFixture.type(chars) + myFixture.type(chars); if (after != null) { - myFixture.checkResult(after) + myFixture.checkResult(after); } else { - myFixture.checkResultByFile(getTestName(false) + "_after.groovy") + myFixture.checkResultByFile(getTestName(false) + "_after.groovy"); } } - void testCodeBlockRightBrace() throws Throwable { doTest('{') } - - void testInterpolationInsideStringRightBrace() throws Throwable { doTest('{') } - - void testStructuralInterpolationInsideStringRightBrace() throws Throwable { doTest('{') } - - void testEnterInMultilineString() throws Throwable { doTest('\n') } - - void testEnterInStringInRefExpr() throws Throwable { doTest('\n') } - - void testEnterInGStringInRefExpr() throws Throwable { doTest('\n') } - - void testPairAngleBracketAfterClassName() throws Throwable { doTest('<') } - - void testPairAngleBracketAfterClassNameOvertype() throws Throwable { doTest('>') } - - void testPairAngleBracketAfterClassNameBackspace() throws Throwable { doTest('\b') } - - void testNoPairLess() throws Throwable { doTest('<') } - - void testTripleString() { - doTest('', "'''", "''''''") + private void doTest(final String chars) { + doTest(null, chars, null); } - void testTripleGString() { - doTest('', '"""', '""""""') + public void testCodeBlockRightBrace() { doTest("{"); } + + public void testInterpolationInsideStringRightBrace() { doTest("{"); } + + public void testStructuralInterpolationInsideStringRightBrace() { doTest("{"); } + + public void testEnterInMultilineString() { doTest("\n"); } + + public void testEnterInStringInRefExpr() { doTest("\n"); } + + public void testEnterInGStringInRefExpr() { doTest("\n"); } + + public void testPairAngleBracketAfterClassName() { doTest("<"); } + + public void testPairAngleBracketAfterClassNameOvertype() { doTest(">"); } + + public void testPairAngleBracketAfterClassNameBackspace() { doTest("\b"); } + + public void testNoPairLess() { doTest("<"); } + + public void testTripleString() { + doTest("", "'''", "''''''"); } - void "test pair brace after doc with mismatch"() { - doTest(''' -class Foo { - /** - * @param o closure to run in {@code ant.zip{ .. }} context - */ - void getProject( Object o ) -} -''', '{', ''' -class Foo { - /** - * @param o closure to run in {@code ant.zip{ .. }} context - */ - void getProject( Object o ) {} -} -''') + public void testTripleGString() { + doTest("", "\"\"\"", "\"\"\"\"\"\""); } - void testRPareth() { - doTest(''' -@Anno( - foo - -''', ')', ''' -@Anno( - foo -) -''') + public void test_pair_brace_after_doc_with_mismatch() { + doTest(""" + class Foo { + /** + * @param o closure to run in {@code ant.zip{ .. }} context + */ + void getProject( Object o ) + } + """, "{", """ + class Foo { + /** + * @param o closure to run in {@code ant.zip{ .. }} context + */ + void getProject( Object o ) {} + } + """); } - void testQuote() { - doTest('''\ -print -''', "'", '''\ -print '' -''') + public void testRPareth() { + doTest(""" + @Anno( + foo + + """, ")", """ + @Anno( + foo + ) + """); } - void testDoubleQuote() { - doTest('''\ -print -''', '"', '''\ -print "" -''') + public void testQuote() { + doTest(""" + print + """, "'", """ + print '' + """); } - void testTripleQuote() { - doTest("""\ -print '' -""", "'", """\ -print '''''' -""") + public void testDoubleQuote() { + doTest(""" + print + """, "\"", """ + print "" + """); } - void testDontInsertTripleQuote1() { - doTest("""\ -print '''''' -""", "'", """\ -print '''''' -""") + public void testTripleQuote() { + doTest(""" + print '' + """, "'", """ + print '''''' + """); } - void testDontInsertTripleQuote2() { - doTest("""\ -print ''' ''' -""", "'", """\ -print ''' ''' -""") + public void testDontInsertTripleQuote1() { + doTest(""" + print '''''' + """, "'", """ + print '''''' + """); } - void testDontInsertTripleQuote3() { - doTest('''\ -print """ "" -''', '"', '''\ -print """ """ -''') + public void testDontInsertTripleQuote2() { + doTest(""" + print ''' ''' + """, "'", """ + print ''' ''' + """); } - void testDontInsertTripleQuote4() { - doTest('''\ -print """ ${} "" -''', '"', '''\ -print """ ${} """ -''') + public void testDontInsertTripleQuote3() { + doTest(""" + print ""\" "" + """, "\"", """ + print ""\" ""\" + """); } - void testSkipQuoteAtLiteralEnd1() { - doTest('''\ -print """ """ -''', '"', '''\ -print """ """ -''') + public void testDontInsertTripleQuote4() { + doTest(""" + print ""\" ${} "" + """, "\"", """ + print ""\" ${} ""\" + """); } - void testSkipQuoteAtLiteralEnd2() { - doTest('''\ -print """ """ -''', '"', '''\ -print """ """ -''') + public void testSkipQuoteAtLiteralEnd1() { + doTest(""" + print ""\" ""\" + """, "\"", """ + print ""\" """ + """); } - void testSkipQuoteAtLiteralEnd3() { - doTest('''\ -print """ """ -''', '"', '''\ -print """ """ -''') + public void testSkipQuoteAtLiteralEnd2() { + doTest(""" + print ""\" """ + """, "\"", """ + print ""\" """ + """); } - void testSkipQuoteAtLiteralEnd4() { - doTest("""\ -print ''' ''' -""", "'", """\ -print ''' ''' -""") + public void testSkipQuoteAtLiteralEnd3() { + doTest(""" + print ""\" """ + """, "\"", """ + print ""\" ""\" + """); } - void testSkipQuoteAtLiteralEnd5() { - doTest("""\ -print ''' ''' -""", "'", """\ -print ''' ''' -""") + public void testSkipQuoteAtLiteralEnd4() { + doTest(""" + print ''' ''' + """, "'", """ + print ''' ''' + """); } - void testSkipQuoteAtLiteralEnd6() { - doTest("""\ -print ''' ''' -""", "'", """\ -print ''' ''' -""") + public void testSkipQuoteAtLiteralEnd5() { + doTest(""" + print ''' ''' + """, "'", """ + print ''' ''' + """); } - void testGroovyDoc() { - doTest('''\ -/** -print 2 -''', '\n', '''\ -/** - * - */ -print 2 -''') + public void testSkipQuoteAtLiteralEnd6() { + doTest(""" + print ''' ''' + """, "'", """ + print ''' ''' + """); } - void testGroovyDoc2() { - doTest('''\ -/** -class A {} -''', '\n', '''\ -/** - * - */ -class A {} -''') + public void testGroovyDoc() { + doTest(""" + /** + print 2 + """, "\n", """ + /** + * + */ + print 2 + """); } - void testParenBeforeString() { - doTest "foo ''", '(', "foo (''" - doTest "foo ''''''", '(', "foo (''''''" - doTest 'foo ""', '(', 'foo (""' - doTest 'foo """"""', '(', 'foo (""""""' - doTest 'foo "$a"', '(', 'foo ("$a"' + public void testGroovyDoc2() { + doTest(""" + /** + class A {} + """, "\n", """ + /** + * + */ + class A {} + """); } - void testBackspace() { - doTest(/''/, '\b', //) - doTest(/''/, '\b', /'/) - doTest(/""/, '\b', //) - doTest(/""/, '\b', /"/) - doTest(/''''''/, '\b', /''/) - doTest(/""""""/, '\b', /""/) + public void testParenBeforeString() { + doTest("foo ''", "(", "foo (''"); + doTest("foo ''''''", "(", "foo (''''''"); + doTest("foo \"\"", "(", "foo (\"\""); + doTest("foo \"\"\"\"\"\"", "(", "foo (\"\"\"\"\"\""); + doTest("foo \"$a\"", "(", "foo (\"$a\""); } - void 'test backslash before closing quote'() { - doTest($/''''''/$, '\\', $/'''\\'''/$) - doTest($/''' '''/$, '\\', $/''' \\'''/$) - doTest("'''\n\n'''", '\\', "'''\n\n\\\\'''") - doTest($/''/$, '\\', $/'\\'/$) - doTest($/' '/$, '\\', $/' \\'/$) + public void testBackspace() { + doTest("''", "\b", ""); + doTest("''", "\b", "'"); + doTest("\"\"", "\b", ""); + doTest("\"\"", "\b", "\""); + doTest("''''''", "\b", "''"); + doTest("\"\"\"\"\"\"", "\b", "\"\""); } - /* - todo uncomment when implemented - void testRegex1() { - doTest('', '/', '//') + public void test_backslash_before_closing_quote() { + doTest("''''''", "\\", "'''\\\\'''"); + doTest("''' '''", "\\", "''' \\\\'''"); + doTest("'''\n\n'''", "\\", "'''\n\n\\\\'''"); + doTest("''", "\\", "'\\\\'"); + doTest("' '", "\\", "' \\\\'"); } - void testRegex2() { - doTest('/a/', '/', '/a/') - } - - void testDollarRegex1() { - doTest('$', '/', '$//$') - }*/ - - void testKeepMultilineStringTrailingSpaces() throws Throwable { - myFixture.configureByFile(getTestName(false) + '.groovy') - EditorSettingsExternalizable editorSettings = EditorSettingsExternalizable.getInstance() - String stripSpaces = editorSettings.getStripTrailingSpaces() + public void testKeepMultilineStringTrailingSpaces() { + myFixture.configureByFile(getTestName(false) + ".groovy"); + EditorSettingsExternalizable editorSettings = EditorSettingsExternalizable.getInstance(); + String stripSpaces = editorSettings.getStripTrailingSpaces(); try { - editorSettings.setStripTrailingSpaces(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE) - Document doc = getEditor().getDocument() - EditorTestUtil.performTypingAction(getEditor(), (char)' ') - PsiDocumentManager.getInstance(getProject()).commitDocument(doc) - FileDocumentManager.getInstance().saveDocument(doc) + editorSettings.setStripTrailingSpaces(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE); + Document doc = getEditor().getDocument(); + EditorTestUtil.performTypingAction(getEditor(), ' '); + PsiDocumentManager.getInstance(getProject()).commitDocument(doc); + FileDocumentManager.getInstance().saveDocument(doc); } finally { - editorSettings.setStripTrailingSpaces(stripSpaces) + editorSettings.setStripTrailingSpaces(stripSpaces); } - myFixture.checkResultByFile(getTestName(false) + "_after.groovy") + + myFixture.checkResultByFile(getTestName(false) + "_after.groovy"); } -} \ No newline at end of file + + @Contract(pure = true) + @Override + public final String getBasePath() { + return TestUtils.getTestDataPath() + "editing/"; + } +}