Class Failure

java.lang.Object
io.github.valentinherrmann.levenshtein.Failure

public final class Failure extends Object
The one format of every behaviour failure: a block of labelled lines, localized with Messages and LevenshteinSettings.getLanguage(). The framework reports exceptions of student code, invokeExpectingException and testGettersSetters with it, and test authors use it for their own checks, so students see the same format everywhere:
Method:   calculateLoudness(double, double)
Scenario: generated case 3, seed 2040003
Input:    volume = 12.50, crowdNoise = 3.00
Expected: 31.25
Actual:   27.00
throw Failure.of(methodWrapper)
        .scenario("generated case 3, seed 2040003")
        .input("volume = 12.50, crowdNoise = 3.00")
        .expected(31.25).actual(actual)
        .error();
Lines that are not set are left out. The AssertionError of error() never has a cause, so no stack trace of the student code is shown.

Structural failures (a missing class, method, ...) keep their short form and do not use this block.

  • Method Details

    • of

      public static Failure of(MethodWrapper<?,?> method)
      Parameters:
      method - the method whose behaviour is wrong (the Method line shows its name and parameter types)
      Returns:
      a new failure
    • of

      public static Failure of(ConstructorWrapper<?> constructor)
      Parameters:
      constructor - the constructor whose behaviour is wrong
      Returns:
      a new failure
    • of

      public static Failure of(String member)
      Parameters:
      member - the text of the Method line, e.g. "toString()"; null leaves the line out
      Returns:
      a new failure
    • scenario

      public Failure scenario(String text)
      Parameters:
      text - what was checked, e.g. "generated case 3, seed 2040003"
      Returns:
      this failure
    • input

      public Failure input(String text)
      Parameters:
      text - the input as it should be shown, e.g. "volume = 12.50"
      Returns:
      this failure
    • arguments

      public Failure arguments(Object... args)
      Shows argument values in the Input line, formatted like format(Object) and separated by commas. No arguments leave the line out.
      Parameters:
      args - the argument values
      Returns:
      this failure
    • expected

      public Failure expected(Object value)
      Parameters:
      value - the expected value, formatted with format(Object)
      Returns:
      this failure
    • actual

      public Failure actual(Object value)
      Parameters:
      value - the actual value, formatted with format(Object)
      Returns:
      this failure
    • expectedText

      public Failure expectedText(String text)
      Parameters:
      text - the expected outcome as it should be shown (not formatted, strings are not quoted)
      Returns:
      this failure
    • actualText

      public Failure actualText(String text)
      Parameters:
      text - the actual outcome as it should be shown (not formatted, strings are not quoted)
      Returns:
      this failure
    • thrown

      public Failure thrown(Throwable t)
      Shows an exception of the student code as actual outcome, e.g. IllegalStateException: message (never with a stack trace). A StackOverflowError adds a hint on endless recursion.
      Parameters:
      t - the exception thrown by the student code
      Returns:
      this failure
    • cases

      public Failure cases(int failed, int total)
      Adds the line "(n of m cases failed)" if more than one case was checked.
      Parameters:
      failed - the number of failed cases
      total - the number of all checked cases
      Returns:
      this failure
    • message

      public String message()
      Returns:
      the block in the current language
    • error

      public AssertionError error()
      Returns:
      a failure with the block as message and without a cause, ready to be thrown
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • format

      public static String format(Object value)
      Formats a value the same way in every failure message: double and float values with LevenshteinSettings.getDecimals() decimals (dot as separator), strings in double quotes, characters in single quotes, null, arrays and collections in brackets, very long values cut.
      Parameters:
      value - any value
      Returns:
      the formatted value
    • call

      public static <R> R call(Failure.ThrowingSupplier<R> code)
      Runs student code and turns an exception it throws into a failure block (Actual: the exception). Failures (AssertionError) and security violations are passed on unchanged.
      Type Parameters:
      R - the result type
      Parameters:
      code - the code to run, e.g. () -> wrapper.invokeRaw(...) or a call of a student method
      Returns:
      the result of the code
    • call

      public static void call(Failure.ThrowingRunnable code)
      Like call(ThrowingSupplier) for code without a result.
      Parameters:
      code - the code to run