From 66508ceb2c66e8d1059c84b77b285cb700521201 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Mon, 22 Apr 2019 18:52:09 +0200 Subject: [PATCH] New AssertThatInstanceOf inspection that moves instanceof expressions out of assertThat(). Fixes to documentation. --- README.md | 34 ++++++++---- build.gradle | 3 +- .../intellij/plugins/cajon/MethodNames.kt | 4 ++ .../AssertThatBinaryExpressionInspection.kt | 6 +-- .../AssertThatInstanceOfInspection.kt | 54 +++++++++++++++++++ ...tThatInvertedBooleanConditionInspection.kt | 3 +- .../RemoveInstanceOfExpressionQuickFix.kt | 36 +++++++++++++ src/main/resources/META-INF/plugin.xml | 19 ++++--- .../AssertThatInstanceOf.html | 7 +++ .../intellij/playground/Playground.java | 7 +++ .../AssertThatInstanceOfInspectionTest.kt | 22 ++++++++ .../InstanceOf/InstanceOfAfter.java | 22 ++++++++ .../InstanceOf/InstanceOfBefore.java | 22 ++++++++ 13 files changed, 215 insertions(+), 24 deletions(-) create mode 100644 src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInstanceOfInspection.kt create mode 100644 src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/RemoveInstanceOfExpressionQuickFix.kt create mode 100644 src/main/resources/inspectionDescriptions/AssertThatInstanceOf.html create mode 100644 src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatInstanceOfInspectionTest.kt create mode 100644 src/test/resources/inspections/InstanceOf/InstanceOfAfter.java create mode 100644 src/test/resources/inspections/InstanceOf/InstanceOfBefore.java diff --git a/README.md b/README.md index 02ed35c..e264293 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,17 @@ # Cajon - Concise AssertJ Optimizing Nitpicker -Cajon is an IntelliJ IDEA Plugin for shortening and optimizing AssertJ assertions. +Cajon is an IntelliJ IDEA Plugin for shortening and optimizing [AssertJ](https://assertj.github.io/doc/) assertions. -## Why? +## Purpose First, code is easier to read, when it is concise and reflects the intention clearly. AssertJ has plenty of different convenience methods that describing various intentions precisely. Why write longer, more complex code that can be expressed in brevity? -Second, AssertJ is able to output more meaningful descriptions when an assertion fails. +Second, when using the available special assertion methods of AssertJ, a failure of a condition +can be expressed in better detail and with more meaningful descriptions. This makes finding bugs and fixing failed tests more efficient. +Nobody likes to read failures of the kind "failed because true is not false". For example: @@ -50,9 +52,10 @@ The plugin will report inspections in your opened editor file as warnings. You can then quick-fix these with your quick-fix hotkey (usually Alt-Return or Opt-Return). Or, you can use the "Run Inspection by Name..." action to run one inspection on a bigger scope (e.g. the whole project). -Applying a quick fix might result in further optimization possibilities, so you might need to perform a couple of fixes before you get to the final result. +Applying a quick fix might result in further optimization possibilities, so +you might need to perform a couple of fixes before you get to the final result. -For example: +Check out this example where every line represents the result after a Cajon quickfix: ``` assertFalse(!(array.length == collection.size())); @@ -88,7 +91,18 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the ``` from: assertThat(!booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE); from: assertThat(!booleanValue).isTrue()/isFalse(); - to: assertThat(!booleanValue).isFalse()/isTrue(); + to: assertThat(booleanValue).isFalse()/isTrue(); + ``` + +- AssertThatInstanceOf + ``` + from: assertThat(object instanceof classname).isEqualTo(true); + from: assertThat(object instanceof classname).isTrue(); + to: assertThat(object).isInstanceOf(classname.class); + + from: assertThat(object instanceof classname).isEqualTo(false); + from: assertThat(object instanceof classname).isFalse(); + to: assertThat(object).isNotInstanceOf(classname.class); ``` - AssertThatStringIsEmpty @@ -297,11 +311,10 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the Cajon is written in Kotlin 1.3. Cajon is probably the only plugin that uses JUnit 5 Jupiter for unit testing so far (or at least the only one that I'm aware of ;) ). -The IntelliJ framework actually uses the JUnit 3 TestCase for plugin testing and I took me quite a while to make it work with JUnit 5. +The IntelliJ framework actually uses the JUnit 3 TestCase for plugin testing and it took me quite a while to make it work with JUnit 5. Feel free to use the code (in package de.platon42.intellij.jupiter) for your projects (with attribution). ## TODO -- AssertThatInstanceOf - AssumeThatInsteadOfReturn - Join consecutive assertThats - Extraction with property names to lambda with Java 8 @@ -314,14 +327,15 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro ## Changelog -#### V0.6 (unreleased) +#### V0.6 (22-Apr-19) - New AssertThatStringExpression inspection that will move ```isEmpty()```, ```equals()```, ```equalsIgnoreCase()```, ```contains()```, ```startsWith()```, and ```endsWith()``` out of actual expression. - Extended AssertThatSize inspection to take ```String```s and ```CharSequences``` into account, too. - New AssertThatInvertedBooleanCondition inspection that will remove inverted boolean expressions inside ```assertThat()```. - Renamed a few inspections to better/shorter names. +- New AssertThatInstanceOf inspection that moves instanceof expressions out of ```assertThat()```. -#### V0.5 (13-Apr-19) +#### V0.5 (18-Apr-19) - Fixed incompatibility with IDEA versions < 2018.2 (affected AssertThatSizeInspection). Minimal version is now 2017.3. - Fixed missing Guava imports (if not already present) for AssertThatGuavaInspection. This was a major PITA to get right. - Added support for referencing and refactoring inside ```.extracting()``` methods with fields, properties and methods (though diff --git a/build.gradle b/build.gradle index 10f2b69..bae5092 100644 --- a/build.gradle +++ b/build.gradle @@ -40,13 +40,14 @@ intellij { patchPluginXml { changeNotes """ -

V0.6 (xx-Apr-19)

+

V0.6 (22-Apr-19)

V0.5 (18-Apr-19)