From 0f929b794100997fbf632ebac7b4169f61f2f992 Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Fri, 9 Nov 2012 18:18:58 +0400 Subject: [PATCH] completion tests refactored --- .../groovy/completion/CompletionResult.groovy | 23 ++++ .../GrCompletionWithLibraryTest.groovy | 7 +- .../completion/GroovyCompletionTest.groovy | 21 ++-- .../GroovyCompletionTestBase.groovy | 113 +++++++++--------- .../GroovySmartCompletionTest.groovy | 6 +- .../EachMethodForMapWithKeyValue.groovy | 4 +- .../EachMethodForMapWithKeyValue_after.groovy | 4 +- .../MultipleCategories_after.groovy | 18 --- ...ClassInStaticMethodCompletion_after.groovy | 10 -- 9 files changed, 105 insertions(+), 101 deletions(-) create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/completion/CompletionResult.groovy delete mode 100644 plugins/groovy/testdata/groovy/completion/MultipleCategories_after.groovy delete mode 100644 plugins/groovy/testdata/groovy/completion/smart/InnerClassInStaticMethodCompletion_after.groovy diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/CompletionResult.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/CompletionResult.groovy new file mode 100644 index 000000000000..23cd232eede4 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/CompletionResult.groovy @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.completion + +/** + * @author Maxim.Medvedev + */ +enum CompletionResult { + contain, equal, notContain +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GrCompletionWithLibraryTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GrCompletionWithLibraryTest.groovy index 73f425c515e0..751657511972 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GrCompletionWithLibraryTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GrCompletionWithLibraryTest.groovy @@ -15,6 +15,7 @@ */ package org.jetbrains.plugins.groovy.completion +import com.intellij.codeInsight.completion.CompletionType import com.intellij.openapi.module.Module import com.intellij.openapi.roots.ContentEntry import com.intellij.openapi.roots.ModifiableRootModel @@ -42,9 +43,9 @@ class GrCompletionWithLibraryTest extends GroovyCompletionTestBase { final String basePath = TestUtils.testDataPath + "groovy/completion/" public void testCategoryMethod() {doBasicTest()} - public void testCategoryProperty() {doBasicTest('\n')} - public void testMultipleCategories() {doBasicTest()} - public void testCategoryForArray() {doBasicTest('\n')} + public void testCategoryProperty() {doCompletionTest(null, null, '\n', CompletionType.BASIC)} + public void testMultipleCategories() {doVariantableTest(null, "", CompletionType.BASIC, CompletionResult.contain, 'getMd5', 'getMd52')} + public void testCategoryForArray() {doCompletionTest(null, null, '\n', CompletionType.BASIC)} public void testArrayLikeAccessForList() throws Throwable {doBasicTest(); } public void testArrayLikeAccessForMap() throws Throwable {doBasicTest();} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy index 3bb489d4fa9b..ffd34d706241 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy @@ -191,7 +191,7 @@ public class GroovyCompletionTest extends GroovyCompletionTestBase { } public void testThisKeywordCompletionAfterClassName2() { - doBasicTest(); + doVariantableTest(null, "", CompletionType.BASIC, CompletionResult.notContain, "this"); } public void testWhileInstanceof() { doBasicTest() } @@ -216,7 +216,7 @@ public class GroovyCompletionTest extends GroovyCompletionTestBase { } public void testTypeCompletionInVariableDeclaration2() { - doBasicTest(); + doVariantableTest(null, "", CompletionType.BASIC, CompletionResult.notContain, "ArrayList"); } public void testTypeCompletionInParameter() { @@ -977,7 +977,7 @@ println "abcd" } public void testNoModifiersAfterDef() { - checkSingleItemCompletion 'def priv', 'def priv' + doVariantableTest('def priv', '', CompletionType.BASIC, CompletionResult.notContain, 'private') } public void testIfSpace() { checkCompletion 'int iff; if', ' ', "int iff; if " } @@ -1425,7 +1425,7 @@ Base b = new Inh<>() } void testDiamondCompletion2() { - doSmartTest('''\ + doCompletionTest('''\ interface Base{} class Inh implements Base{} @@ -1441,7 +1441,7 @@ class Inh implements Base{} def foo(Base b){} foo(new Inh()) -''') +''', CompletionType.SMART) } void testPropertiesOfBaseClass() { @@ -1468,7 +1468,7 @@ class Inheritor extends Base { } void testDiamondCompletionInAssignmentCompletion() { - doSmartTest('''\ + doCompletionTest('''\ class Foo {} Foo var @@ -1478,7 +1478,7 @@ class Foo {} Foo var var = new Foo<>() -''') +''', CompletionType.SMART) } void testDiamondCompletionInAssignmentCompletion2() { @@ -1572,4 +1572,11 @@ setBarrr() void testNewExprDoesntCompleteDef() { doNoVariantsTest('def a = \new ', 'def', 'final') } + + void testThisInScriptCompletion() { + doVariantableTest('''\ +def foo() {} +this. +''', "", CompletionType.BASIC, CompletionResult.contain, 'foo') + } } \ No newline at end of file diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTestBase.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTestBase.groovy index 8dfd44884b66..8f3d414a7817 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTestBase.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTestBase.groovy @@ -14,96 +14,99 @@ * limitations under the License. */ package org.jetbrains.plugins.groovy.completion - import com.intellij.codeInsight.CodeInsightSettings import com.intellij.codeInsight.completion.CompletionType import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase -import com.intellij.util.containers.ContainerUtil -import org.jetbrains.plugins.groovy.GroovyFileType -/** - * @author Maxim.Medvedev - */ + abstract public class GroovyCompletionTestBase extends LightCodeInsightFixtureTestCase { + protected void doSmartTest() { - myFixture.configureByFile(getTestName(false) + ".groovy"); - myFixture.complete(CompletionType.SMART); - checkResult(); + doCompletionTest(CompletionType.SMART) + } + + protected void doBasicTest(String before = null, String after = null) { + doCompletionTest(before, after, CompletionType.BASIC) } protected void doSmartTest(String before, String after) { - myFixture.configureByText('_a.groovy', before) - myFixture.complete(CompletionType.SMART) - assertNull(myFixture.lookupElements) - myFixture.checkResult(after) + doCompletionTest(before, after, CompletionType.SMART) } protected void checkResult() { myFixture.checkResultByFile(getTestName(false) + "_after.groovy", true); } - protected void doBasicTest(String type = "") { - myFixture.testCompletionTyping(getTestName(false) + ".groovy", type, getTestName(false) + "_after.groovy"); + protected void doCompletionTest(String before = null, String after = null, String type = "", CompletionType ct) { + if (before == null) { + myFixture.configureByFile(getTestName(false) + ".groovy") + } + else { + myFixture.configureByText(getTestName(false) + ".groovy", before) + } + + myFixture.complete(ct) + type.each { myFixture.type(it) } + + assertNull(myFixture.lookupElements) + if (after == null) { + myFixture.checkResultByFile(getTestName(false) + "_after.groovy", true) + } + else { + myFixture.checkResult(after, true) + } } - protected void doBasicTest(String before, String after) { - myFixture.configureByText('_a.groovy', before) - myFixture.completeBasic() - assertNull(myFixture.lookupElements) - myFixture.checkResult(after) + protected void doVariantableTest(String before = null, String type = "", CompletionType ct, CompletionResult testType = CompletionResult.equal, String... variants) { + if (before == null) { + myFixture.configureByFile(getTestName(false) + ".groovy") + } + else { + myFixture.configureByText(getTestName(false) + ".groovy", before) + } + + myFixture.complete(ct) + type.each { myFixture.type(it) } + + assertNotNull(myFixture.lookupElements) + + final actual = myFixture.lookupElementStrings + switch (testType) { + case CompletionResult.contain: + assertTrue(actual.containsAll(variants)) + break + case CompletionResult.equal: + assertOrderedEquals(actual, variants) + break + case CompletionResult.notContain: + variants.each { + assertFalse(actual.contains(it)) + } + } } public void doVariantableTest(String... variants) { - myFixture.configureByFile(getTestName(false) + ".groovy"); - myFixture.complete(CompletionType.BASIC); - assertOrderedEquals(myFixture.lookupElementStrings, variants); + doVariantableTest(CompletionType.BASIC, variants) } public void doHasVariantsTest(String... variants) { - myFixture.configureByFile(getTestName(false) + ".groovy"); - myFixture.complete(CompletionType.BASIC); - if (!myFixture.lookupElementStrings.containsAll(variants)) { - assertOrderedEquals(myFixture.lookupElementStrings, variants) - } + doVariantableTest(null, "", CompletionType.BASIC, CompletionResult.contain, variants) } public void doSmartCompletion(String... variants) { - myFixture.configureByFile(getTestName(false) + ".groovy"); - myFixture.complete(CompletionType.SMART); - final List list = myFixture.lookupElementStrings; - assertNotNull(list); - assertOrderedEquals(list, variants); + doVariantableTest(CompletionType.SMART, variants) } - void doSmartVariantableTest(String before, String... variants) { - myFixture.configureByText(getTestName(false) + ".groovy", before); - myFixture.complete(CompletionType.SMART); - final List list = myFixture.lookupElementStrings; - assertNotNull(list); - assertOrderedEquals(list, variants); - } - - public void checkCompletion(String before, String type, String after) { - myFixture.configureByText("a.groovy", before); - myFixture.completeBasic(); - myFixture.type(type); - myFixture.checkResult(after); + doCompletionTest(before, after, type, CompletionType.BASIC) } public void checkSingleItemCompletion(String before, String after) { - myFixture.configureByText("a.groovy", before); - assert !myFixture.completeBasic(); - myFixture.checkResult(after); + doCompletionTest(before, after, CompletionType.BASIC) } public void doNoVariantsTest(String before, String... excludedVariants) { - myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, before) - myFixture.completeBasic() - final excluded = ContainerUtil.newHashSet(excludedVariants) - for (String lookup : myFixture.lookupElementStrings) { - assertFalse(lookup, excluded.contains(lookup)) - } + doVariantableTest(before, "", CompletionType.BASIC, CompletionResult.notContain, excludedVariants) } protected static def caseSensitiveNone() { diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovySmartCompletionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovySmartCompletionTest.groovy index a21e3cb1a0db..01d6a53d31e2 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovySmartCompletionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovySmartCompletionTest.groovy @@ -43,7 +43,7 @@ public class GroovySmartCompletionTest extends GroovyCompletionTestBase { public void testSmartAfterNewInCall() throws Throwable { doSmartTest(); } - public void testInnerClassInStaticMethodCompletion() throws Throwable { doSmartTest(); } + public void testInnerClassInStaticMethodCompletion() throws Throwable { doVariantableTest(null, "", CompletionType.SMART, CompletionResult.notContain, 'Inner'); } public void testSmartCompletionInAssignmentExpression() throws Throwable { doSmartTest(); } @@ -82,9 +82,7 @@ public class GroovySmartCompletionTest extends GroovyCompletionTestBase { public void testEnumMembersInAssignmentInsideEnum() {doSmartCompletion "IN_STOCK", "NOWHERE", "ORDERED", "next", "previous" } public void testPreferVarargElement() { - doSmartCompletion "Foo", "Foo" - myFixture.type('\n') - checkResult() + doCompletionTest(null, null, '\n', CompletionType.SMART) } public void testGlobalStaticMembers() { diff --git a/plugins/groovy/testdata/groovy/completion/EachMethodForMapWithKeyValue.groovy b/plugins/groovy/testdata/groovy/completion/EachMethodForMapWithKeyValue.groovy index 0707b88a6e78..e6aeea1019b0 100644 --- a/plugins/groovy/testdata/groovy/completion/EachMethodForMapWithKeyValue.groovy +++ b/plugins/groovy/testdata/groovy/completion/EachMethodForMapWithKeyValue.groovy @@ -1,5 +1,5 @@ -Map map=["2":2, "3":3]; +Map map=["2":2, "3":3]; map.each{ key, value-> - key.codePoint + key.codePointA } \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/completion/EachMethodForMapWithKeyValue_after.groovy b/plugins/groovy/testdata/groovy/completion/EachMethodForMapWithKeyValue_after.groovy index 0707b88a6e78..3d23e5d88823 100644 --- a/plugins/groovy/testdata/groovy/completion/EachMethodForMapWithKeyValue_after.groovy +++ b/plugins/groovy/testdata/groovy/completion/EachMethodForMapWithKeyValue_after.groovy @@ -1,5 +1,5 @@ -Map map=["2":2, "3":3]; +Map map=["2":2, "3":3]; map.each{ key, value-> - key.codePoint + key.codePointAt() } \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/completion/MultipleCategories_after.groovy b/plugins/groovy/testdata/groovy/completion/MultipleCategories_after.groovy deleted file mode 100644 index 5342fb2fc7c5..000000000000 --- a/plugins/groovy/testdata/groovy/completion/MultipleCategories_after.groovy +++ /dev/null @@ -1,18 +0,0 @@ -class StringCategory { - static String getMd5(String s) { - return "MD5Value";//suppose to be some code that generate md5 - } -} - -class StringCategory2 { - static String getMd52(String s) { - return "MD5Value"; - } -} - - - -use(StringCategory, StringCategory2) { - println "hello world".getMd5 -} - diff --git a/plugins/groovy/testdata/groovy/completion/smart/InnerClassInStaticMethodCompletion_after.groovy b/plugins/groovy/testdata/groovy/completion/smart/InnerClassInStaticMethodCompletion_after.groovy deleted file mode 100644 index 754d9f9db692..000000000000 --- a/plugins/groovy/testdata/groovy/completion/smart/InnerClassInStaticMethodCompletion_after.groovy +++ /dev/null @@ -1,10 +0,0 @@ -class A{ - class Inner { - - } - - static def foo() { - Inner i=new - } - -} \ No newline at end of file