Package de.hsh.graja.comment
Class DiffComment
- java.lang.Object
-
- de.hsh.graja.util.comment.Content
-
- de.hsh.graja.util.comment.AbstractElementContent
-
- de.hsh.graja.comment.DiffComment
-
- All Implemented Interfaces:
Serializable
public class DiffComment extends AbstractElementContent
This class represents a synopsis of expected and observed outcomes of a student program.
Since comparisons between expected an observed outcomes usually is done after some normalizations, this class is capable of printing the normalizations, so that a student can understand the report, since this will deviate from the behaviour of the program when executed on a student computer.
A typical usecase of this comment is like this:
String input= ....; // some file input, that a student submitted method 'm' had to analyze. Sequence explanation= new Sequence(); String legendString= VerbatimComment.createSymbolLegendForText(input); if (!legendString.isEmpty()) { legendString= " ("+legendString+")"; } explanation.addItem(new Paragraph(includeSpec, String.format("Calling 'm' for the following file content%s:", legendString))); explanation.addItem(new Verbatim(includeSpec, WhitespaceExpansion.ALL_TO_SYMBOLS, input)); String expected= ...; // prepare a sample output String observed= ...; // invoke the student method 'm' DiffHelper.compareStrings(expected, observed) .normalizeOutput(new StringNormalizer(StringNormalizer.StandardRule.IGNORE_NEWLINE_DIFFERENCES)) .includeExplanationInDiffSynopsis(explanation) .start();
A possible output is this:
Calling 'm' for the following file content (¶=line feed): this is the first line¶ the next line is empty¶ ¶ this is the last line
Expected and observed behaviours differ.
In the following diff view the observed output was normalized by the following steps:
- transform newline differences
Expected Observed 1 this is the first line¶ this is the first line¶ 1 2 ¶ ¶ 2 3 the next line is empty¶ the next line is empty¶ 3 4 ¶ ¶ 4 5 ¶ ¶ 5 6 ¶ <M /////////////////////// 7 this is the last line¶ this is the last line¶ 6 /////////////////////// A> ¶ 7 Legend: <M=missing in observation, A>=added in observation, :///:=no output, ¶=line feed
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DiffComment.DiffSynopsisType
A diff synopsis can be either detailed with every line of expected and observed outcome compared, or it can be a simple comparison of the number of lines.
-
Field Summary
-
Fields inherited from class de.hsh.graja.util.comment.Content
EXCLUSIVE, renderUuid
-
-
Constructor Summary
Constructors Constructor Description DiffComment(ApplicabilitySpec applicability, String expected, String observed, Content explanation, List<String> appliedNormalizersToObservedOutput)
Initialize this object with typeDiffComment.DiffSynopsisType.DETAIL
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
countDifferentLines()
Calculates the number of lines, that differ.boolean
equals(Object obj)
implements the contract ofObject.equals(Object)
.List<String>
getAppliedNormalizersToObservedOutput()
List<String>
getExpectedLines()
Content
getExplanation()
Iterable<Content>
getNestedContents()
List<String>
getObservedLines()
DiffComment.DiffSynopsisType
getReportType()
int
hashCode()
implements the contract ofObject.hashCode()
.void
renderHtmlImpl(StringBuilder sb, IncludeInfo includeInfo, RenderContext ctx, boolean useJavascript)
render content to HTML.void
renderPlainImpl(StringBuilder sb, IncludeInfo includeInfo, RenderContext ctx)
render content to plain text.void
setReportType(DiffComment.DiffSynopsisType t)
sets the type of the reportString
toString(String prefix)
This method is for debugging purposes.void
trimCRLF()
deletes all CR and LF at line endings-
Methods inherited from class de.hsh.graja.util.comment.Content
click, disableCallerStacktraceRecording, enableCallerStacktraceRecording, excludeGlobally, getIncludeSpec, getRenderUuid, getStackdump, hide, include, isCallerStacktraceRecordingEnabled, isHidden, isShowing, onClick, onClick, onHide, onHide, onLoad, onLoad, onShow, onShow, registerCallable, renderHtml, renderHtml, renderIdAttribute, renderJavascriptOncePerClass, renderOnClickAttribute, renderPlain, renderPlain, setGlobalIncludeInfo, setHref, setIncludeSpec, show, toString
-
-
-
-
Constructor Detail
-
DiffComment
public DiffComment(ApplicabilitySpec applicability, String expected, String observed, Content explanation, List<String> appliedNormalizersToObservedOutput)
Initialize this object with typeDiffComment.DiffSynopsisType.DETAIL
.- Parameters:
applicability
- the applicability of the commentexpected
- the expected outcomeobserved
- the observed outcomeexplanation
- an optional comment that is printed to explain the synopsisappliedNormalizersToObservedOutput
- a list of normalizers that will be reported in the context of the synopsis.
-
-
Method Detail
-
setReportType
public void setReportType(DiffComment.DiffSynopsisType t)
sets the type of the report- Parameters:
t
- type of report
-
getReportType
public DiffComment.DiffSynopsisType getReportType()
- Returns:
- the type of the report
-
trimCRLF
public void trimCRLF()
deletes all CR and LF at line endings
-
getExpectedLines
public List<String> getExpectedLines()
- Returns:
- lines of expected output. The returned lines may not be original but may be the result of a normalization process.
-
getObservedLines
public List<String> getObservedLines()
- Returns:
- lines of observed output. The returned lines may not be original but may be the result of a normalization process.
-
getExplanation
public Content getExplanation()
- Returns:
- an explanation comment that is meant to be appended to a diff output
-
getAppliedNormalizersToObservedOutput
public List<String> getAppliedNormalizersToObservedOutput()
- Returns:
- a list of normalizer rules that have been applied to the originally observed thereby changing the observation.
-
countDifferentLines
public int countDifferentLines()
Calculates the number of lines, that differ.- Returns:
- number of differing lines.
-
toString
public String toString(String prefix)
Description copied from class:Content
This method is for debugging purposes.- Overrides:
toString
in classContent
- Parameters:
prefix
- If the output has multiple lines,prefix
specifies the indentation of the subsequent lines.- Returns:
- a string representation of the content and it's internal structure.
- See Also:
Content.toString(java.lang.String)
-
getNestedContents
public Iterable<Content> getNestedContents()
- Overrides:
getNestedContents
in classAbstractElementContent
- Returns:
- the set of contents nested in this content. Nested contents can be list items in a List, elements in a Sequence, elements in a Grid, elements in a Paragraph, and so on. If this is a leaf content, the returned set is empty.
- See Also:
Content#getNestedComments()
-
hashCode
public int hashCode()
implements the contract ofObject.hashCode()
.- Overrides:
hashCode
in classContent
- See Also:
Object.hashCode()
-
equals
public boolean equals(Object obj)
implements the contract ofObject.equals(Object)
.
-
renderHtmlImpl
public void renderHtmlImpl(StringBuilder sb, IncludeInfo includeInfo, RenderContext ctx, boolean useJavascript)
Description copied from class:Content
render content to HTML. This method is meant to be overriden by subclasses.- Specified by:
renderHtmlImpl
in classContent
- Parameters:
sb
- HTML code resultincludeInfo
- controls, which content to be rendered.ctx
- render contextuseJavascript
- if true, the generated HTML code will contain Javascript- See Also:
Content#renderHtmlImpl(StringBuilder, IncludeInfo, RenderContext)
-
renderPlainImpl
public void renderPlainImpl(StringBuilder sb, IncludeInfo includeInfo, RenderContext ctx)
Description copied from class:Content
render content to plain text. This method is meant to be overriden by subclasses.- Specified by:
renderPlainImpl
in classContent
- Parameters:
sb
- plain text resultincludeInfo
- controls, which content to be rendered.ctx
- render context (unused).- See Also:
Content#renderHtmlImpl(StringBuilder, IncludeInfo, RenderContext)
-
-