use of org.eclipse.xtext.validation.RangeBasedDiagnostic in project dsl-devkit by dsldevkit.
the class AbstractXtextTestUtil method processDiagnostic.
/**
* Gets the offset and the error message for a given {@link Diagnostic}.
*
* @param diagnostic
* instance of {@link Diagnostic}
* @return
* offset and error message
*/
private Pair<Integer, String> processDiagnostic(final Diagnostic diagnostic) {
StringBuilder errorMessage = new StringBuilder();
if (diagnostic instanceof AbstractValidationDiagnostic) {
AbstractValidationDiagnostic avd = (AbstractValidationDiagnostic) diagnostic;
errorMessage.append("Unexpected issue found. Code '");
errorMessage.append(avd.getIssueCode()).append("'\n");
errorMessage.append(avd.getMessage());
if (avd instanceof FeatureBasedDiagnostic && ((FeatureBasedDiagnostic) avd).getFeature() != null) {
List<INode> nodes = NodeModelUtils.findNodesForFeature(avd.getSourceEObject(), ((FeatureBasedDiagnostic) avd).getFeature());
if (nodes != null && !nodes.isEmpty()) {
return new Pair<Integer, String>(findFirstNonHiddenLeafNode(nodes.get(0)).getTotalOffset(), errorMessage.toString());
}
} else if (avd instanceof RangeBasedDiagnostic) {
return new Pair<Integer, String>(((RangeBasedDiagnostic) avd).getOffset(), errorMessage.toString());
} else {
return new Pair<Integer, String>(NodeModelUtils.getNode(avd.getSourceEObject()).getTotalOffset(), errorMessage.toString());
}
}
return null;
}
use of org.eclipse.xtext.validation.RangeBasedDiagnostic in project dsl-devkit by dsldevkit.
the class AbstractValidationTest method memorizeUnexpectedErrors.
/**
* Memorize the position and issue code of each unexpected diagnostic that appears in the file.
* A diagnostic is considered as expected if a marker with the issue code in the test file was set.
*/
private void memorizeUnexpectedErrors() {
for (Diagnostic diagnostic : getUnexpectedDiagnostics()) {
if (diagnostic instanceof AbstractValidationDiagnostic) {
AbstractValidationDiagnostic avd = (AbstractValidationDiagnostic) diagnostic;
// Create error message
StringBuilder sb = new StringBuilder();
sb.append("Unexpected issue found. Code '");
sb.append(avd.getIssueCode());
sb.append(DOT_AND_LINEBREAK);
// Retrieve the position and add the error
if (avd instanceof FeatureBasedDiagnostic && ((FeatureBasedDiagnostic) avd).getFeature() != null) {
List<INode> nodes = NodeModelUtils.findNodesForFeature(avd.getSourceEObject(), ((FeatureBasedDiagnostic) avd).getFeature());
if (nodes.isEmpty()) {
INode node = NodeModelUtils.getNode(avd.getSourceEObject());
memorizeErrorOnPosition(getXtextTestUtil().findFirstNonHiddenLeafNode(node).getTotalOffset(), sb.toString());
} else {
for (INode node : nodes) {
memorizeErrorOnPosition(getXtextTestUtil().findFirstNonHiddenLeafNode(node).getTotalOffset(), sb.toString());
}
}
} else if (avd instanceof RangeBasedDiagnostic) {
memorizeErrorOnPosition(((RangeBasedDiagnostic) avd).getOffset(), sb.toString());
} else {
memorizeErrorOnPosition(NodeModelUtils.getNode(avd.getSourceEObject()).getTotalOffset(), sb.toString());
}
} else {
// Create error message
StringBuilder sb = new StringBuilder();
sb.append("Unexpected diagnostic found. '");
sb.append(diagnostic.toString());
sb.append(DOT_AND_LINEBREAK);
// Add error message
memorizeErrorOnPosition(0, sb.toString());
}
}
}
Aggregations