Skip to content

Commit

Permalink
Add whitespace validation to RewriteTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Feb 3, 2024
1 parent 2923de5 commit 33d8f7d
Show file tree
Hide file tree
Showing 41 changed files with 326 additions and 99 deletions.
5 changes: 5 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/SourceFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ default <P> TreeVisitor<?, PrintOutputCapture<P>> printer(Cursor cursor) {
throw new UnsupportedOperationException("SourceFile implementations should override this method");
}

@Incubating(since = "8.2.0")
default <S, T extends S> T service(Class<S> service) {
throw new UnsupportedOperationException("Service " + service + " not supported");
}

/**
* A measure of the size of the AST by count of number of AST nodes or some other similar measure. Because perfect referential
* uniqueness is space inefficient, this weight will always be approximate and is best used for comparative size between two ASTs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.internal;

import org.openrewrite.ExecutionContext;
import org.openrewrite.TreeVisitor;

/**
* Return a visitor suitable for use by the test framework to validate that whitespace contains no non-whitespace characters.
*/
public interface WhitespaceValidationService {

TreeVisitor<?, ExecutionContext> getVisitor();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,58 +13,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.groovy;
package org.openrewrite.groovy.internal;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.groovy.GroovyVisitor;
import org.openrewrite.groovy.tree.GSpace;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.WhitespaceValidationService;
import org.openrewrite.java.tree.Space;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

public interface GroovyParserTest extends RewriteTest {
@Override
default void defaults(RecipeSpec spec) {
spec.recipe(new SpaceContainsOnlyWhitespace());
}
}

@Value
@EqualsAndHashCode(callSuper = false)
class SpaceContainsOnlyWhitespace extends Recipe {

@Override
public String getDisplayName() {
return "Space contains only whitespace";
}

@Override
public String getDescription() {
return "Spaces containing non-whitespace characters is a common parser failure mode not otherwise detected.";
}

public class GroovyWhitespaceValidationService implements WhitespaceValidationService {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new GroovyVisitor<>() {
return new GroovyVisitor<ExecutionContext>() {
@Override
public Space visitSpace(Space space, GSpace.Location loc, ExecutionContext executionContext) {
if(!space.getWhitespace().isBlank()) {
if(!StringUtils.isBlank(space.getWhitespace())) {
return space.withWhitespace("~~(non-whitespace)~~>" + space.getWhitespace() + "<~~");
}
return space;
}

@Override
public Space visitSpace(Space space, Space.Location loc, ExecutionContext executionContext) {
if(!space.getWhitespace().isBlank()) {
if(!StringUtils.isBlank(space.getWhitespace())) {
return space.withWhitespace("~~(non-whitespace)~~>" + space.getWhitespace() + "<~~");
}
return space;
}
};

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
@NonNullApi
package org.openrewrite.groovy.internal;

import org.openrewrite.internal.lang.NonNullApi;
11 changes: 11 additions & 0 deletions rewrite-groovy/src/main/java/org/openrewrite/groovy/tree/G.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.openrewrite.*;
import org.openrewrite.groovy.GroovyPrinter;
import org.openrewrite.groovy.GroovyVisitor;
import org.openrewrite.groovy.internal.GroovyWhitespaceValidationService;
import org.openrewrite.internal.WhitespaceValidationService;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.internal.TypesInUse;
import org.openrewrite.java.tree.*;
Expand Down Expand Up @@ -136,6 +138,15 @@ public G.CompilationUnit withPackageDeclaration(Package packageDeclaration) {
return getPadding().withPackageDeclaration(JRightPadded.withElement(this.packageDeclaration, packageDeclaration));
}

@SuppressWarnings("unchecked")
@Override
public <S, T extends S> T service(Class<S> service) {
if(WhitespaceValidationService.class.getName().equals(service.getName())) {
return (T) new GroovyWhitespaceValidationService();
}
return JavaSourceFile.super.service(service);
}

List<JRightPadded<Statement>> statements;

public List<Statement> getStatements() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings({"GroovyUnusedAssignment", "GrUnnecessarySemicolon"})
class AssertTest implements GroovyParserTest {
class AssertTest implements RewriteTest {

@Issue("https://github.com/openrewrite/rewrite/issues/3473")
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings({"GroovyUnusedAssignment", "GrUnnecessarySemicolon"})
class AssignmentTest implements GroovyParserTest {
class AssignmentTest implements RewriteTest {

@Test
void concat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings({"GroovyUnusedAssignment", "GrUnnecessarySemicolon", "UnnecessaryQualifiedReference"})
class BinaryTest implements GroovyParserTest {
class BinaryTest implements RewriteTest {

@SuppressWarnings("GroovyConstantConditional")
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings("GrUnnecessarySemicolon")
class BlockTest implements GroovyParserTest {
class BlockTest implements RewriteTest {

@Test
void block() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
Expand All @@ -28,7 +28,7 @@
import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings("GrUnnecessaryPublicModifier")
class ClassDeclarationTest implements GroovyParserTest {
class ClassDeclarationTest implements RewriteTest {

@Test
void multipleClassDeclarationsInOneCompilationUnit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

class ClassExpressionTest implements GroovyParserTest {
class ClassExpressionTest implements RewriteTest {

@Test
void classExpressions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.groovy.GroovyParser;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings("GroovyUnusedAssignment")
class CompilationUnitTest implements GroovyParserTest {
class CompilationUnitTest implements RewriteTest {

@SuppressWarnings("GrPackage")
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

public class ConstructorTest implements GroovyParserTest {
public class ConstructorTest implements RewriteTest {

@Test
void inParens() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

class EnumTest implements GroovyParserTest {
class EnumTest implements RewriteTest {

@Disabled
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

class FieldAccessTest implements GroovyParserTest {
class FieldAccessTest implements RewriteTest {

@Test
void starAccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings({"GroovyEmptyStatementBody", "GroovyUnusedAssignment", "GrUnnecessarySemicolon", "GroovyUnnecessaryContinue"})
class ForLoopTest implements GroovyParserTest {
class ForLoopTest implements RewriteTest {

@Test
void forLoopMultipleInit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings({"GroovyIfStatementWithIdenticalBranches", "GroovyConstantIfStatement", "GrUnnecessarySemicolon", "GroovyUnusedIncOrDec", "GroovyEmptyStatementBody"})
class IfTest implements GroovyParserTest {
class IfTest implements RewriteTest {

@Test
void ifElse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.java.tree.Space;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.groovy.Assertions.groovy;

class ImportTest implements GroovyParserTest {
class ImportTest implements RewriteTest {
@Test
void classImport() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

class LabelsTest implements GroovyParserTest {
class LabelsTest implements RewriteTest {

@Test
void singleLabel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings("GroovyUnusedAssignment")
class LambdaTest implements GroovyParserTest {
class LambdaTest implements RewriteTest {

@Test
void lambdaExpressionNoParens() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
package org.openrewrite.groovy.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.groovy.GroovyParserTest;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;

@SuppressWarnings("GroovyUnusedAssignment")
class ListTest implements GroovyParserTest {
class ListTest implements RewriteTest {

@Test
void listLiteral() {
Expand Down
Loading

0 comments on commit 33d8f7d

Please sign in to comment.