Added new AssertThatFileExpression to move out many common methods from inside the assertThat() expression (exists(), getName(), getParent(), and many more).
This commit is contained in:
parent
acc81863f5
commit
a0ed4eab76
56
README.md
56
README.md
@ -258,6 +258,54 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
|
|||||||
choosing between both fixes (does not work well for batch processing), or ignore this edge case
|
choosing between both fixes (does not work well for batch processing), or ignore this edge case
|
||||||
altogether (just to be sure to not break any code).
|
altogether (just to be sure to not break any code).
|
||||||
|
|
||||||
|
- AssertThatFileExpression
|
||||||
|
|
||||||
|
Moves File method calls inside ```assertThat()``` out.
|
||||||
|
|
||||||
|
```
|
||||||
|
from: assertThat(file.canRead()).isTrue();
|
||||||
|
to: assertThat(file).canRead();
|
||||||
|
|
||||||
|
from: assertThat(file.canWrite()).isTrue();
|
||||||
|
to: assertThat(file).canWrite();
|
||||||
|
|
||||||
|
from: assertThat(file.exists()).isTrue();
|
||||||
|
to: assertThat(file).exists();
|
||||||
|
|
||||||
|
from: assertThat(file.exists()).isFalse();
|
||||||
|
to: assertThat(file).doesNotExist();
|
||||||
|
|
||||||
|
from: assertThat(file.isAbsolute()).isTrue();
|
||||||
|
to: assertThat(file).isAbsolute();
|
||||||
|
|
||||||
|
from: assertThat(file.isAbsolute()).isFalse();
|
||||||
|
to: assertThat(file).isRelative();
|
||||||
|
|
||||||
|
from: assertThat(file.isDirectory()).isTrue();
|
||||||
|
to: assertThat(file).isDirectory();
|
||||||
|
|
||||||
|
from: assertThat(file.isFile()).isTrue();
|
||||||
|
to: assertThat(file).isFile();
|
||||||
|
|
||||||
|
from: assertThat(file.getName()).isEqualTo(filename);
|
||||||
|
to: assertThat(file).hasName(filename);
|
||||||
|
|
||||||
|
from: assertThat(file.getParent()).isEqualTo(pathname);
|
||||||
|
to: assertThat(file).hasParent(pathname);
|
||||||
|
|
||||||
|
from: assertThat(file.getParent()).isNull();
|
||||||
|
from: assertThat(file.getParentFile()).isNull();
|
||||||
|
to: assertThat(file).hasNoParent();
|
||||||
|
|
||||||
|
from: assertThat(file.list()).isEmpty();
|
||||||
|
from: assertThat(file.listFiles()).isEmpty();
|
||||||
|
to: assertThat(file).isEmptyDirectory();
|
||||||
|
|
||||||
|
from: assertThat(file.list()).isNotEmpty();
|
||||||
|
from: assertThat(file.listFiles()).isNotEmpty();
|
||||||
|
to: assertThat(file).isNotEmptyDirectory();
|
||||||
|
```
|
||||||
|
|
||||||
- AssertThatEnumerableIsEmpty
|
- AssertThatEnumerableIsEmpty
|
||||||
|
|
||||||
Uses ```isEmpty()``` for ```hasSize(0)``` iterable assertions instead.
|
Uses ```isEmpty()``` for ```hasSize(0)``` iterable assertions instead.
|
||||||
@ -552,10 +600,10 @@ The IntelliJ framework actually uses the JUnit 3 TestCase for plugin testing and
|
|||||||
Feel free to use the code (in package ```de.platon42.intellij.jupiter```) for your projects (with attribution).
|
Feel free to use the code (in package ```de.platon42.intellij.jupiter```) for your projects (with attribution).
|
||||||
|
|
||||||
## Planned features
|
## Planned features
|
||||||
- More Optional fixes such as opt1.get() == opt2.get() etc.
|
- More Optional fixes such as ```opt1.get() == opt2.get()``` etc.
|
||||||
- More moving out of methods for File, Path, LocalDate/Time etc.
|
- More moving out of methods for Path, LocalDate/Time etc.
|
||||||
- Converting ```foo.compareTo(bar) == 0``` to ```isEqualTo()``` (yes, I've *really* seen code like that)
|
- Converting ```foo.compareTo(bar) == 0``` to ```isEqualTo()``` (yes, I've *really* seen code like that)
|
||||||
- Extraction with property names to lambda with Java 8
|
- Extraction with property names to lambda/method reference with Java 8
|
||||||
|
|
||||||
```
|
```
|
||||||
from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")...
|
from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")...
|
||||||
@ -569,6 +617,8 @@ Feel free to use the code (in package ```de.platon42.intellij.jupiter```) for yo
|
|||||||
for array types. Sigh. Shouldn't be working >12h a day and then do some more stuff at home.
|
for array types. Sigh. Shouldn't be working >12h a day and then do some more stuff at home.
|
||||||
- Fixed a bug in AssertThatBinaryExpression inspection for ```assertThat(null != expression)``` and related
|
- Fixed a bug in AssertThatBinaryExpression inspection for ```assertThat(null != expression)``` and related
|
||||||
that would not correctly invert the condition on transformation.
|
that would not correctly invert the condition on transformation.
|
||||||
|
- Added new AssertThatFileExpression to move out many common methods from inside the
|
||||||
|
```assertThat()``` expression (```exists(), getName(), getParent()```, and many more).
|
||||||
|
|
||||||
#### V1.5 (24-Sep-19)
|
#### V1.5 (24-Sep-19)
|
||||||
- Fix for AssertThatCollectionOrMap inspection sometimes causing an index out of bounds exception.
|
- Fix for AssertThatCollectionOrMap inspection sometimes causing an index out of bounds exception.
|
||||||
|
@ -49,6 +49,8 @@ patchPluginXml {
|
|||||||
for array types. Sigh. Shouldn't be working >12h a day and then do some more stuff at home.
|
for array types. Sigh. Shouldn't be working >12h a day and then do some more stuff at home.
|
||||||
<li>Fixed a bug in AssertThatBinaryExpression inspection for assertThat(null != expression) and related
|
<li>Fixed a bug in AssertThatBinaryExpression inspection for assertThat(null != expression) and related
|
||||||
that would not correctly invert the condition on transformation.
|
that would not correctly invert the condition on transformation.
|
||||||
|
<li>Added new AssertThatFileExpression to move out many common methods from inside the
|
||||||
|
assertThat() expression (exists(), getName(), getParent() and many more).
|
||||||
</ul>
|
</ul>
|
||||||
<p>Full changelog available at <a href="https://github.com/chrisly42/cajon-plugin#changelog">Github project site</a>.</p>
|
<p>Full changelog available at <a href="https://github.com/chrisly42/cajon-plugin#changelog">Github project site</a>.</p>
|
||||||
"""
|
"""
|
||||||
|
@ -44,6 +44,8 @@ class AssertJClassNames {
|
|||||||
@NonNls
|
@NonNls
|
||||||
const val ABSTRACT_MAP_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractMapAssert"
|
const val ABSTRACT_MAP_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractMapAssert"
|
||||||
@NonNls
|
@NonNls
|
||||||
|
const val ABSTRACT_OBJECT_ARRAY_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractObjectArrayAssert"
|
||||||
|
@NonNls
|
||||||
const val ABSTRACT_ITERABLE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractIterableAssert"
|
const val ABSTRACT_ITERABLE_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractIterableAssert"
|
||||||
@NonNls
|
@NonNls
|
||||||
const val ABSTRACT_OPTIONAL_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractOptionalAssert"
|
const val ABSTRACT_OPTIONAL_ASSERT_CLASSNAME = "org.assertj.core.api.AbstractOptionalAssert"
|
||||||
|
@ -0,0 +1,117 @@
|
|||||||
|
package de.platon42.intellij.plugins.cajon.inspections
|
||||||
|
|
||||||
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
|
import com.intellij.psi.*
|
||||||
|
import com.siyeh.ig.callMatcher.CallMatcher
|
||||||
|
import de.platon42.intellij.plugins.cajon.*
|
||||||
|
import de.platon42.intellij.plugins.cajon.quickfixes.MoveOutMethodCallExpressionQuickFix
|
||||||
|
|
||||||
|
|
||||||
|
class AssertThatFileExpressionInspection : AbstractAssertJInspection() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val DISPLAY_NAME = "Asserting a file specific expression"
|
||||||
|
|
||||||
|
private val MAPPINGS = listOf(
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "canRead"),
|
||||||
|
"canRead", expectBoolean = true
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "canWrite"),
|
||||||
|
"canWrite", expectBoolean = true
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "exists"),
|
||||||
|
"exists", "doesNotExist", expectBoolean = true
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "isAbsolute"),
|
||||||
|
"isAbsolute", "isRelative", expectBoolean = true
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "isDirectory"),
|
||||||
|
"isDirectory", expectBoolean = true
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "isFile"),
|
||||||
|
"isFile", expectBoolean = true
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "getName"),
|
||||||
|
"hasName",
|
||||||
|
expectedMatcher = CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING)
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "getParent", "getParentFile"),
|
||||||
|
"hasNoParent", expectNullNonNull = true
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "getParent"),
|
||||||
|
"hasParent",
|
||||||
|
expectedMatcher = CallMatcher.anyOf(IS_EQUAL_TO_OBJECT, IS_EQUAL_TO_STRING)
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "getParentFile"),
|
||||||
|
"hasParent",
|
||||||
|
expectedMatcher = IS_EQUAL_TO_OBJECT
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "list", "listFiles"),
|
||||||
|
"isEmptyDirectory",
|
||||||
|
expectedMatcher = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_OBJECT_ARRAY_ASSERT_CLASSNAME, MethodNames.IS_EMPTY)
|
||||||
|
.parameterCount(0)!!
|
||||||
|
),
|
||||||
|
Mapping(
|
||||||
|
CallMatcher.instanceCall(CommonClassNames.JAVA_IO_FILE, "list", "listFiles"),
|
||||||
|
"isNotEmptyDirectory",
|
||||||
|
expectedMatcher = CallMatcher.instanceCall(AssertJClassNames.ABSTRACT_OBJECT_ARRAY_ASSERT_CLASSNAME, MethodNames.IS_NOT_EMPTY)
|
||||||
|
.parameterCount(0)!!
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getDisplayName() = DISPLAY_NAME
|
||||||
|
|
||||||
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
|
return object : JavaElementVisitor() {
|
||||||
|
override fun visitExpressionStatement(statement: PsiExpressionStatement) {
|
||||||
|
super.visitExpressionStatement(statement)
|
||||||
|
if (!statement.hasAssertThat()) return
|
||||||
|
val staticMethodCall = statement.findStaticMethodCall() ?: return
|
||||||
|
|
||||||
|
val assertThatArgument = staticMethodCall.getArgOrNull(0) as? PsiMethodCallExpression ?: return
|
||||||
|
val expectedCallExpression = statement.findOutmostMethodCall() ?: return
|
||||||
|
|
||||||
|
for (mapping in MAPPINGS.filter { it.callMatcher.test(assertThatArgument) }) {
|
||||||
|
if (mapping.expectBoolean && ASSERT_THAT_BOOLEAN.test(staticMethodCall)) {
|
||||||
|
val expectedBooleanResult = expectedCallExpression.getAllTheSameExpectedBooleanConstants() ?: continue
|
||||||
|
val replacementMethod = if (expectedBooleanResult) mapping.replacementForTrue else mapping.replacementForFalse ?: return
|
||||||
|
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, replacementMethod) { desc, method ->
|
||||||
|
MoveOutMethodCallExpressionQuickFix(desc, method)
|
||||||
|
}
|
||||||
|
} else if (mapping.expectNullNonNull != null) {
|
||||||
|
val expectedNullNonNullResult = expectedCallExpression.getExpectedNullNonNullResult() ?: continue
|
||||||
|
val replacementMethod = if (expectedNullNonNullResult xor mapping.expectNullNonNull) mapping.replacementForTrue else mapping.replacementForFalse ?: continue
|
||||||
|
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, replacementMethod) { desc, method ->
|
||||||
|
MoveOutMethodCallExpressionQuickFix(desc, method, useNullNonNull = true)
|
||||||
|
}
|
||||||
|
} else if (mapping.expectedMatcher?.test(expectedCallExpression) == true) {
|
||||||
|
registerMoveOutMethod(holder, expectedCallExpression, assertThatArgument, mapping.replacementForTrue) { desc, method ->
|
||||||
|
MoveOutMethodCallExpressionQuickFix(desc, method, replaceOnlyThisMethod = mapping.expectedMatcher)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Mapping(
|
||||||
|
val callMatcher: CallMatcher,
|
||||||
|
val replacementForTrue: String,
|
||||||
|
val replacementForFalse: String? = null,
|
||||||
|
val expectBoolean: Boolean = false,
|
||||||
|
val expectNullNonNull: Boolean? = null,
|
||||||
|
val expectedMatcher: CallMatcher? = null
|
||||||
|
)
|
||||||
|
}
|
@ -4,6 +4,7 @@ import com.intellij.codeInspection.ProblemDescriptor
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.JavaPsiFacade
|
import com.intellij.psi.JavaPsiFacade
|
||||||
import com.intellij.psi.PsiMethodCallExpression
|
import com.intellij.psi.PsiMethodCallExpression
|
||||||
|
import com.siyeh.ig.callMatcher.CallMatcher
|
||||||
import de.platon42.intellij.plugins.cajon.*
|
import de.platon42.intellij.plugins.cajon.*
|
||||||
|
|
||||||
class MoveOutMethodCallExpressionQuickFix(
|
class MoveOutMethodCallExpressionQuickFix(
|
||||||
@ -11,7 +12,8 @@ class MoveOutMethodCallExpressionQuickFix(
|
|||||||
private val replacementMethod: String,
|
private val replacementMethod: String,
|
||||||
private val useNullNonNull: Boolean = false,
|
private val useNullNonNull: Boolean = false,
|
||||||
private val noExpectedExpression: Boolean = false,
|
private val noExpectedExpression: Boolean = false,
|
||||||
private val keepExpectedAsSecondArgument: Boolean = false
|
private val keepExpectedAsSecondArgument: Boolean = false,
|
||||||
|
private val replaceOnlyThisMethod: CallMatcher? = null
|
||||||
) :
|
) :
|
||||||
AbstractCommonQuickFix(description) {
|
AbstractCommonQuickFix(description) {
|
||||||
|
|
||||||
@ -29,29 +31,46 @@ class MoveOutMethodCallExpressionQuickFix(
|
|||||||
val assertExpression = assertThatMethodCall.firstArg as? PsiMethodCallExpression ?: return
|
val assertExpression = assertThatMethodCall.firstArg as? PsiMethodCallExpression ?: return
|
||||||
val assertExpressionArg = if (noExpectedExpression) null else assertExpression.getArgOrNull(0)?.copy()
|
val assertExpressionArg = if (noExpectedExpression) null else assertExpression.getArgOrNull(0)?.copy()
|
||||||
|
|
||||||
if (keepExpectedAsSecondArgument) {
|
when {
|
||||||
assertExpressionArg ?: return
|
replaceOnlyThisMethod != null -> {
|
||||||
val secondArg =
|
val methodsToFix = assertThatMethodCall.collectMethodCallsUpToStatement()
|
||||||
if (useNullNonNull) JavaPsiFacade.getElementFactory(project).createExpressionFromText("null", null) else outmostCallExpression.getArgOrNull(0)?.copy() ?: return
|
.filter(replaceOnlyThisMethod::test)
|
||||||
|
.toList()
|
||||||
|
|
||||||
assertExpression.replace(assertExpression.qualifierExpression)
|
assertExpression.replace(assertExpression.qualifierExpression)
|
||||||
|
|
||||||
val expectedExpression = createExpectedMethodCall(outmostCallExpression, replacementMethod, assertExpressionArg, secondArg)
|
methodsToFix
|
||||||
expectedExpression.replaceQualifierFromMethodCall(outmostCallExpression)
|
.forEach {
|
||||||
outmostCallExpression.replace(expectedExpression)
|
val expectedExpression = createExpectedMethodCall(it, replacementMethod, *it.argumentList.expressions)
|
||||||
} else {
|
expectedExpression.replaceQualifierFromMethodCall(it)
|
||||||
val methodsToFix = assertThatMethodCall.collectMethodCallsUpToStatement()
|
it.replace(expectedExpression)
|
||||||
.filter { (if (useNullNonNull) it.getExpectedNullNonNullResult() else it.getExpectedBooleanResult()) != null }
|
}
|
||||||
.toList()
|
}
|
||||||
|
keepExpectedAsSecondArgument -> {
|
||||||
|
assertExpressionArg ?: return
|
||||||
|
val secondArg =
|
||||||
|
if (useNullNonNull) JavaPsiFacade.getElementFactory(project).createExpressionFromText("null", null) else outmostCallExpression.getArgOrNull(0)?.copy() ?: return
|
||||||
|
|
||||||
assertExpression.replace(assertExpression.qualifierExpression)
|
assertExpression.replace(assertExpression.qualifierExpression)
|
||||||
|
|
||||||
methodsToFix
|
val expectedExpression = createExpectedMethodCall(outmostCallExpression, replacementMethod, assertExpressionArg, secondArg)
|
||||||
.forEach {
|
expectedExpression.replaceQualifierFromMethodCall(outmostCallExpression)
|
||||||
val expectedExpression = createExpectedMethodCall(it, replacementMethod, *listOfNotNull(assertExpressionArg).toTypedArray())
|
outmostCallExpression.replace(expectedExpression)
|
||||||
expectedExpression.replaceQualifierFromMethodCall(it)
|
}
|
||||||
it.replace(expectedExpression)
|
else -> {
|
||||||
}
|
val methodsToFix = assertThatMethodCall.collectMethodCallsUpToStatement()
|
||||||
|
.filter { (if (useNullNonNull) it.getExpectedNullNonNullResult() else it.getExpectedBooleanResult()) != null }
|
||||||
|
.toList()
|
||||||
|
|
||||||
|
assertExpression.replace(assertExpression.qualifierExpression)
|
||||||
|
|
||||||
|
methodsToFix
|
||||||
|
.forEach {
|
||||||
|
val expectedExpression = createExpectedMethodCall(it, replacementMethod, *listOfNotNull(assertExpressionArg).toTypedArray())
|
||||||
|
expectedExpression.replaceQualifierFromMethodCall(it)
|
||||||
|
it.replace(expectedExpression)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -45,6 +45,8 @@
|
|||||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringExpressionInspection"/>
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringExpressionInspection"/>
|
||||||
<localInspection groupPath="Java" shortName="AssertThatCollectionOrMapExpression" enabledByDefault="true" level="WARNING"
|
<localInspection groupPath="Java" shortName="AssertThatCollectionOrMapExpression" enabledByDefault="true" level="WARNING"
|
||||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatCollectionOrMapExpressionInspection"/>
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatCollectionOrMapExpressionInspection"/>
|
||||||
|
<localInspection groupPath="Java" shortName="AssertThatFileExpression" enabledByDefault="true" level="WARNING"
|
||||||
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatFileExpressionInspection"/>
|
||||||
|
|
||||||
<localInspection groupPath="Java" shortName="JoinAssertThatStatements" enabledByDefault="true" level="WARNING"
|
<localInspection groupPath="Java" shortName="JoinAssertThatStatements" enabledByDefault="true" level="WARNING"
|
||||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.JoinAssertThatStatementsInspection"/>
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.JoinAssertThatStatementsInspection"/>
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
Operates on assertions on objects of type File. Turns assertThat(file.someMethod(arg)).someAssertion() into assertThat(file).someMethod(arg).
|
||||||
|
<!-- tooltip end -->
|
||||||
|
<br>someMethod() can be canRead(), canWrite(), exists(), isAbsolute(), isDirectory(), isFile(),
|
||||||
|
getName(), getParent(), getParentFile(), list() and listFiles().
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,35 @@
|
|||||||
|
package de.platon42.intellij.plugins.cajon.inspections
|
||||||
|
|
||||||
|
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
|
||||||
|
import de.platon42.intellij.jupiter.MyFixture
|
||||||
|
import de.platon42.intellij.jupiter.TestDataSubPath
|
||||||
|
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
internal class AssertThatFileExpressionInspectionTest : AbstractCajonTest() {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestDataSubPath("inspections/FileExpression")
|
||||||
|
internal fun assertThat_with_certain_File_methods(@MyFixture myFixture: JavaCodeInsightTestFixture) {
|
||||||
|
myFixture.enableInspections(AssertThatFileExpressionInspection::class.java)
|
||||||
|
myFixture.configureByFile("FileExpressionBefore.java")
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove canRead() of actual expression and use assertThat().canRead() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove canWrite() of actual expression and use assertThat().canWrite() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove exists() of actual expression and use assertThat().exists() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove exists() of actual expression and use assertThat().doesNotExist() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isAbsolute() of actual expression and use assertThat().isAbsolute() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isAbsolute() of actual expression and use assertThat().isRelative() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isDirectory() of actual expression and use assertThat().isDirectory() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isFile() of actual expression and use assertThat().isFile() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getName() of actual expression and use assertThat().hasName() instead"), 3)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getParent() of actual expression and use assertThat().hasNoParent() instead"), 2)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getParentFile() of actual expression and use assertThat().hasNoParent() instead"), 2)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getParent() of actual expression and use assertThat().hasParent() instead"), 1)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove getParentFile() of actual expression and use assertThat().hasParent() instead"), 1)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove listFiles() of actual expression and use assertThat().isEmptyDirectory() instead"), 1)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove listFiles() of actual expression and use assertThat().isNotEmptyDirectory() instead"), 1)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove list() of actual expression and use assertThat().isEmptyDirectory() instead"), 1)
|
||||||
|
executeQuickFixes(myFixture, Regex.fromLiteral("Remove list() of actual expression and use assertThat().isNotEmptyDirectory() instead"), 1)
|
||||||
|
myFixture.checkResultByFile("FileExpressionAfter.java")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.fail;
|
||||||
|
|
||||||
|
public class FileExpression {
|
||||||
|
|
||||||
|
private void fileExpression() {
|
||||||
|
File file = new File("foo");
|
||||||
|
|
||||||
|
assertThat(file).as("foo").canRead();
|
||||||
|
assertThat(file).canRead();
|
||||||
|
assertThat(file).canRead();
|
||||||
|
assertThat(file.canRead()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.canRead()).isNotEqualTo(true);
|
||||||
|
assertThat(file.canRead()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file).as("foo").canWrite();
|
||||||
|
assertThat(file).canWrite();
|
||||||
|
assertThat(file).canWrite();
|
||||||
|
assertThat(file.canWrite()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.canWrite()).isNotEqualTo(true);
|
||||||
|
assertThat(file.canWrite()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file).as("foo").exists();
|
||||||
|
assertThat(file).exists();
|
||||||
|
assertThat(file).exists();
|
||||||
|
assertThat(file).as("foo").doesNotExist();
|
||||||
|
assertThat(file).doesNotExist();
|
||||||
|
assertThat(file).doesNotExist();
|
||||||
|
|
||||||
|
assertThat(file).as("foo").isAbsolute();
|
||||||
|
assertThat(file).isAbsolute();
|
||||||
|
assertThat(file).isAbsolute();
|
||||||
|
assertThat(file).as("foo").isRelative();
|
||||||
|
assertThat(file).isRelative();
|
||||||
|
assertThat(file).isRelative();
|
||||||
|
|
||||||
|
assertThat(file).as("foo").isDirectory();
|
||||||
|
assertThat(file).isDirectory();
|
||||||
|
assertThat(file).isDirectory();
|
||||||
|
assertThat(file.isDirectory()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.isDirectory()).isNotEqualTo(true);
|
||||||
|
assertThat(file.isDirectory()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file).as("foo").isFile();
|
||||||
|
assertThat(file).isFile();
|
||||||
|
assertThat(file).isFile();
|
||||||
|
assertThat(file.isFile()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.isFile()).isNotEqualTo(true);
|
||||||
|
assertThat(file.isFile()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file).hasName("foo");
|
||||||
|
assertThat(file.getName()).isNotEqualTo("foo");
|
||||||
|
assertThat(file).hasName(null);
|
||||||
|
assertThat(file.getName()).isNull();
|
||||||
|
assertThat(file.getName()).isNotEqualTo(null);
|
||||||
|
assertThat(file.getName()).isNotNull();
|
||||||
|
assertThat(file.getName()).isEmpty();
|
||||||
|
assertThat(file.getName()).isNotEmpty();
|
||||||
|
|
||||||
|
assertThat(file).hasParent("foo");
|
||||||
|
assertThat(file.getParent()).isNotEqualTo("foo");
|
||||||
|
assertThat(file).hasNoParent();
|
||||||
|
assertThat(file).hasNoParent();
|
||||||
|
assertThat(file.getParent()).isNotEqualTo(null);
|
||||||
|
assertThat(file.getParent()).isNotNull();
|
||||||
|
assertThat(file.getParent()).isEmpty();
|
||||||
|
assertThat(file.getParent()).isNotEmpty();
|
||||||
|
|
||||||
|
assertThat(file).hasParent(new File("foo"));
|
||||||
|
assertThat(file.getParentFile()).isNotEqualTo(new File("foo"));
|
||||||
|
assertThat(file).hasNoParent();
|
||||||
|
assertThat(file).hasNoParent();
|
||||||
|
assertThat(file.getParentFile()).isNotEqualTo(null);
|
||||||
|
assertThat(file.getParentFile()).isNotNull();
|
||||||
|
|
||||||
|
assertThat(file.listFiles()).isNull();
|
||||||
|
assertThat(file.listFiles()).isNullOrEmpty();
|
||||||
|
assertThat(file).isEmptyDirectory();
|
||||||
|
assertThat(file).isNotEmptyDirectory();
|
||||||
|
|
||||||
|
assertThat(file.list()).isNull();
|
||||||
|
assertThat(file.list()).isNullOrEmpty();
|
||||||
|
assertThat(file).isEmptyDirectory();
|
||||||
|
assertThat(file).isNotEmptyDirectory();
|
||||||
|
|
||||||
|
assertThat(file.getName()).endsWith(".foo"); // could be turned into .hasExtension("foo"), but not always.
|
||||||
|
|
||||||
|
assertThat(file).as("foo").hasName("foo").as("bar").hasName("bar");
|
||||||
|
|
||||||
|
org.junit.Assert.assertThat(file, null);
|
||||||
|
fail("oh no!");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.fail;
|
||||||
|
|
||||||
|
public class FileExpression {
|
||||||
|
|
||||||
|
private void fileExpression() {
|
||||||
|
File file = new File("foo");
|
||||||
|
|
||||||
|
assertThat(file.canRead()).as("foo").isEqualTo(true);
|
||||||
|
assertThat(file.canRead()).isNotEqualTo(false);
|
||||||
|
assertThat(file.canRead()).isTrue();
|
||||||
|
assertThat(file.canRead()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.canRead()).isNotEqualTo(true);
|
||||||
|
assertThat(file.canRead()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file.canWrite()).as("foo").isEqualTo(true);
|
||||||
|
assertThat(file.canWrite()).isNotEqualTo(false);
|
||||||
|
assertThat(file.canWrite()).isTrue();
|
||||||
|
assertThat(file.canWrite()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.canWrite()).isNotEqualTo(true);
|
||||||
|
assertThat(file.canWrite()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file.exists()).as("foo").isEqualTo(true);
|
||||||
|
assertThat(file.exists()).isNotEqualTo(false);
|
||||||
|
assertThat(file.exists()).isTrue();
|
||||||
|
assertThat(file.exists()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.exists()).isNotEqualTo(true);
|
||||||
|
assertThat(file.exists()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file.isAbsolute()).as("foo").isEqualTo(true);
|
||||||
|
assertThat(file.isAbsolute()).isNotEqualTo(false);
|
||||||
|
assertThat(file.isAbsolute()).isTrue();
|
||||||
|
assertThat(file.isAbsolute()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.isAbsolute()).isNotEqualTo(true);
|
||||||
|
assertThat(file.isAbsolute()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file.isDirectory()).as("foo").isEqualTo(true);
|
||||||
|
assertThat(file.isDirectory()).isNotEqualTo(false);
|
||||||
|
assertThat(file.isDirectory()).isTrue();
|
||||||
|
assertThat(file.isDirectory()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.isDirectory()).isNotEqualTo(true);
|
||||||
|
assertThat(file.isDirectory()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file.isFile()).as("foo").isEqualTo(true);
|
||||||
|
assertThat(file.isFile()).isNotEqualTo(false);
|
||||||
|
assertThat(file.isFile()).isTrue();
|
||||||
|
assertThat(file.isFile()).as("foo").isEqualTo(false);
|
||||||
|
assertThat(file.isFile()).isNotEqualTo(true);
|
||||||
|
assertThat(file.isFile()).isFalse();
|
||||||
|
|
||||||
|
assertThat(file.getName()).isEqualTo("foo");
|
||||||
|
assertThat(file.getName()).isNotEqualTo("foo");
|
||||||
|
assertThat(file.getName()).isEqualTo(null);
|
||||||
|
assertThat(file.getName()).isNull();
|
||||||
|
assertThat(file.getName()).isNotEqualTo(null);
|
||||||
|
assertThat(file.getName()).isNotNull();
|
||||||
|
assertThat(file.getName()).isEmpty();
|
||||||
|
assertThat(file.getName()).isNotEmpty();
|
||||||
|
|
||||||
|
assertThat(file.getParent()).isEqualTo("foo");
|
||||||
|
assertThat(file.getParent()).isNotEqualTo("foo");
|
||||||
|
assertThat(file.getParent()).isEqualTo(null);
|
||||||
|
assertThat(file.getParent()).isNull();
|
||||||
|
assertThat(file.getParent()).isNotEqualTo(null);
|
||||||
|
assertThat(file.getParent()).isNotNull();
|
||||||
|
assertThat(file.getParent()).isEmpty();
|
||||||
|
assertThat(file.getParent()).isNotEmpty();
|
||||||
|
|
||||||
|
assertThat(file.getParentFile()).isEqualTo(new File("foo"));
|
||||||
|
assertThat(file.getParentFile()).isNotEqualTo(new File("foo"));
|
||||||
|
assertThat(file.getParentFile()).isEqualTo(null);
|
||||||
|
assertThat(file.getParentFile()).isNull();
|
||||||
|
assertThat(file.getParentFile()).isNotEqualTo(null);
|
||||||
|
assertThat(file.getParentFile()).isNotNull();
|
||||||
|
|
||||||
|
assertThat(file.listFiles()).isNull();
|
||||||
|
assertThat(file.listFiles()).isNullOrEmpty();
|
||||||
|
assertThat(file.listFiles()).isEmpty();
|
||||||
|
assertThat(file.listFiles()).isNotEmpty();
|
||||||
|
|
||||||
|
assertThat(file.list()).isNull();
|
||||||
|
assertThat(file.list()).isNullOrEmpty();
|
||||||
|
assertThat(file.list()).isEmpty();
|
||||||
|
assertThat(file.list()).isNotEmpty();
|
||||||
|
|
||||||
|
assertThat(file.getName()).endsWith(".foo"); // could be turned into .hasExtension("foo"), but not always.
|
||||||
|
|
||||||
|
assertThat(file.getName()).as("foo").isEqualTo("foo").as("bar").isEqualTo("bar");
|
||||||
|
|
||||||
|
org.junit.Assert.assertThat(file, null);
|
||||||
|
fail("oh no!");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user