Class 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  
     1this is the first line¶   this is the first line¶ 1
     2¶                         ¶                       2
     3the next line is empty¶   the next line is empty¶ 3
     4¶                         ¶                       4
     5¶                         ¶                       5
     6¶                      <M ///////////////////////  
     7this 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
    • Constructor Detail

      • DiffComment

        public DiffComment​(ApplicabilitySpec applicability,
                           String expected,
                           String observed,
                           Content explanation,
                           List<String> appliedNormalizersToObservedOutput)
        Initialize this object with type DiffComment.DiffSynopsisType.DETAIL.
        Parameters:
        applicability - the applicability of the comment
        expected - the expected outcome
        observed - the observed outcome
        explanation - an optional comment that is printed to explain the synopsis
        appliedNormalizersToObservedOutput - a list of normalizers that will be reported in the context of the synopsis.
    • Method Detail

      • 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 class Content
        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 class AbstractElementContent
        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()
      • 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 class Content
        Parameters:
        sb - HTML code result
        includeInfo - controls, which content to be rendered.
        ctx - render context
        useJavascript - 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 class Content
        Parameters:
        sb - plain text result
        includeInfo - controls, which content to be rendered.
        ctx - render context (unused).
        See Also:
        Content#renderHtmlImpl(StringBuilder, IncludeInfo, RenderContext)