Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ private void next() {

private void addDiagnostic(DiagnosticCapable builder) {
builder.diagnostics()
.diagnostic(currentDiagnostic.diagnosticInfo().severity(), currentDiagnostic.message());
.diagnostic(currentDiagnostic.diagnosticInfo().severity(), currentDiagnostic.message(),
currentDiagnostic.location().lineRange());
}

private static boolean hasDiagnosticPassed(LinePosition nodeStartLine, LinePosition diagnosticEndLine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.flowmodelgenerator.core.model;

import io.ballerina.tools.diagnostics.DiagnosticSeverity;
import io.ballerina.tools.text.LineRange;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -33,13 +34,19 @@
public record Diagnostics(boolean hasDiagnostics, List<Info> diagnostics) {

/**
* Represents diagnostic information with severity and message.
* Represents diagnostic information with severity, message and range.
*
* @param severity severity of the diagnostic
* @param message message of the diagnostic
* @param range source line range of the diagnostic
* @since 1.0.0
*/
public record Info(DiagnosticSeverity severity, String message) { }
public record Info(DiagnosticSeverity severity, String message, LineRange range) {

public Info(DiagnosticSeverity severity, String message) {
this(severity, message, null);
}
}

public static class Builder<T> extends FacetedBuilder<T> {

Expand Down Expand Up @@ -67,6 +74,11 @@ public Builder<T> diagnostic(DiagnosticSeverity severity, String message) {
return this;
}

public Builder<T> diagnostic(DiagnosticSeverity severity, String message, LineRange range) {
this.diagnostics.add(new Info(severity, message, range));
return this;
}

public Diagnostics build() {
return new Diagnostics(hasDiagnostics || !diagnostics.isEmpty(),
diagnostics.isEmpty() ? null : diagnostics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.flowmodelgenerator.extension;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.ballerina.flowmodelgenerator.extension.request.FlowModelSourceGeneratorRequest;
Expand Down Expand Up @@ -49,11 +50,16 @@ public void test(Path config) throws IOException {
JsonElement flowNode = getResponse(request).get("flowNode");
notifyDidClose(sourcePath);

if (!flowNode.equals(testConfig.output())) {
assertDiagnosticsIncludeRange(flowNode, "flowNode");
JsonElement normalizedActualFlowNode = flowNode.deepCopy();
stripDiagnosticRanges(normalizedActualFlowNode);
JsonElement normalizedExpectedFlowNode = testConfig.output().deepCopy();
stripDiagnosticRanges(normalizedExpectedFlowNode);
if (!normalizedActualFlowNode.equals(normalizedExpectedFlowNode)) {
TestConfig updateConfig = new TestConfig(testConfig.source(), testConfig.description(),
testConfig.flowNode(), flowNode);
// updateConfig(configJsonPath, updateConfig);
compareJsonElements(flowNode, testConfig.output());
compareJsonElements(normalizedActualFlowNode, normalizedExpectedFlowNode);
Assert.fail(String.format("Failed test: '%s' (%s)", testConfig.description(), configJsonPath));
}
}
Expand All @@ -77,7 +83,13 @@ public void testMultipleRequests() throws IOException, InterruptedException {
FlowModelSourceGeneratorRequest finalReq = new FlowModelSourceGeneratorRequest(sourcePath, flowNode);
JsonObject response = getResponse(finalReq);
notifyDidClose(sourcePath);
Assert.assertEquals(response.get("flowNode"), testConfig.output());
JsonElement actualFlowNode = response.get("flowNode");
assertDiagnosticsIncludeRange(actualFlowNode, "flowNode");
JsonElement normalizedActualFlowNode = actualFlowNode.deepCopy();
stripDiagnosticRanges(normalizedActualFlowNode);
JsonElement normalizedExpectedFlowNode = testConfig.output().deepCopy();
stripDiagnosticRanges(normalizedExpectedFlowNode);
Assert.assertEquals(normalizedActualFlowNode, normalizedExpectedFlowNode);
}

@Override
Expand All @@ -101,4 +113,71 @@ public String description() {
return description == null ? "" : description;
}
}

private static void stripDiagnosticRanges(JsonElement jsonElement) {
if (jsonElement == null || jsonElement.isJsonNull()) {
return;
}
if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
for (JsonElement element : jsonArray) {
stripDiagnosticRanges(element);
}
return;
}
if (!jsonElement.isJsonObject()) {
return;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("diagnostics")) {
JsonElement diagnosticsElement = jsonObject.get("diagnostics");
if (diagnosticsElement.isJsonObject()) {
JsonArray diagnosticsArray = diagnosticsElement.getAsJsonObject().getAsJsonArray("diagnostics");
if (diagnosticsArray != null) {
for (JsonElement diagnosticElement : diagnosticsArray) {
if (diagnosticElement.isJsonObject()) {
diagnosticElement.getAsJsonObject().remove("range");
}
}
}
}
}
for (String key : jsonObject.keySet()) {
stripDiagnosticRanges(jsonObject.get(key));
}
}

private static void assertDiagnosticsIncludeRange(JsonElement jsonElement, String path) {
if (jsonElement == null || jsonElement.isJsonNull()) {
return;
}
if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
assertDiagnosticsIncludeRange(jsonArray.get(i), path + "[" + i + "]");
}
return;
}
if (!jsonElement.isJsonObject()) {
return;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("diagnostics")) {
JsonElement diagnosticsElement = jsonObject.get("diagnostics");
if (diagnosticsElement.isJsonObject()) {
JsonArray diagnosticsArray = diagnosticsElement.getAsJsonObject().getAsJsonArray("diagnostics");
if (diagnosticsArray != null) {
for (int i = 0; i < diagnosticsArray.size(); i++) {
JsonElement diagnosticElement = diagnosticsArray.get(i);
Assert.assertTrue(diagnosticElement.isJsonObject()
&& diagnosticElement.getAsJsonObject().has("range"),
String.format("Expected diagnostic range at %s.diagnostics.diagnostics[%d]", path, i));
}
}
}
}
for (String key : jsonObject.keySet()) {
assertDiagnosticsIncludeRange(jsonObject.get(key), path + "." + key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package io.ballerina.flowmodelgenerator.extension;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import io.ballerina.flowmodelgenerator.extension.request.FlowModelGeneratorRequest;
Expand Down Expand Up @@ -54,17 +56,34 @@ public void test(Path config) throws IOException {
boolean fileNameEquality = testFileName != null && balFileName.equals(testFileName.getAsString());
JsonObject modifiedDiagram = jsonModel.deepCopy();
modifiedDiagram.addProperty("fileName", balFileName);
assertDiagnosticsIncludeRange(modifiedDiagram, "diagram");
JsonObject normalizedActualDiagram = modifiedDiagram.deepCopy();
stripDiagnosticRanges(normalizedActualDiagram);
JsonObject normalizedExpectedDiagram = testConfig.diagram().deepCopy();
stripDiagnosticRanges(normalizedExpectedDiagram);

boolean flowEquality = modifiedDiagram.equals(testConfig.diagram());
boolean flowEquality = normalizedActualDiagram.equals(normalizedExpectedDiagram);
if (!fileNameEquality || !flowEquality) {
TestConfig updatedConfig = new TestConfig(testConfig.start(), testConfig.end(), testConfig.source(),
testConfig.description(), modifiedDiagram);
// updateConfig(configJsonPath, updatedConfig);
compareJsonElements(modifiedDiagram, testConfig.diagram());
compareJsonElements(normalizedActualDiagram, normalizedExpectedDiagram);
Assert.fail(String.format("Failed test: '%s' (%s)", testConfig.description(), configJsonPath));
}
}

@Test
public void testDiagnosticRangesInFlowModel() throws IOException {
Path configJsonPath = configDir.resolve("diagnostics1.json");
TestConfig testConfig = gson.fromJson(Files.newBufferedReader(configJsonPath), TestConfig.class);

FlowModelGeneratorRequest request = new FlowModelGeneratorRequest(
getSourcePath(testConfig.source()), testConfig.start(), testConfig.end());
JsonObject jsonModel = getResponseAndCloseFile(request, testConfig.source()).getAsJsonObject("flowModel");

assertDiagnosticsIncludeRange(jsonModel, "diagram");
}

@Override
protected String[] skipList() {
return new String[]{
Expand Down Expand Up @@ -107,4 +126,72 @@ public String description() {
return description == null ? "" : description;
}
}

private static void stripDiagnosticRanges(JsonElement jsonElement) {
if (jsonElement == null || jsonElement.isJsonNull()) {
return;
}
if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
for (JsonElement element : jsonArray) {
stripDiagnosticRanges(element);
}
return;
}
if (!jsonElement.isJsonObject()) {
return;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("diagnostics")) {
JsonElement diagnosticsElement = jsonObject.get("diagnostics");
if (diagnosticsElement.isJsonObject()) {
JsonObject diagnosticsObject = diagnosticsElement.getAsJsonObject();
JsonArray diagnosticsArray = diagnosticsObject.getAsJsonArray("diagnostics");
if (diagnosticsArray != null) {
for (JsonElement diagnosticElement : diagnosticsArray) {
if (diagnosticElement.isJsonObject()) {
diagnosticElement.getAsJsonObject().remove("range");
}
}
}
}
}
for (String key : jsonObject.keySet()) {
stripDiagnosticRanges(jsonObject.get(key));
}
}

private static void assertDiagnosticsIncludeRange(JsonElement jsonElement, String path) {
if (jsonElement == null || jsonElement.isJsonNull()) {
return;
}
if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
assertDiagnosticsIncludeRange(jsonArray.get(i), path + "[" + i + "]");
}
return;
}
if (!jsonElement.isJsonObject()) {
return;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("diagnostics")) {
JsonElement diagnosticsElement = jsonObject.get("diagnostics");
if (diagnosticsElement.isJsonObject()) {
JsonArray diagnosticsArray = diagnosticsElement.getAsJsonObject().getAsJsonArray("diagnostics");
if (diagnosticsArray != null) {
for (int i = 0; i < diagnosticsArray.size(); i++) {
JsonElement diagnosticElement = diagnosticsArray.get(i);
Assert.assertTrue(diagnosticElement.isJsonObject()
&& diagnosticElement.getAsJsonObject().has("range"),
String.format("Expected diagnostic range at %s.diagnostics.diagnostics[%d]", path, i));
}
}
}
}
for (String key : jsonObject.keySet()) {
assertDiagnosticsIncludeRange(jsonObject.get(key), path + "." + key);
}
}
}
Loading