rspec/rules/S5845/java/rule.adoc
Fred Tingaud 51369b610e
Make sure that includes are always surrounded by empty lines (#2270)
When an include is not surrounded by empty lines, its content is inlined
on the same line as the adjacent content. That can lead to broken tags
and other display issues.
This PR fixes all such includes and introduces a validation step that
forbids introducing the same problem again.
2023-06-22 10:38:01 +02:00

84 lines
2.5 KiB
Plaintext

== Why is this an issue?
Assertions comparing incompatible types always fail, and negative assertions always pass. At best, negative assertions are useless. At worst, the developer loses time trying to fix his code logic before noticing wrong assertions.
Dissimilar types are:
* comparing a primitive with null
* comparing an object with an unrelated primitive (E.G. a string with an int)
* comparing unrelated classes
* comparing an array to a non-array
* comparing two arrays of dissimilar types
This rule also raises issues for unrelated ``++class++`` and ``++interface++`` or unrelated ``++interface++`` types in negative assertions. Because except in some corner cases, those types are more likely to be dissimilar. And inside a negative assertion, there is no test failure to inform the developer about this unusual comparison.
Supported test frameworks:
* JUnit4
* JUnit5
* AssertJ
=== Noncompliant code example
[source,java]
----
interface KitchenTool {}
interface Plant {}
class Spatula implements KitchenTool {}
class Tree implements Plant {}
void assertValues(int size,
Spatula spatula, KitchenTool tool, KitchenTool[] tools,
Tree tree, Plant plant, Tree[] trees) {
// Whatever the given values, those negative assertions will always pass due to dissimilar types:
assertThat(size).isNotNull(); // Noncompliant; primitives can not be null
assertThat(spatula).isNotEqualTo(tree); // Noncompliant; unrelated classes
assertThat(tool).isNotSameAs(tools); // Noncompliant; array & non-array
assertThat(trees).isNotEqualTo(tools); // Noncompliant; incompatible arrays
// Those assertions will always fail
assertThat(size).isNull(); // Noncompliant
assertThat(spatula).isEqualTo(tree); // Noncompliant
// Those negative assertions are more likely to always pass
assertThat(spatula).isNotEqualTo(plant); // Noncompliant; unrelated class and interface
assertThat(tool).isNotEqualTo(plant); // Noncompliant; unrelated interfaces
}
----
== Resources
* S2159 - Silly equality checks should not be made
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Change the assertion arguments to not compare dissimilar types
=== Highlighting
Primary: The assertion actual argument.
Secondary: if possible, the assertion expected argument.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]