Tests for code transformation: if statement with assignments <-> assignment with if expression

This commit is contained in:
Alexey Sedunov
2013-04-04 15:52:03 +04:00
parent 9f828b8f41
commit af8c3792f6
18 changed files with 428 additions and 0 deletions
@@ -31,6 +31,7 @@ import org.jetbrains.jet.jvm.compiler.*;
import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveDescriptorRendererTest;
import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparingTest;
import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveTest;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.AbstractCodeTransformationTest;
import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTest;
import org.jetbrains.jet.plugin.folding.AbstractKotlinFoldingTest;
import org.jetbrains.jet.plugin.hierarchy.AbstractHierarchyTest;
@@ -259,6 +260,14 @@ public class GenerateTests {
testModel("idea/testData/codeInsight/surroundWith/functionLiteral", "doTestWithFunctionLiteralSurrounder")
);
generateTest(
"idea/tests/",
"CodeTransformationsTestGenerated",
AbstractCodeTransformationTest.class,
testModel("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression", "doTestIfStatementWithAssignmentsToExpression"),
testModel("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement", "doTestAssignmentWithIfExpressionToStatement")
);
generateTest(
"idea/tests/",
"HierarchyTestGenerated",
@@ -0,0 +1,21 @@
fun test(n: Int): String {
var res: String
if (n == 1) {
<caret>res = if (3 > 2) {
println("***")
"one"
} else {
println("***")
"???"
}
} else if (n == 2) {
println("***")
res = "two"
} else {
println("***")
res = "too many"
}
return res
}
@@ -0,0 +1,21 @@
fun test(n: Int): String {
var res: String
if (n == 1) {
<caret>if (3 > 2) {
println("***")
res = "one"
} else {
println("***")
res = "???"
}
} else if (n == 2) {
println("***")
res = "two"
} else {
println("***")
res = "too many"
}
return res
}
@@ -0,0 +1,21 @@
fun test(n: Int): String {
var res: String
<caret>res = if (n == 1) {
if (3 > 2) {
println("***")
"one"
} else {
println("***")
"???"
}
} else if (n == 2) {
println("***")
"two"
} else {
println("***")
"too many"
}
return res
}
@@ -0,0 +1,21 @@
fun test(n: Int): String {
var res: String
<caret>if (n == 1) {
if (3 > 2) {
println("***")
res = "one"
} else {
println("***")
res = "???"
}
} else if (n == 2) {
println("***")
res = "two"
} else {
println("***")
res = "too many"
}
return res
}
@@ -0,0 +1,13 @@
fun test(n: Int): String {
var res: String
<caret>res = if (n == 1) {
println("***")
"one"
} else {
println("***")
"two"
}
return res
}
@@ -0,0 +1,13 @@
fun test(n: Int): String {
var res: String
<caret>if (n == 1) {
println("***")
res = "one"
} else {
println("***")
res = "two"
}
return res
}
@@ -0,0 +1,12 @@
// IS_APPLICABLE: false
fun test(n: Int): String {
var res: String
<caret>if (n == 1) {
"one"
} else {
"two"
}
return res
}
@@ -0,0 +1,21 @@
fun test(n: Int): String {
var res: String
if (n == 1) {
<caret>if (3 > 2) {
println("***")
res = "one"
} else {
println("***")
res = "???"
}
} else if (n == 2) {
println("***")
res = "two"
} else {
println("***")
res = "too many"
}
return res
}
@@ -0,0 +1,21 @@
fun test(n: Int): String {
var res: String
if (n == 1) {
<caret>res = if (3 > 2) {
println("***")
"one"
} else {
println("***")
"???"
}
} else if (n == 2) {
println("***")
res = "two"
} else {
println("***")
res = "too many"
}
return res
}
@@ -0,0 +1,21 @@
fun test(n: Int): String {
var res: String
<caret>if (n == 1) {
if (3 > 2) {
println("***")
res = "one"
} else {
println("***")
res = "???"
}
} else if (n == 2) {
println("***")
res = "two"
} else {
println("***")
res = "too many"
}
return res
}
@@ -0,0 +1,21 @@
fun test(n: Int): String {
var res: String
<caret>res = if (n == 1) {
if (3 > 2) {
println("***")
"one"
} else {
println("***")
"???"
}
} else if (n == 2) {
println("***")
"two"
} else {
println("***")
"too many"
}
return res
}
@@ -0,0 +1,13 @@
fun test(n: Int): String {
var res: String
<caret>if (n == 1) {
println("***")
res = "one"
} else {
println("***")
res = "two"
}
return res
}
@@ -0,0 +1,13 @@
fun test(n: Int): String {
var res: String
<caret>res = if (n == 1) {
println("***")
"one"
} else {
println("***")
"two"
}
return res
}
@@ -0,0 +1,11 @@
// IS_APPLICABLE: false
fun test(n: Int): String {
var res: String = ""
<caret>if (n == 1) {
println("***")
res = "one"
}
return res
}
@@ -0,0 +1,14 @@
// IS_APPLICABLE: false
fun test(n: Int): String {
var res: String
<caret>if (n == 1) {
res = "one"
println("***")
} else {
println("***")
res = "two"
}
return res
}
@@ -0,0 +1,60 @@
/*
* Copyright 2010-2013 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.codeTransformations;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.testFramework.LightCodeInsightTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.InTextDirectivesUtils;
import java.io.File;
public abstract class AbstractCodeTransformationTest extends LightCodeInsightTestCase {
public void doTestIfStatementWithAssignmentsToExpression(@NotNull String path) throws Exception {
doTest(path, new IfStatementWithAssignmentsToExpressionIntention());
}
public void doTestAssignmentWithIfExpressionToStatement(@NotNull String path) throws Exception {
doTest(path, new AssignmentWithIfExpressionToStatementIntention());
}
private void doTest(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
configureByFile(path);
String fileText = FileUtil.loadFile(new File(path));
String isApplicableString = InTextDirectivesUtils.findStringWithPrefix("// IS_APPLICABLE: ", fileText);
boolean isApplicableExpected = isApplicableString == null || isApplicableString.equals("true");
assert isApplicableExpected == intentionAction.isAvailable(getProject(), getEditor(), getFile())
: "isAvailable() for " + intentionAction.getClass() + " should return " + isApplicableExpected;
if (isApplicableExpected) {
invokeAndCheck(path, intentionAction);
}
}
private void invokeAndCheck(@NotNull String path, @NotNull IntentionAction intentionAction) {
intentionAction.invoke(getProject(), getEditor(), getFile());
checkResultByFile(path + ".after");
}
@NotNull
@Override
protected String getTestDataPath() {
return "";
}
}
@@ -0,0 +1,102 @@
/*
* Copyright 2010-2013 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.codeTransformations;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.AbstractCodeTransformationTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@InnerTestClasses({CodeTransformationsTestGenerated.IfStatementWithAssignmentsToExpression.class, CodeTransformationsTestGenerated.AssignmentWithIfExpressionToStatement.class})
public class CodeTransformationsTestGenerated extends AbstractCodeTransformationTest {
@TestMetadata("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression")
public static class IfStatementWithAssignmentsToExpression extends AbstractCodeTransformationTest {
public void testAllFilesPresentInIfStatementWithAssignmentsToExpression() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("innerIfTransformed.kt")
public void testInnerIfTransformed() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt");
}
@TestMetadata("nestedIfs.kt")
public void testNestedIfs() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt");
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt");
}
@TestMetadata("simpleIfWithoutElse.kt")
public void testSimpleIfWithoutElse() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutElse.kt");
}
@TestMetadata("simpleIfWithoutTerminatingAssignment.kt")
public void testSimpleIfWithoutTerminatingAssignment() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutTerminatingAssignment.kt");
}
}
@TestMetadata("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement")
public static class AssignmentWithIfExpressionToStatement extends AbstractCodeTransformationTest {
public void testAllFilesPresentInAssignmentWithIfExpressionToStatement() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("innerIfTransformed.kt")
public void testInnerIfTransformed() throws Exception {
doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt");
}
@TestMetadata("nestedIfs.kt")
public void testNestedIfs() throws Exception {
doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt");
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt");
}
@TestMetadata("simpleIfWithoutAssignment.kt")
public void testSimpleIfWithoutAssignment() throws Exception {
doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIfWithoutAssignment.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("CodeTransformationsTestGenerated");
suite.addTestSuite(IfStatementWithAssignmentsToExpression.class);
suite.addTestSuite(AssignmentWithIfExpressionToStatement.class);
return suite;
}
}