Ternary implements Scalar<T> { @Override public T value(final Field<T> field) { final T result; if (this.pred.resolve(field)) { result = field.actual(this.right); } else { result = field.actual(this.wrong); } return result; } Ternary(final Predicate<Field<T>> pred, final Scalar<T> oks, final Scalar<T> kos); @Override T value(final Field<T> field);  }
@Test public void valueToFirstScalarWhenTrue() { final Scalar<Object> first = new Scalar.Default<>(new Object()); final Scalar<Object> second = new Scalar.Default<>(new Object()); final SpyField<Object> field = new SpyField<>(new Object(), new Object()); new Ternary<>(new True<>(), first, second).value(field); final Optional<List<Scalar<Object>>> params = field.calls().actuals(); MatcherAssert.assertThat(params.isPresent(), Matchers.equalTo(true)); MatcherAssert.assertThat(params.get().contains(first), Matchers.equalTo(true)); MatcherAssert.assertThat(params.get().contains(second), Matchers.equalTo(false)); }
@Test public void valueToSecondScalarWhenFalse() { final Scalar<Object> first = new Scalar.Default<>(new Object()); final Scalar<Object> second = new Scalar.Default<>(new Object()); final SpyField<Object> field = new SpyField<>(new Object(), new Object()); new Ternary<>(new False<>(), first, second).value(field); final Optional<List<Scalar<Object>>> params = field.calls().actuals(); MatcherAssert.assertThat(params.isPresent(), Matchers.equalTo(true)); MatcherAssert.assertThat(params.get().contains(first), Matchers.equalTo(false)); MatcherAssert.assertThat(params.get().contains(second), Matchers.equalTo(true)); }