com.insightfullogic.lambdabehave.expectations.BoundExpectation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lambda-behave Show documentation
Show all versions of lambda-behave Show documentation
A modern testing and behavioural specification framework for Java 8
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 super String> 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 super T> 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 super T> matcher) {
assertThat(objectUnderTest, negatedIfNeeded(matcher));
return this;
}
protected Matcher super T> negatedIfNeeded(final Matcher super T> matcher) {
return positive ? matcher : Matchers.not(matcher);
}
@SuppressWarnings("unchecked")
protected Class getWrappedClass() {
return (Class)objectUnderTest.getClass();
}
}