From eab50f590bedc381c3a4dd8fbc58a4a2021f5815 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Sun, 5 May 2019 19:02:35 +0200 Subject: [PATCH] Added a first version of a new inspection that tries to detect bogus uses of return statements in test methods and replaces them by assumeThat() calls. Removed oraclejdk8 from travis-ci. Added custom Jupiter DisplayNameGenerator for user friendly test names in report. Extended documentation. Removed Playground. --- .travis.yml | 1 - README.md | 96 +++- build.gradle | 3 +- .../plugins/cajon/AssertJClassNames.kt | 3 + .../intellij/plugins/cajon/Extensions.kt | 5 +- .../intellij/plugins/cajon/MethodNames.kt | 3 + .../AssumeThatInsteadOfReturnInspection.kt | 146 ++++++ .../JoinAssertThatStatementsInspection.kt | 30 +- .../quickfixes/JoinStatementsQuickFix.kt | 4 +- .../ReplaceIfByAssumeThatQuickFix.kt | 49 ++ src/main/resources/META-INF/plugin.xml | 2 + .../AssumeThatInsteadOfReturn.html | 18 + .../JUnitAssertToAssertJ.html | 4 +- .../intellij/playground/Playground.java | 449 ------------------ .../plugins/cajon/AbstractCajonTest.kt | 11 + ...AssumeThatInsteadOfReturnInspectionTest.kt | 24 + .../AssumeThat/AssumeThatAfter.java | 151 ++++++ .../AssumeThat/AssumeThatBefore.java | 156 ++++++ 18 files changed, 681 insertions(+), 474 deletions(-) create mode 100644 src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssumeThatInsteadOfReturnInspection.kt create mode 100644 src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/ReplaceIfByAssumeThatQuickFix.kt create mode 100644 src/main/resources/inspectionDescriptions/AssumeThatInsteadOfReturn.html delete mode 100644 src/test/java/de/platon42/intellij/playground/Playground.java create mode 100644 src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssumeThatInsteadOfReturnInspectionTest.kt create mode 100644 src/test/resources/inspections/AssumeThat/AssumeThatAfter.java create mode 100644 src/test/resources/inspections/AssumeThat/AssumeThatBefore.java diff --git a/.travis.yml b/.travis.yml index eedc000..ebc3f32 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: java jdk: - - oraclejdk8 - openjdk8 before_script: diff --git a/README.md b/README.md index b72b8fa..1abbe5f 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,9 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the ## Implemented inspections and quickfixes - JoinAssertThatStatements + + Joins multiple ```assertThat()``` statements with same actual expression together. + ``` from: assertThat(expected).someCondition(); assertThat(expected).anotherCondition(); @@ -82,8 +85,11 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the except for method calls with known side-effect methods such as ```Iterator.next()``` -- please notify me about others. The comments of the statements will be preserved. When using ```.extracting()``` or similar, the statements will not be merged. - + - AssertThatObjectIsNullOrNotNull + + Uses ```isNull()``` and ```isNotNull()``` instead. + ``` from: assertThat(object).isEqualTo(null); to: assertThat(object).isNull(); @@ -93,12 +99,18 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the ``` - AssertThatBooleanCondition + + Uses ```isTrue()``` and ```isFalse()``` instead. + ``` from: assertThat(booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE); to: assertThat(booleanValue).isTrue()/isFalse(); ``` - AssertThatInvertedBooleanCondition + + Inverts the boolean condition to make it more readable. + ``` from: assertThat(!booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE); from: assertThat(!booleanValue).isTrue()/isFalse(); @@ -106,6 +118,9 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the ``` - AssertThatInstanceOf + + Moves ```instanceof``` expressions out of ```assertThat()```. + ``` from: assertThat(object instanceof classname).isEqualTo(true); from: assertThat(object instanceof classname).isTrue(); @@ -117,13 +132,21 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the ``` - AssertThatStringIsEmpty + + Uses ```isEmpty()``` for empty string assertions. + ``` from: assertThat(charSequence/string).isEqualTo(""); from: assertThat(charSequence/string).hasSize(0); to: assertThat(charSequence/string).isEmpty(); ``` + The ```assertThat(string.length()).isEqualTo(0);``` case is handled in the AssertThatSize inspection. + - AssertThatStringExpression + + Moves string operations inside assertThat() out. + ``` from: assertThat(stringActual.isEmpty()).isTrue(); to: assertThat(stringActual).isEmpty(); @@ -147,12 +170,19 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the Analogously with ```isFalse()```. - AssertThatEnumerableIsEmpty + + Uses ```isEmpty()``` for ```hasSize(0)``` iterable assertions instead. + ``` from: assertThat(enumerable).hasSize(0); to: assertThat(enumerable).isEmpty(); ``` - AssertThatSize + + Makes assertions on sizes of arrays, collections, strings, + or ```CharSequence```s more concise. + ``` from: assertThat(array.length).isEqualTo(0); from: assertThat(array.length).isLessThanOrEqualTo(0); @@ -201,6 +231,9 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the ``` - AssertThatBinaryExpression + + Splits a boolean condition represented by binary expression out of ```assertThat()```. + ``` from: assertThat(primActual == primExpected).isTrue(); to: assertThat(primActual).isEqualTo(primExpected); @@ -220,6 +253,11 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the ...and many, many more combinations (more than 150). - AssertThatJava8Optional + + Examines the statement for Java 8 Optional type and whether the statement + effectively tries to assert the presence, absence or content and then + replaces the statement by better assertions. + ``` from: assertThat(opt.isPresent()).isEqualTo(true); from: assertThat(opt.isPresent()).isNotEqualTo(false); @@ -249,6 +287,11 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the ``` - AssertThatGuavaOptional + + Examines the statement for Google Guava Optional type and whether the statement + effectively tries to assert the presence, absence or content and then + replaces the statement by better assertions. + ``` from: assertThat(opt.isPresent()).isEqualTo(true); from: assertThat(opt.isPresent()).isNotEqualTo(false); @@ -276,7 +319,54 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the AssertJ for Guava needs to be available in the classpath. +- AssumeThatInsteadOfReturn + + Tries to detect bogus uses of return statements in test methods and replaces them by ```assumeThat()``` calls. + + Novices will use these to skip test execution by bailing out early on some preconditions not met. + However, this suggests that the test has actually been run and passed instead of showing the test + as being skipped. + + Return statements in ```if``` statements in main test methods (must be annotated with JUnit 4 or + Jupiter @Test annotations) will be verified to have at least one ```assertThat()``` statement in the code flow. + Method calls within the same class will be examined for ```assertThat()``` statements, too. + However, at most 50 statements and down to five recursions will be tolerated before giving up. + + Currently, the quickfix may lose some comments during operation. The other branch of the ```if``` statement + will be inlined (blocks with declarations will remain a code block due to variable scope). + + The generated ```assumeThat()``` statement could be optimized further (similar to ```assertThat()```). + + Example: + + ``` + @Test + public void check_fuel_emission() { + if (System.getProperty("manufacturer").equals("Volkswagen")) { + return; + } + double nitroxppm = doWltpDrivingCycle(); + assertThat(nitroxppm).isLessThan(500.0); + } + ``` + will be transformed to + ``` + @Test + public void check_fuel_emission() { + assumeThat(System.getProperty("manufacturer").equals("Volkswagen")).isFalse(); + double nitroxppm = doWltpDrivingCycle(); + assertThat(nitroxppm).isLessThan(500.0); + } + ``` + - JUnitAssertToAssertJ + + Tries to convert most of the JUnit 4 assertions to AssertJ format. + + Does not support Hamcrest-Matchers. + If you need that kind of conversion, you might want to check out the + [Assertions2AssertJ plugin](https://plugins.jetbrains.com/plugin/10345-assertions2assertj) by Ric Emery. + ``` assertTrue(condition); assertTrue(message, condition); @@ -332,7 +422,6 @@ 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). ## Planned features -- AssumeThatInsteadOfReturn - Extraction with property names to lambda with Java 8 ``` from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")... @@ -343,7 +432,7 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro ## Changelog -#### V0.8 (unreleased) +#### V0.8 (05-May-19) - Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions (sorry, released it too hastily). - Fixed ```isEmpty()``` for enumerables and strings and ```isNull()``` for object conversions to be applied only if it is the terminal method call as ```isEmpty()``` and ```isNull()``` return void. - Heavily reworked inspections for edge cases, such as multiple ```isEqualTo()``` calls inside a single statement. @@ -351,6 +440,7 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro - Corrected highlighting for many inspections. - Fixed family names for inspections in batch mode. - Reworded many inspection messages for better understanding. +- Added a first version of a new inspection that tries to detect bogus uses of return statements in test methods and replaces them by ```assumeThat()``` calls. #### V0.7 (28-Apr-19) - Another fix for AssertThatGuavaOptional inspection regarding using the same family name for slightly different quick fix executions diff --git a/build.gradle b/build.gradle index 34c7cde..54f59ec 100644 --- a/build.gradle +++ b/build.gradle @@ -42,7 +42,7 @@ intellij { patchPluginXml { changeNotes """ -

V0.8 (unreleased)

+

V0.8 (05-May-19)

V0.7 (28-Apr-19)