- Tests for OUT_OF_CODE_BLOCK_MODIFICATION behaviour
- Enable behaviour for property accessors
This commit is contained in:
@@ -26,7 +26,9 @@ public final class JetModifiableBlockHelper {
|
||||
private JetModifiableBlockHelper() {
|
||||
}
|
||||
|
||||
// TODO: Need tests for this method
|
||||
/**
|
||||
* Tested in OutOfBlockModificationTest
|
||||
*/
|
||||
public static boolean shouldChangeModificationCount(PsiElement place) {
|
||||
JetDeclaration declaration = PsiTreeUtil.getParentOfType(place, JetDeclaration.class, true);
|
||||
if (declaration != null) {
|
||||
@@ -38,6 +40,9 @@ public final class JetModifiableBlockHelper {
|
||||
|
||||
return shouldChangeModificationCount(function);
|
||||
}
|
||||
else if (declaration instanceof JetPropertyAccessor) {
|
||||
return false;
|
||||
}
|
||||
else if (declaration instanceof JetProperty) {
|
||||
JetProperty property = (JetProperty) declaration;
|
||||
if (property.getPropertyTypeRef() != null) {
|
||||
@@ -47,6 +52,7 @@ public final class JetModifiableBlockHelper {
|
||||
return shouldChangeModificationCount(property);
|
||||
}
|
||||
else if (declaration instanceof JetFunctionLiteral) {
|
||||
// TODO: Check return type
|
||||
return shouldChangeModificationCount(declaration);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// FALSE
|
||||
fun main() {
|
||||
fun some = 12<caret>
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// TRUE
|
||||
fun some() = <caret>12
|
||||
@@ -0,0 +1,4 @@
|
||||
// TRUE
|
||||
class Some {
|
||||
fun <caret>
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// FALSE
|
||||
class Test {
|
||||
val more : Int = 0
|
||||
val test : Int
|
||||
get() {
|
||||
<caret>
|
||||
return more
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// TRUE
|
||||
fun more() = { println<caret> }
|
||||
@@ -0,0 +1,5 @@
|
||||
// FALSE
|
||||
|
||||
fun main() {
|
||||
jq() { pri<caret> }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// FALSE
|
||||
|
||||
fun test() {
|
||||
val a = 1<caret>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// FALSE
|
||||
class Test {
|
||||
val a : () -> Int = { <caret>pri }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// TRUE
|
||||
class Test {
|
||||
val a = "aasdf<caret>"
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 2010-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.jet.plugin.codeInsight;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.JavaCodeBlockModificationListener;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public class OutOfBlockModificationTest extends LightCodeInsightFixtureTestCase {
|
||||
|
||||
public void testFunInFun() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFunNoBody() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInClass() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInClassPropertyAccessor() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInFunctionLiteral() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInFunWithInference() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInMethod() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInPropertyWithFunctionLiteral() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInPropertyWithInference() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myFixture.setTestDataPath(PluginTestCaseBase.getTestDataPathBase() + "/codeInsight/outOfBlock");
|
||||
}
|
||||
|
||||
protected void doTest() {
|
||||
myFixture.configureByFile(getTestName(false) + ".kt");
|
||||
String text = myFixture.getDocument(myFixture.getFile()).getText();
|
||||
|
||||
boolean expectedOutOfBlock = false;
|
||||
if (text.startsWith("// TRUE")) {
|
||||
expectedOutOfBlock = true;
|
||||
}
|
||||
else if (text.startsWith("// FALSE")) {
|
||||
expectedOutOfBlock = false;
|
||||
}
|
||||
else {
|
||||
fail("Expectation of code block result test should be configured with " +
|
||||
"\"// TRUE\" or \"// FALSE\" directive in the beginning of the file");
|
||||
}
|
||||
|
||||
PsiElement element = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
|
||||
assertNotNull("Should be valid element", element);
|
||||
|
||||
assertEquals("Result for out of block test is differs from expected on element " + element,
|
||||
!expectedOutOfBlock, isInsideCodeBlock(element));
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy of private {@link JavaCodeBlockModificationListener.isInsideCodeBlock()}
|
||||
*/
|
||||
@SuppressWarnings("JavadocReference")
|
||||
private static boolean isInsideCodeBlock(PsiElement element) {
|
||||
if (element instanceof PsiFileSystemItem) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (element == null || element.getParent() == null) return true;
|
||||
|
||||
PsiElement parent = element;
|
||||
while (true) {
|
||||
if (parent instanceof PsiFile || parent instanceof PsiDirectory || parent == null) {
|
||||
return false;
|
||||
}
|
||||
if (parent instanceof PsiClass) return false; // anonymous or local class
|
||||
if (parent instanceof PsiModifiableCodeBlock) {
|
||||
if (!((PsiModifiableCodeBlock)parent).shouldChangeModificationCount(element)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
parent = parent.getParent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user