-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#314): first naive implementation of RuleInherianceInTests
- Loading branch information
1 parent
bf917b2
commit 8411be2
Showing
8 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Inheritance in tests | ||
|
||
@todo #314:90min Add documentation for the RuleInheritenceInTests.java. | ||
The documentation should be added to the `docs/rules/inheritance-in-tests.md` file. | ||
The documentation should contain the description of the rule and the | ||
examples of the correct and incorrect code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/github/lombrozo/testnames/rules/RuleInheritenceInTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022-2023 Volodya | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.github.lombrozo.testnames.rules; | ||
|
||
import com.github.lombrozo.testnames.Complaint; | ||
import com.github.lombrozo.testnames.Rule; | ||
import com.github.lombrozo.testnames.TestClass; | ||
import com.github.lombrozo.testnames.complaints.ComplaintLinked; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* The rule that checks a test class doesn't use inheritance. | ||
* @since 1.2.0 | ||
*/ | ||
public final class RuleInheritenceInTests implements Rule { | ||
|
||
/** | ||
* Test class. | ||
*/ | ||
private final TestClass clazz; | ||
|
||
/** | ||
* Constructor. | ||
* @param clazz Test class. | ||
*/ | ||
public RuleInheritenceInTests(final TestClass clazz) { | ||
this.clazz = clazz; | ||
} | ||
|
||
@Override | ||
public Collection<Complaint> complaints() { | ||
final String parent = this.clazz.characteristics().parent(); | ||
if (parent.equals("java.lang.Object")) { | ||
return Collections.emptyList(); | ||
} else { | ||
return Collections.singleton( | ||
new ComplaintLinked( | ||
String.format( | ||
"The test class '%s' has parent class '%s', inheritance in tests is dangerous for maintainability", | ||
this.clazz.name(), | ||
parent | ||
), | ||
String.format( | ||
"Please rename remove all the parents classes from the %s test class", | ||
this.clazz.name() | ||
), | ||
this.getClass(), | ||
"inheritance-in-tests.md" | ||
) | ||
); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/github/lombrozo/testnames/rules/RuleInheritenceInTestsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.github.lombrozo.testnames.rules; | ||
|
||
import com.github.lombrozo.testnames.TestClass; | ||
import com.github.lombrozo.testnames.TestClassCharacteristics; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class RuleInheritenceInTestsTest { | ||
|
||
@Test | ||
void doesNotFoundParents() { | ||
MatcherAssert.assertThat( | ||
"Should not find any complaints if there are no parents", | ||
new RuleInheritenceInTests(new TestClass.Fake()).complaints(), | ||
Matchers.emptyIterable() | ||
); | ||
} | ||
|
||
@Test | ||
void hasSomeParent() { | ||
final String parent = "some.parent.Parent"; | ||
final TestClass.Fake clazz = new TestClass.Fake(new TestClassCharacteristics.Fake(parent)); | ||
MatcherAssert.assertThat( | ||
"Should find complaint if there is a parent", | ||
new RuleInheritenceInTests(clazz).complaints().iterator().next().message(), | ||
Matchers.containsString( | ||
String.format( | ||
"The test class '%s' has parent class '%s', inheritance in tests is dangerous for maintainability", | ||
clazz.name(), | ||
parent | ||
) | ||
) | ||
); | ||
} | ||
} |