@Test public void testPresentInstances_callingIteratorTwice() { List<Optional<String>> optionals = ImmutableList.of(Optional.of("a"), Optional.<String>absent(), Optional.of("c")); Iterable<String> onlyPresent = Optional.presentInstances(optionals); assertThat(onlyPresent).containsAllOf("a", "c"); }
@Test public void testOf_null() { try { Optional.of(null); fail(); } catch (NullPointerException expected) { } }
@Test public void testEqualsAndHashCode_present() { assertEquals(Optional.of("training"), Optional.of("training")); assertFalse(Optional.of("a").equals(Optional.of("b"))); assertFalse(Optional.of("a").equals(Optional.absent())); assertEquals(Optional.of("training").hashCode(), Optional.of("training").hashCode()); }
@Test public void testAsSet_presentIsImmutable() { Set<String> presentAsSet = Optional.of("a").asSet(); try { presentAsSet.add("b"); fail(); } catch (UnsupportedOperationException expected) { } }
@Test public void testOrNull_present() { assertEquals("a", Optional.of("a").orNull()); }
@Test public void testOr_Optional_absent() { assertEquals(Optional.of("fallback"), Optional.absent().or(Optional.of("fallback"))); }
@Test public void testOr_T_present() { assertEquals("a", Optional.of("a").or("default")); }
@Test public void testGet_present() { assertEquals("training", Optional.of("training").get()); }
@Test public void testIsPresent_yes() { assertTrue(Optional.of("training").isPresent()); }
@Test public void testPresentInstances_somePresent() { List<Optional<String>> optionals = ImmutableList.of(Optional.of("a"), Optional.<String>absent(), Optional.of("c")); assertThat(Optional.presentInstances(optionals)).containsAllOf("a", "c"); }
@Test public void testToString_present() { assertEquals("Optional.of(training)", Optional.of("training").toString()); }
@Test public void testAsSet_present() { Set<String> expected = Collections.singleton("a"); assertEquals(expected, Optional.of("a").asSet()); }