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

com.insightfullogic.lambdabehave.expectations.BoundExpectation Maven / Gradle / Ivy

There is a newer version: 0.4
Show newest version
package com.insightfullogic.lambdabehave.expectations;

import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

import java.util.Collection;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class BoundExpectation {

    protected final boolean positive;
    protected final T objectUnderTest;

    BoundExpectation(final T value, final boolean positive) {
        this.objectUnderTest = value;
        this.positive = positive;
    }

    public  BoundExpectation and(final O value) {
        return new BoundExpectation(value, true);
    }

    public  CollectionExpectation and(final Collection collection) {
        return new CollectionExpectation(collection, true);
    }

    public StringExpectation and(final String str) {
        return new StringExpectation(str, true);
    }

    public BoundExpectation isEqualTo(final T expected) {
        return matches(equalTo(expected));
    }

    public BoundExpectation hasToString(final Matcher str) {
        return matches(Matchers.hasToString(str));
    }

    public BoundExpectation hasToString(final String str) {
        return matches(Matchers.hasToString(str));
    }

    public BoundExpectation instanceOf(final Class type) {
        return matches(Matchers.instanceOf(type));
    }

    public BoundExpectation isNotNull() {
        return matches(Matchers.notNullValue());
    }

    public BoundExpectation toBeNull() {
        return matches(Matchers.nullValue());
    }

    public BoundExpectation sameInstance(final T target) {
        return matches(Matchers.sameInstance(target));
    }

    public BoundExpectation any() {
        return matches(Matchers.any(getWrappedClass()));
    }

    public BoundExpectation hasProperty(final String propertyName, final Matcher propertyValue) {
        return matches(Matchers.hasProperty(propertyName, propertyValue));
    }

    public BoundExpectation is(final Matcher matcher) {
        return matches(matcher);
    }

    public BoundExpectation is(final T value) {
        return matches(Matchers.is(value));
    }

    public BoundExpectation isIn(final Collection values) {
        return matches(Matchers.isIn(values));
    }

    public BoundExpectation isIn(final T ... values) {
        return matches(Matchers.isIn(values));
    }

    public BoundExpectation never() {
        return new BoundExpectation<>(objectUnderTest, !positive);
    }

    private BoundExpectation matches(final Matcher matcher) {
        assertThat(objectUnderTest, negatedIfNeeded(matcher));
        return this;
    }

    protected Matcher negatedIfNeeded(final Matcher matcher) {
        return positive ? matcher : Matchers.not(matcher);
    }

    @SuppressWarnings("unchecked")
    protected Class getWrappedClass() {
        return (Class)objectUnderTest.getClass();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy