OptionAssertion { @NotNull public OptionAssertion<T> assertValue(@NotNull final T expected) { checkNotNull(expected, "Expected value cannot be null: use assertNone instead"); if (!actual.isSome()) { throw fail("Option was not Some"); } if (!matches(actual, equalsPredicate(expected))) { throw fail(String.format("Actual Option value: <%s> did not equal expected value: <%s>", OptionUnsafe.getUnsafe(actual), expected)); } return this; } OptionAssertion(@NotNull final Option<T> actual); void assertIsNone(); @NotNull OptionAssertion<T> assertIsSome(); @NotNull OptionAssertion<T> assertValue(@NotNull final T expected); @NotNull OptionAssertion<T> assertValue(@NotNull final Func1<T, Boolean> predicate);  }
@Test public void assertValue_prohibitsNullValue() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage(startsWith("Expected value cannot be null: use assertNone instead")); new OptionAssertion<Object>(Option.none()) .assertValue((Object) null); }
@Test public void assertValue_prohibitsNullPredicateFunction() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage(startsWith("Predicate function cannot be null")); new OptionAssertion<Object>(Option.none()) .assertValue(null); }
@Test public void assertValue_doesNotThrowAssertionError_whenPredicateTrue() { final String expected = "value"; new OptionAssertion<String>(Option.ofObj("value")) .assertValue(new Func1<String, Boolean>() { @Override public Boolean call(final String actualValue) { return actualValue.equals(expected); } }); }
@Test public void assertValue_throwsAssertionError_whenPredicateFalse() { thrown.expect(AssertionError.class); thrown.expectMessage(startsWith("Actual Option value: <value> did not match predicate")); new OptionAssertion<String>(Option.ofObj("value")) .assertValue(new Func1<String, Boolean>() { @Override public Boolean call(final String actualValue) { return actualValue.equals("different"); } }); }
@Test public void assertValue_throwsAssertionError_whenOptionNone() { thrown.expect(AssertionError.class); thrown.expectMessage(startsWith("Option was not Some")); new OptionAssertion<Object>(Option.none()) .assertValue(new Func1<Object, Boolean>() { @Override public Boolean call(final Object actualValue) { return actualValue.equals("expected"); } }); }
@Test public void assertValue_doesNotThrowAssertionError_whenEqualTo() { String actual = "value"; String expected = "value"; new OptionAssertion<String>(Option.ofObj(actual)) .assertValue(expected); }
@Test public void assertValue_throwsAssertionError_whenNotEqualTo() { thrown.expect(AssertionError.class); thrown.expectMessage( startsWith( "Actual Option value: <actual> did not equal expected value: <expected>")); String actual = "actual"; new OptionAssertion<String>(Option.ofObj(actual)) .assertValue("expected"); }
@Test public void assertValue_throwsAssertionError_whenOptionNone_notEqualTo() { thrown.expect(AssertionError.class); thrown.expectMessage(startsWith("Option was not Some")); new OptionAssertion<Object>(Option.none()) .assertValue("expected"); }