From 6fa3ee99d955ed60985668b2f69adbd38e4f0517 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Wed, 23 Jan 2019 20:13:50 +0300 Subject: [PATCH] [groovy] spock: use real jar in tests --- .../plugins/groovy/ext/spock/SpockTest.groovy | 156 +++++++----------- .../groovy/ext/spock/SpockTestBase.groovy | 20 +++ .../plugins/groovy/util/HighlightingTest.java | 15 +- .../plugins/groovy/util/TypingTest.java | 6 +- 4 files changed, 96 insertions(+), 101 deletions(-) create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/ext/spock/SpockTestBase.groovy diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/ext/spock/SpockTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/ext/spock/SpockTest.groovy index 231f05067659..f6c931914001 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/ext/spock/SpockTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/ext/spock/SpockTest.groovy @@ -1,43 +1,23 @@ -// 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. +// Copyright 2000-2019 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.ext.spock -import com.intellij.codeInsight.lookup.LookupElement -import com.intellij.psi.CommonClassNames -import com.intellij.psi.PsiFile -import com.intellij.psi.PsiVariable -import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase -import com.intellij.util.containers.ContainerUtil +import groovy.transform.CompileStatic import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection import org.jetbrains.plugins.groovy.codeInspection.declaration.GrMethodMayBeStaticInspection import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection +import org.jetbrains.plugins.groovy.util.HighlightingTest +import org.jetbrains.plugins.groovy.util.TypingTest +import org.junit.Ignore +import org.junit.Test -/** - * @author Sergey Evdokimov - */ -class SpockTest extends LightCodeInsightFixtureTestCase { - - @Override - protected void setUp() { - super.setUp() - - myFixture.addFileToProject("spock/lang/Specification.groovy", """ -package spock.lang; - -class Specification { - -} -""") - myFixture.addClass '''\ -package groovy.lang; -public class Closure { - T call(Object ... args) { return null; } -} -''' - } +import static com.intellij.psi.CommonClassNames.JAVA_LANG_STRING +@CompileStatic +class SpockTest extends SpockTestBase implements TypingTest, HighlightingTest { + @Test void testCompletion() { - def file = myFixture.addFileToProject("FooSpec.groovy", """ + configureByText '''\ class FooSpec extends spock.lang.Specification { def "foo test"() { expect: @@ -52,40 +32,37 @@ class FooSpec extends spock.lang.Specification { ""|""|""||"" } } -""") - - checkCompletionStatic(file, "varAssigment", "varShl", "varShl1", "varShl2", "varShl3", "varTable1", "varTable2", "varTable3", "varTable4") - } - - private void doTest(String expectedType, String text) { - myFixture.configureByText("FooSpec.groovy", text) - - def var = myFixture.elementAtCaret - assertInstanceOf(var, PsiVariable) - assertEquals(expectedType, ((PsiVariable)var).type.canonicalText) +''' + fixture.completeBasic() + fixture.assertPreferredCompletionItems( + 0, + "varAssigment", "varShl", "varShl1", "varShl2", "varShl3", "varTable1", "varTable2", "varTable3", "varTable4" + ) } + @Test void testEquals() { - doTest(CommonClassNames.JAVA_LANG_STRING, """ + typingTest '''\ class FooSpec extends spock.lang.Specification { def "foo test"() { expect: - name + name where: name = "xxx" } } -""") +''', JAVA_LANG_STRING } + @Test void testTable() { - doTest("java.util.List", """ + typingTest '''\ class FooSpec extends spock.lang.Specification { def "foo test"() { List list = zzz() expect: - name + name where: varTable1|varT.a.ble2|varTable3|name @@ -94,57 +71,61 @@ class FooSpec extends spock.lang.Specification { ""|""|""|null } } -""") +''', 'java.util.List' } + @Test void testShlSimple() { - doTest(AbstractMap.name, """ + typingTest '''\ class FooSpec extends spock.lang.Specification { def "foo test"() { expect: - name + name where: name << [new HashMap(), new TreeMap(), [aaa:1, bbb:2], null] } } -""") +''', '[java.util.AbstractMap,java.lang.Cloneable,java.io.Serializable]' } + @Test void testShlMulti1() { - doTest(CommonClassNames.JAVA_LANG_STRING, """ + typingTest '''\ class FooSpec extends spock.lang.Specification { def "foo test"() { def c = { return "1111" } expect: - name + name where: [x1, _, name] << [['x', 'y', c()], ['x', 'y', null]] } } -""") +''', JAVA_LANG_STRING } + @Test void testShlMulti2() { - doTest(CommonClassNames.JAVA_LANG_STRING, """ + typingTest '''\ class FooSpec extends spock.lang.Specification { def "foo test"() { def list = ["a", "b", "c"] expect: - name + name where: [x1, _, name] << [list, ['aaa', 'bbb', 'ccc']] } } -""") +''', JAVA_LANG_STRING } + @Test void testRename() { - myFixture.configureByText("FooSpec.groovy", """ + fixture.configureByText("FooSpec.groovy", """ class FooSpec extends spock.lang.Specification { @spock.lang.Unroll("xxx #name a #name #name #name") def "foo test"() { @@ -157,9 +138,9 @@ class FooSpec extends spock.lang.Specification { } """) - myFixture.renameElementAtCaret("n") + fixture.renameElementAtCaret("n") - myFixture.checkResult(""" + fixture.checkResult(""" class FooSpec extends spock.lang.Specification { @spock.lang.Unroll("xxx #n a #n #n #n") def "foo test"() { @@ -172,9 +153,9 @@ class FooSpec extends spock.lang.Specification { } """) - myFixture.renameElementAtCaret("z1234567890") + fixture.renameElementAtCaret("z1234567890") - myFixture.checkResult(""" + fixture.checkResult(""" class FooSpec extends spock.lang.Specification { @spock.lang.Unroll("xxx #z1234567890 a #z1234567890 #z1234567890 #z1234567890") def "foo test"() { @@ -186,45 +167,26 @@ class FooSpec extends spock.lang.Specification { } } """) - - } - - - void checkCompletionStatic(PsiFile file, String... expectedVariants) { - myFixture.configureFromExistingVirtualFile(file.getVirtualFile()) - - LookupElement[] lookupElements = myFixture.completeBasic() - - assertNotNull(lookupElements) - - Set missedVariants = ContainerUtil.newHashSet(expectedVariants) - - for (LookupElement lookupElement : lookupElements) { - missedVariants.remove(lookupElement.getLookupString()) - } - - assertEmpty("Some completion variants are missed", missedVariants) } + @Ignore("unignore when IDEA-205861 is fixed") + @Test void testVariable_resolved() { - myFixture.enableInspections(GroovyAssignabilityCheckInspection, GrUnresolvedAccessInspection) - - myFixture.configureByText("FooSpec.groovy", """\ + highlightingTest '''\ class FooSpec extends spock.lang.Specification { def "foo test"() { - String subscriber = Mock() + String subscriber = Mock() then: (0.._) * subscriber.concat(_) subscriber.concat(asdasdasd) subscriber.concat(23) } } -""") - - myFixture.checkHighlighting(true, false, true) +''', GroovyAssignabilityCheckInspection, GrUnresolvedAccessInspection } + @Test void testVariable_NotExistingInCompletion() { - myFixture.configureByText("FooSpec.groovy", """ + fixture.configureByText("FooSpec.groovy", """ class FooSpec extends spock.lang.Specification { def "foo test"() { String subscriber = Mock() @@ -232,13 +194,17 @@ class FooSpec extends spock.lang.Specification { } } """) - myFixture.completeBasic() - def elements = myFixture.getLookupElementStrings() - assert !elements.contains("_") + fixture.completeBasic() + def elements = fixture.getLookupElementStrings() + assert elements.contains("_") } + @Ignore("see com.intellij.execution.junit2.inspection.JUnitCantBeStaticExtension") + @Test void 'test method may be static'() { - myFixture.configureByText 'specs.groovy', '''\ + def inspection = new GrMethodMayBeStaticInspection() + inspection.myIgnoreEmptyMethods = false + highlightingTest '''\ class SomeSpec extends spock.lang.Specification { def cleanup() {} def setupSpec() {} @@ -247,10 +213,6 @@ class SomeSpec extends spock.lang.Specification { expect: 1 == 1 } } -''' - def inspection = new GrMethodMayBeStaticInspection() - inspection.myIgnoreEmptyMethods = false - myFixture.enableInspections inspection - myFixture.checkHighlighting() +''', inspection } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/ext/spock/SpockTestBase.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/ext/spock/SpockTestBase.groovy new file mode 100644 index 000000000000..59dbc898db1f --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/ext/spock/SpockTestBase.groovy @@ -0,0 +1,20 @@ +// Copyright 2000-2019 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.ext.spock + +import com.intellij.testFramework.LightProjectDescriptor +import org.jetbrains.plugins.groovy.GroovyProjectDescriptors +import org.jetbrains.plugins.groovy.LibraryLightProjectDescriptor +import org.jetbrains.plugins.groovy.RepositoryTestLibrary +import org.jetbrains.plugins.groovy.util.LightProjectTest + +abstract class SpockTestBase extends LightProjectTest { + + private static final LightProjectDescriptor SPOCK_PROJECT = new LibraryLightProjectDescriptor( + GroovyProjectDescriptors.LIB_GROOVY_2_4 + new RepositoryTestLibrary("org.spockframework:spock-core:1.2-groovy-2.4") + ) + + @Override + final LightProjectDescriptor getProjectDescriptor() { + SPOCK_PROJECT + } +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/util/HighlightingTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/util/HighlightingTest.java index c5c5a591557f..94e01b30fed5 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/util/HighlightingTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/util/HighlightingTest.java @@ -1,8 +1,7 @@ -// 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. +// Copyright 2000-2019 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.util; import com.intellij.codeInspection.LocalInspectionTool; -import org.intellij.lang.annotations.Language; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -26,9 +25,19 @@ public interface HighlightingTest extends BaseTest { getFixture().testHighlighting(getTestName() + ".groovy"); } - default void highlightingTest(@Language("Groovy") String text) { + default void highlightingTest(String text) { getFixture().enableInspections(getInspections()); configureByText(text); getFixture().checkHighlighting(); } + + default void highlightingTest(String text, LocalInspectionTool... inspections) { + getFixture().enableInspections(inspections); + highlightingTest(text); + } + + default void highlightingTest(String text, Class... inspections) { + getFixture().enableInspections(inspections); + highlightingTest(text); + } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/util/TypingTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/util/TypingTest.java index 680be81ad85e..009311e01c55 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/util/TypingTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/util/TypingTest.java @@ -1,4 +1,4 @@ -// 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. +// Copyright 2000-2019 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.util; import org.intellij.lang.annotations.Language; @@ -14,6 +14,10 @@ public interface TypingTest extends BaseTest { typingTest(lastExpression(text), expectedType); } + default void typingTest(@NotNull String text, @Nullable String expectedType) { + typingTest(elementUnderCaret(text, GrExpression.class), expectedType); + } + default void typingTest(@NotNull String text, @NotNull Class clazz, @Nullable String expectedType) { typingTest(elementUnderCaret(text, clazz), expectedType); }