All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.serenitybdd.screenplay.GivenWhenThen Maven / Gradle / Ivy

There is a newer version: 4.2.9
Show newest version
package net.serenitybdd.screenplay;

import net.serenitybdd.screenplay.questions.ConsequenceGroup;
import net.serenitybdd.screenplay.questions.NamedPredicate;
import org.hamcrest.Matcher;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import static org.hamcrest.MatcherAssert.assertThat;

public class GivenWhenThen {
    public static  T givenThat(T actor) {
        return actor;
    }
    public static Actor andThat(Actor actor) {return actor; }

    public static Actor when(Actor actor) {  return actor; }
    public static Actor then(Actor actor) { return actor; }
    public static Actor and(Actor actor) { return actor; }
    public static Actor but(Actor actor) { return actor; }

    public static  void then(T actual, Matcher matcher) {
        assertThat(actual, matcher);
    }

    public static  Consequence seeThat(Question actual, Matcher expected) {
        return new QuestionConsequence(actual, expected);
    }

    public static  Consequence seeThat(Question actual, Predicate expected) {
        return new PredicateConsequence(actual, expected);
    }

    public static  Consequence seeThat(String subject, Question actual, Predicate expected) {
        return new PredicateConsequence(subject, actual, expected);
    }

    public static  Consequence seeThat(String subject, Question actual, Matcher expected) {
        return new QuestionConsequence(subject, actual, expected);
    }

    public static  Consequence seeThat(Question actual) {
        return new BooleanQuestionConsequence(actual);
    }

    public static  Consequence seeThat(String subject, Question actual) {
        return new BooleanQuestionConsequence(subject, actual);
    }

    public static  Consequence[] seeThat(Question actual, Matcher... expectedMatchers) {

        if (thereAreNo(expectedMatchers)) {
            return consequenceGroupFor(actual);
        } else {
            return consequencesForEachMatcher(actual, expectedMatchers);
        }
    }

    private static  Consequence[] consequenceGroupFor(Question actual) {
        return new Consequence[]{ new ConsequenceGroup(actual)};
    }

    private static  Consequence[] consequencesForEachMatcher(Question actual, Matcher[] expectedMatchers) {
        List> consequences = new ArrayList<>();

        for(Matcher matcher : expectedMatchers) {
            consequences.add(new QuestionConsequence(actual, matcher));
        }
        return consequences.toArray(new Consequence[]{});
    }

    private static  boolean thereAreNo(Matcher[] expectedMatchers) {
        return expectedMatchers.length == 0;
    }

    public static  Consequence[] seeThat(String subject, Question actual, Matcher... expectedMatchers) {
        List> consequences = new ArrayList<>();
        for(Matcher matcher : expectedMatchers) {
            consequences.add(new QuestionConsequence(subject, actual, matcher));
        }
        return consequences.toArray(new Consequence[]{});
    }

    public static  Task seeIf(Question question, Matcher matcher) {
        Consequence consequence = seeThat(question, matcher);
        return Task.where("See if " + question.toString() + " " + matcher.toString(),
                (Performable) consequence::evaluateFor
        );
    }

    public static  NamedPredicate returnsAValueThat(String name, Predicate predicate) {
        return new NamedPredicate(name, predicate);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy