Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions rewrite-go/src/main/java/org/openrewrite/golang/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.golang;

import org.jspecify.annotations.Nullable;
import org.openrewrite.ParseExceptionResult;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.golang.marker.GoProject;
Expand All @@ -24,23 +25,68 @@
import org.openrewrite.golang.tree.GoSum;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.test.SourceSpec;
import org.openrewrite.test.SourceSpecs;
import org.openrewrite.test.TypeValidation;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import static java.util.stream.Collectors.joining;

public final class Assertions {
private Assertions() {
}

public static SourceFile validateTypes(SourceFile source, TypeValidation typeValidation) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed; whilst already valuable, for now this is a misnomer: we only validate no unknowns/erroneous elements are added, whereas we likely want to validate no missing/nil types are present.

TypeValidation itself is misnamed as well, as it's been adding more and more types of validations we could opt into, like the addition that no non-whitespace characters should be present in any prefix, as another guard on the parser we could add here.

if (source instanceof JavaSourceFile) {
if (typeValidation.erroneous()) {
List<J.Erroneous> erroneous = new JavaIsoVisitor<List<J.Erroneous>>() {
@Override
public J.Erroneous visitErroneous(J.Erroneous erroneous, List<J.Erroneous> list) {
list.add(erroneous);
return super.visitErroneous(erroneous, list);
}
}.reduce(source, new ArrayList<>());
if (!erroneous.isEmpty()) {
throw new IllegalStateException("LST contains erroneous nodes\n" + erroneous.stream()
.map(J.Erroneous::getText)
.collect(joining("\n\n")));
}
}
if (typeValidation.unknown()) {
List<J.Unknown> unknowns = new JavaIsoVisitor<List<J.Unknown>>() {
@Override
public J.Unknown visitUnknown(J.Unknown unknown, List<J.Unknown> list) {
list.add(unknown);
return super.visitUnknown(unknown, list);
}
}.reduce(source, new ArrayList<>());
if (!unknowns.isEmpty()) {
throw new IllegalStateException("LST contains unknown elements\n" + unknowns.stream()
.map(unknown -> unknown.getSource().getMarkers()
.findFirst(ParseExceptionResult.class)
.map(ParseExceptionResult::getMessage)
.orElse("") + unknown.getSource().getText())
.collect(joining("\n\n")));
}
}
}
return source;
}

public static SourceSpecs go(@Nullable String before) {
return go(before, s -> {
});
}

public static SourceSpecs go(@Nullable String before, Consumer<SourceSpec<Go.CompilationUnit>> spec) {
SourceSpec<Go.CompilationUnit> go = new SourceSpec<>(Go.CompilationUnit.class, null, GolangParser.builder(), before, null);
SourceSpec<Go.CompilationUnit> go = new SourceSpec<>(Go.CompilationUnit.class, null, GolangParser.builder(), before,
Assertions::validateTypes, ctx -> {
});
spec.accept(go);
return go;
}
Expand All @@ -52,7 +98,9 @@ public static SourceSpecs go(@Nullable String before, String after) {

public static SourceSpecs go(@Nullable String before, String after,
Consumer<SourceSpec<Go.CompilationUnit>> spec) {
SourceSpec<Go.CompilationUnit> go = new SourceSpec<>(Go.CompilationUnit.class, null, GolangParser.builder(), before, s -> after);
SourceSpec<Go.CompilationUnit> go = new SourceSpec<>(Go.CompilationUnit.class, null, GolangParser.builder(), before,
Assertions::validateTypes, ctx -> {
}).after(s -> after);
spec.accept(go);
return go;
}
Expand Down
Loading