
org.openrewrite.java.NoWhitespaceAfterTest.kt Maven / Gradle / Ivy
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.openrewrite.ExecutionContext
import org.openrewrite.InMemoryExecutionContext
import org.openrewrite.Recipe
import org.openrewrite.Tree
import org.openrewrite.java.cleanup.NoWhitespaceAfter
import org.openrewrite.java.cleanup.NoWhitespaceAfterStyle
import org.openrewrite.java.format.AutoFormatVisitor
import org.openrewrite.java.style.Checkstyle
import org.openrewrite.style.NamedStyles
@Suppress(
"CStyleArrayDeclaration",
"StringConcatenationMissingWhitespace",
"ConstantConditions",
"UnusedAssignment",
"UnaryPlus",
"ReturnOfThis",
"SimplifiableAnnotation"
)
interface NoWhitespaceAfterTest : JavaRecipeTest {
override val recipe: Recipe?
get() = NoWhitespaceAfter()
fun noWhitespaceAfterStyle(with: NoWhitespaceAfterStyle.() -> NoWhitespaceAfterStyle = { this }) =
listOf(
NamedStyles(
Tree.randomId(), "test", "test", "test", emptySet(), listOf(
Checkstyle.noWhitespaceAfterStyle().run { with(this) }
)
)
)
@Test
fun arrayAccess(jp: JavaParser.Builder<*, *>) = assertChanged(
parser = jp.styles(noWhitespaceAfterStyle()).build(),
before = """
class Test {
static void method(int[] n) {
int m = n [0];
}
}
""",
after = """
class Test {
static void method(int[] n) {
int m = n[0];
}
}
""",
afterConditions = { cu ->
val nucu = AutoFormatVisitor().visit(cu, InMemoryExecutionContext {})
assertThat(nucu).isEqualTo(cu)
}
)
@Test
fun variableDeclaration(jp: JavaParser.Builder<*, *>) = assertChanged(
parser = jp.styles(noWhitespaceAfterStyle()).build(),
before = """
class Test {
static void method() {
int [] [] a;
int [] b;
int c, d = 0;
}
}
""",
after = """
class Test {
static void method() {
int[][] a;
int[] b;
int c, d = 0;
}
}
""",
afterConditions = { cu ->
val nucu = AutoFormatVisitor().visit(cu, InMemoryExecutionContext {})
assertThat(nucu).isEqualTo(cu)
}
)
@Test
fun arrayVariableDeclaration(jp: JavaParser.Builder<*, *>) = assertChanged(
parser = jp.styles(noWhitespaceAfterStyle()).build(),
before = """
class Test {
static void method() {
int[] n = { 1, 2};
int[] p = {1, 2 };
}
}
""",
after = """
class Test {
static void method() {
int[] n = {1, 2};
int[] p = {1, 2};
}
}
""",
afterConditions = { cu ->
val nucu = AutoFormatVisitor().visit(cu, InMemoryExecutionContext {})
assertThat(nucu).isEqualTo(cu)
}
)
@Test
fun assignment(jp: JavaParser.Builder<*, *>) = assertChanged(
parser = jp.styles(noWhitespaceAfterStyle()).build(),
before = """
class Test {
static void method(int m) {
long o = - m;
}
}
""",
after = """
class Test {
static void method(int m) {
long o = -m;
}
}
""",
afterConditions = { cu ->
val nucu = AutoFormatVisitor().visit(cu, InMemoryExecutionContext {})
assertThat(nucu).isEqualTo(cu)
}
)
@Test
fun unaryOperation(jp: JavaParser.Builder<*, *>) = assertChanged(
parser = jp.styles(noWhitespaceAfterStyle()).build(),
before = """
class Test {
static void method(int m) {
++ m;
-- m;
int o = + m;
o = ~ m;
boolean b = false;
b = ! b;
}
}
""",
after = """
class Test {
static void method(int m) {
++m;
--m;
int o = +m;
o = ~m;
boolean b = false;
b = !b;
}
}
""",
afterConditions = { cu ->
val nucu = AutoFormatVisitor().visit(cu, InMemoryExecutionContext {})
assertThat(nucu).isEqualTo(cu)
}
)
@Test
fun typecastOperation(jp: JavaParser.Builder<*, *>) = assertChanged(
parser = jp.styles(noWhitespaceAfterStyle {
withTypecast(true)
}).build(),
before = """
class Test {
static void method(int m) {
long o = - m;
m = (int) o;
}
}
""",
after = """
class Test {
static void method(int m) {
long o = -m;
m = (int)o;
}
}
""",
afterConditions = { cu ->
val nucu = AutoFormatVisitor().visit(cu, InMemoryExecutionContext {})
assertThat(nucu).isEqualTo(cu)
}
)
@Test
fun methodReference(jp: JavaParser.Builder<*, *>) = assertChanged(
parser = jp.styles(noWhitespaceAfterStyle {
withMethodRef(true)
}).build(),
before = """
import java.util.stream.Stream;
class Test {
static void method(Stream