From 8b88dff20502a3eb6ad65e1de871e06d8eb20eee Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 27 Feb 2014 14:40:51 -0500 Subject: [PATCH] Add ability to test for expected errors in intentions If an intention test contains `// SHOULD_FAIL_WITH: my_string`, the test is expected to throw `IntentionTestException("my_string")`. This is useful for testing whether or not a function that should be called, is called (a pop-up dialog is displayed, etc.). --- .../AbstractCodeTransformationTest.java | 16 +++++++++++++--- .../intentions/IntentionTestException.kt | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestException.kt diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java index 1b04eea0cdd..9432bf4c1e0 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java @@ -170,9 +170,19 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes "isAvailable() for " + intentionAction.getClass() + " should return " + isApplicableExpected, isApplicableExpected == intentionAction.isAvailable(getProject(), getEditor(), getFile())); - if (isApplicableExpected) { - intentionAction.invoke(getProject(), getEditor(), getFile()); - checkResultByFile(path + ".after"); + String shouldFailString = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// SHOULD_FAIL_WITH: "); + + try { + if (isApplicableExpected) { + intentionAction.invoke(getProject(), getEditor(), getFile()); + // Don't bother checking if it should have failed. + if (shouldFailString == null) { + checkResultByFile(path + ".after"); + } + } + assertNull("Expected test to fail.", shouldFailString); + } catch (IntentionTestException e) { + assertEquals("Failure message mismatch.", shouldFailString, e.getMessage()); } } diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestException.kt b/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestException.kt new file mode 100644 index 00000000000..a9912217f25 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestException.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2010-2014 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.intentions; + +public class IntentionTestException(message: String): RuntimeException(message)