ContextualValidationEventFormatter implements ValidationEventFormatter { @Override public String format(ValidationEvent event) { StringWriter writer = new StringWriter(); Formatter formatter = new Formatter(writer); formatter.format("%s: %s (%s)%n", event.getSeverity(), event.getShapeId().map(ShapeId::toString).orElse("-"), event.getId()); if (event.getSourceLocation() != SourceLocation.NONE) { String humanReadableFilename = getHumanReadableFilename(event.getSourceLocation()); String contextualLine = null; try { contextualLine = loadContextualLine(event.getSourceLocation()); } catch (IOException e) { } if (contextualLine == null) { formatter.format(" @ %s%n", event.getSourceLocation()); } else { formatter.format(" @ %s%n", humanReadableFilename); formatter.format(" |%n"); formatter.format("%4d | %s%n", event.getSourceLocation().getLine(), contextualLine); formatter.format(" | %" + event.getSourceLocation().getColumn() + "s%n", "^"); } } formatter.format(" = %s%n", event.getMessage().replace("\n", " \n")); formatter.flush(); return writer.toString(); }  @Override String format(ValidationEvent event);  }
@Test public void loadsContext() { Model model = Model.assembler() .addImport(getClass().getResource("context.smithy")) .assemble() .unwrap(); Shape shape = model.expectShape(ShapeId.from("example.smithy#Foo")); ValidationEvent event = ValidationEvent.builder() .id("foo") .severity(Severity.ERROR) .message("This is the message") .shape(shape) .build(); String format = new ContextualValidationEventFormatter().format(event); format = format.replace("\r\n", "\n"); assertThat(format, startsWith("ERROR: example.smithy#Foo (foo)")); assertThat(format, containsString("\n @ ")); assertThat(format, endsWith( "\n |" + "\n 3 | structure Foo {" + "\n | ^" + "\n = This is the message" + "\n")); }
@Test public void doesNotLoadSourceLocationNone() { ValidationEvent event = ValidationEvent.builder() .id("foo") .severity(Severity.ERROR) .message("This is the message") .sourceLocation(SourceLocation.NONE) .build(); String format = new ContextualValidationEventFormatter().format(event); format = format.replace("\r\n", "\n"); assertThat(format, equalTo( "ERROR: - (foo)" + "\n = This is the message" + "\n")); }