Skip to content

Commit

Permalink
refactor: Operator wrapping on end of line (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne committed Sep 21, 2024
1 parent 4367149 commit d9711b6
Show file tree
Hide file tree
Showing 24 changed files with 97 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ private static class AssertExceptionTypeVisitor extends JavaIsoVisitor<Execution
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (ASSERT_THROWS_MATCHER.matches(mi)
&& mi.getArguments().size() == 2
&& getCursor().getParentTreeCursor().getValue() instanceof J.Block) {
if (ASSERT_THROWS_MATCHER.matches(mi) &&
mi.getArguments().size() == 2 &&
getCursor().getParentTreeCursor().getValue() instanceof J.Block) {
J executable = mi.getArguments().get(1);
if (executable instanceof J.Lambda) {
executable = ((J.Lambda) executable).withType(THROWING_CALLABLE_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ private String getStringTemplateAndAppendArguments(J.MethodInvocation assertThat
arguments.add(extractEitherArgument(assertThatArgumentIsEmpty, assertThatArgument, methodToReplaceArgument));

// Special case for Path.of() assertions
if ("java.nio.file.Path".equals(requiredType) && dedicatedAssertion.contains("Raw")
&& TypeUtils.isAssignableTo("java.lang.String", assertThatArgument.getType())) {
if ("java.nio.file.Path".equals(requiredType) && dedicatedAssertion.contains("Raw") &&
TypeUtils.isAssignableTo("java.lang.String", assertThatArgument.getType())) {
maybeAddImport("java.nio.file.Path");
return "assertThat(#{any()}).%s(Path.of(#{any()}))";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (ASSERT_EQUALS.matches(mi) && isBooleanLiteral(mi) &&
JavaType.Primitive.Boolean.equals(mi.getArguments().get(1).getType())) {
StringBuilder sb = new StringBuilder();
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource())
? "assertTrue" : "assertFalse";
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource()) ?
"assertTrue" : "assertFalse";
Expression assertion = mi.getArguments().get(1);
if (mi.getSelect() == null) {
maybeRemoveImport("org.junit.jupiter.api.Assertions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ private boolean isEquals(Expression expr) {

J.MethodInvocation methodInvocation = (J.MethodInvocation) expr;

return "equals".equals(methodInvocation.getName().getSimpleName())
&& methodInvocation.getArguments().size() == 1;
return "equals".equals(methodInvocation.getName().getSimpleName()) &&
methodInvocation.getArguments().size() == 1;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (ASSERT_NOT_EQUALS.matches(mi) && isBooleanLiteral(mi)) {
StringBuilder sb = new StringBuilder();
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource())
? "assertFalse" : "assertTrue";
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource()) ?
"assertFalse" : "assertTrue";
Expression assertion = mi.getArguments().get(1);
if (mi.getSelect() == null) {
maybeRemoveImport("org.junit.jupiter.api.Assertions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ private boolean isEqualBinary(J.MethodInvocation method) {
// Prevent breaking identity comparison.
// Objects that are compared with == should not be compared with `.equals()` instead.
// Out of the primitives == is not allowed when both are of type String
return binary.getLeft().getType() instanceof JavaType.Primitive
&& binary.getRight().getType() instanceof JavaType.Primitive
&& !(binary.getLeft().getType() == JavaType.Primitive.String
&& binary.getRight().getType() == JavaType.Primitive.String);
return binary.getLeft().getType() instanceof JavaType.Primitive &&
binary.getRight().getType() instanceof JavaType.Primitive &&
!(binary.getLeft().getType() == JavaType.Primitive.String &&
binary.getRight().getType() == JavaType.Primitive.String);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ private boolean isEquals(Expression expr) {

J.MethodInvocation methodInvocation = (J.MethodInvocation) expr;

return "equals".equals(methodInvocation.getName().getSimpleName())
&& methodInvocation.getArguments().size() == 1;
return "equals".equals(methodInvocation.getName().getSimpleName()) &&
methodInvocation.getArguments().size() == 1;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,26 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
// Quickly reject invalid methods
String simpleName = method.getSimpleName();
int nameLength = simpleName.length();
if (nameLength < 5
|| !simpleName.startsWith("test")
|| !(simpleName.charAt(4) == '_' || Character.isUpperCase(simpleName.charAt(4)))
|| TypeUtils.isOverride(method.getMethodType())
|| !hasJUnit5MethodAnnotation(method)) {
if (nameLength < 5 ||
!simpleName.startsWith("test") ||
!(simpleName.charAt(4) == '_' || Character.isUpperCase(simpleName.charAt(4))) ||
TypeUtils.isOverride(method.getMethodType()) ||
!hasJUnit5MethodAnnotation(method)) {
return m;
}

// Reject invalid start character
boolean snakecase = simpleName.charAt(4) == '_'
&& 5 < nameLength
&& Character.isAlphabetic(simpleName.charAt(5));
boolean snakecase = simpleName.charAt(4) == '_' &&
5 < nameLength &&
Character.isAlphabetic(simpleName.charAt(5));
if (!snakecase && !Character.isAlphabetic(simpleName.charAt(4))) {
return m;
}

// Avoid reserved keywords
String newMethodName = snakecase
? NameCaseConvention.format(NameCaseConvention.LOWER_UNDERSCORE, simpleName.substring(5))
: NameCaseConvention.format(NameCaseConvention.LOWER_CAMEL, simpleName.substring(4));
String newMethodName = snakecase ?
NameCaseConvention.format(NameCaseConvention.LOWER_UNDERSCORE, simpleName.substring(5)) :
NameCaseConvention.format(NameCaseConvention.LOWER_CAMEL, simpleName.substring(4));
if (RESERVED_KEYWORDS.contains(newMethodName)) {
return m;
}
Expand Down Expand Up @@ -149,11 +149,11 @@ private boolean methodExists(JavaType.Method method, String newName) {

private static boolean hasJUnit5MethodAnnotation(MethodDeclaration method) {
for (J.Annotation a : method.getLeadingAnnotations()) {
if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory")) {
if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory")) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);

// reject invalid methods
if (TypeUtils.isOverride(m.getMethodType())
|| !hasJUnit5MethodAnnotation(method)
|| throwsNothingOrException(method)) {
if (TypeUtils.isOverride(m.getMethodType()) ||
!hasJUnit5MethodAnnotation(method) ||
throwsNothingOrException(method)) {
return m;
}

Expand Down Expand Up @@ -101,11 +101,11 @@ private boolean throwsNothingOrException(J.MethodDeclaration method) {

private boolean hasJUnit5MethodAnnotation(J.MethodDeclaration method) {
for (J.Annotation a : method.getLeadingAnnotations()) {
if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory")) {
if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestTemplate") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory")) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ private static final class TestsNotPublicVisitor extends JavaIsoVisitor<Executio
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, ctx);

if (c.getKind() != J.ClassDeclaration.Kind.Type.Interface
&& c.getModifiers().stream().anyMatch(mod -> mod.getType() == J.Modifier.Type.Public)
&& c.getModifiers().stream().noneMatch(mod -> mod.getType() == J.Modifier.Type.Abstract)
&& !acc.extendedClasses.contains(String.valueOf(c.getType()))) {
if (c.getKind() != J.ClassDeclaration.Kind.Type.Interface &&
c.getModifiers().stream().anyMatch(mod -> mod.getType() == J.Modifier.Type.Public) &&
c.getModifiers().stream().noneMatch(mod -> mod.getType() == J.Modifier.Type.Abstract) &&
!acc.extendedClasses.contains(String.valueOf(c.getType()))) {
boolean hasTestMethods = c.getBody().getStatements().stream()
.filter(org.openrewrite.java.tree.J.MethodDeclaration.class::isInstance)
.map(J.MethodDeclaration.class::cast)
Expand Down Expand Up @@ -168,14 +168,14 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex

private boolean hasJUnit5MethodAnnotation(J.MethodDeclaration method) {
for (J.Annotation a : method.getLeadingAnnotations()) {
if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.AfterEach")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.BeforeEach")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.AfterAll")
|| TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.BeforeAll")) {
if (TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.Test") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.RepeatedTest") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.params.ParameterizedTest") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.TestFactory") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.AfterEach") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.BeforeEach") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.AfterAll") ||
TypeUtils.isOfClassType(a.getType(), "org.junit.jupiter.api.BeforeAll")) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ private Expression rewriteFullyQualifiedToArgumentMatcher(Expression methodArgum

// update the Class type parameter and method return type
Expression classArgument = (Expression) templateParams.get(0);
if (classArgument.getType() == null
|| invocationArgument.getMethodType() == null
|| invocationArgument.getMethodType().getParameterTypes().size() != 1
|| !(invocationArgument.getMethodType().getParameterTypes().get(0) instanceof JavaType.Parameterized)) {
if (classArgument.getType() == null ||
invocationArgument.getMethodType() == null ||
invocationArgument.getMethodType().getParameterTypes().size() != 1 ||
!(invocationArgument.getMethodType().getParameterTypes().get(0) instanceof JavaType.Parameterized)) {
return invocationArgument;
}
JavaType.Parameterized newParameterType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ private static boolean isNotMockIdentifier(J.Identifier identifier, Set<String>
if (spies.contains(identifier.getSimpleName())) {
return false;
}
if (identifier.getType() instanceof JavaType.Method
&& TypeUtils.isAssignableTo("mockit.Invocations",
if (identifier.getType() instanceof JavaType.Method &&
TypeUtils.isAssignableTo("mockit.Invocations",
((JavaType.Method) identifier.getType()).getDeclaringType())) {
return false;
}
Expand All @@ -149,9 +149,9 @@ private static boolean isNotMockIdentifier(J.Identifier identifier, Set<String>
return true;
}
for (JavaType.FullyQualified annotationType : fieldType.getAnnotations()) {
if (TypeUtils.isAssignableTo("mockit.Mocked", annotationType)
|| TypeUtils.isAssignableTo("mockit.Injectable", annotationType)
|| TypeUtils.isAssignableTo("mockit.Tested", annotationType)) {
if (TypeUtils.isAssignableTo("mockit.Mocked", annotationType) ||
TypeUtils.isAssignableTo("mockit.Injectable", annotationType) ||
TypeUtils.isAssignableTo("mockit.Tested", annotationType)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.staticImports("org.junit.jupiter.api.Assertions.assertInstanceOf")
.build();

J.MethodInvocation methodd = reason != null
? template.apply(getCursor(), mi.getCoordinates().replace(), clazz.toString(), expression, reason)
: template.apply(getCursor(), mi.getCoordinates().replace(), clazz.toString(), expression);
J.MethodInvocation methodd = reason != null ?
template.apply(getCursor(), mi.getCoordinates().replace(), clazz.toString(), expression, reason) :
template.apply(getCursor(), mi.getCoordinates().replace(), clazz.toString(), expression);
maybeAddImport("org.junit.jupiter.api.Assertions", "assertInstanceOf");
return methodd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ private JavaTemplate getNestedJavaTemplate(ExecutionContext ctx) {
}

private boolean hasTestMethods(final J.ClassDeclaration cd) {
return !FindAnnotations.find(cd, "@" + TEST_JUNIT4).isEmpty()
|| !FindAnnotations.find(cd, "@" + TEST_JUNIT_JUPITER).isEmpty();
return !FindAnnotations.find(cd, "@" + TEST_JUNIT4).isEmpty() ||
!FindAnnotations.find(cd, "@" + TEST_JUNIT_JUPITER).isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
}
}

String exceptionDeclParam = ((isExpectArgAMatcher || isExpectMessageArgAMatcher || isExpectedCauseArgAMatcher)
|| expectMessageMethodInvocation != null) ?
String exceptionDeclParam = ((isExpectArgAMatcher || isExpectMessageArgAMatcher || isExpectedCauseArgAMatcher) ||
expectMessageMethodInvocation != null) ?
"Throwable exception = " : "";

Object expectedExceptionParam = (expectMethodInvocation == null || isExpectArgAMatcher) ?
Expand Down
Loading

0 comments on commit d9711b6

Please sign in to comment.