Class MappedCondition<FROM,​TO>

  • Type Parameters:
    FROM - the type of object this condition accepts.
    TO - the type of object the nested condition accepts.
    All Implemented Interfaces:
    Descriptable<Condition<FROM>>

    public class MappedCondition<FROM,​TO>
    extends Condition<FROM>
    Container Condition that maps the object under test and then check the resulting mapped value against its nested Condition.

    Example:

     Condition<String> hasLineSeparator = new Condition<>(t -> t.contains(System.lineSeparator()), "has lineSeparator");
    
     Condition<Optional<String>> optionalWithLineSeparator = MappedCondition.mappedCondition(Optional::get, hasLineSeparator, "optional value has lineSeparator");
    
     // assertion succeeds
     assertThat(Optional.of("a" + System.lineSeparator())).is(optionalWithLineSeparator);
     // returns true
     optionalWithLineSeparator.matches(Optional.of("a" + System.lineSeparator()));
    
     // assertion fails
     assertThat(Optional.of("a")).is(optionalWithLineSeparator);
     // returns false
     optionalWithLineSeparator.matches(Optional.of("a"));
    Author:
    Stefan Bischof
    • Method Detail

      • mappedCondition

        public static <FROM,​TO> MappedCondition<FROM,​TO> mappedCondition​(Function<FROM,​TO> mapping,
                                                                                     Condition<TO> condition,
                                                                                     String mappingDescription,
                                                                                     Object... args)
        Creates a new MappedCondition.

        Example:

         Condition<String> hasLineSeparator = new Condition<>(t -> t.contains(System.lineSeparator()), "has lineSeparator");
        
         Condition<Optional<String>> optionalWithLineSeparator = MappedCondition.mappedCondition(Optional::get, hasLineSeparator, "optional value has lineSeparator");
        
         // assertion succeeds
         assertThat(Optional.of("a" + System.lineSeparator())).is(optionalWithLineSeparator);
         // returns true
         optionalWithLineSeparator.matches(Optional.of("a" + System.lineSeparator()));
        
         // assertion fails
         assertThat(Optional.of("a")).is(optionalWithLineSeparator);
         // returns false
         optionalWithLineSeparator.matches(Optional.of("a"));

        Note that the mappingDescription argument follows String.format(String, Object...) syntax.

        Type Parameters:
        FROM - the type of object the given condition accept.
        TO - the type of object the nested condition accept.
        Parameters:
        mapping - the Function that maps the value to test to the a value for the nested condition.
        condition - the nested condition to evaluate.
        mappingDescription - describes the mapping, follows String.format(String, Object...) syntax.
        args - for describing the mapping as in String.format(String, Object...) syntax.
        Returns:
        the created MappedCondition.
        Throws:
        NullPointerException - if the given condition is null.
        NullPointerException - if the given mapping is null.
      • mappedCondition

        public static <FROM,​TO> MappedCondition<FROM,​TO> mappedCondition​(Function<FROM,​TO> mapping,
                                                                                     Condition<TO> condition)
        Creates a new MappedCondition
        Type Parameters:
        FROM - the type of object the given condition accept.
        TO - the type of object the nested condition accept.
        Parameters:
        mapping - the Function that maps the value to test to the a value for the nested condition.
        condition - the nested condition to evaluate.
        Returns:
        the created MappedCondition.
        Throws:
        NullPointerException - if the given condition is null.
        NullPointerException - if the given mapping is null.
      • matches

        public boolean matches​(FROM value)
        Maps the value with the given function and verifies that it satisfies the nested Condition.
        Overrides:
        matches in class Condition<FROM>
        Parameters:
        value - the value to map
        Returns:
        true if the given mapped value satisfies the nested condition; false otherwise.
      • buildMappingDescription

        protected String buildMappingDescription​(FROM from,
                                                 TO to)
        Build the mapped condition description when applied with the FROM and TO values.
        Parameters:
        from - the value to map
        to - the mapped value
        Returns:
        the mapped condition description .
      • buildMappingDescription

        private String buildMappingDescription​(FROM from,
                                               TO to,
                                               boolean withNested)
      • conditionDescriptionWithStatus

        public Description conditionDescriptionWithStatus​(FROM actual)
        Description copied from class: Condition
        Returns the description of this condition with its status failed or success.
        Overrides:
        conditionDescriptionWithStatus in class Condition<FROM>
        Parameters:
        actual - the instance to evaluate the condition status against.
        Returns:
        the description of this condition with its status.
      • className

        private static String className​(Object object)