public static Object good(AnInterface s) { return Functions.constant(s); } }
public void testIndex_nullKey() { List<Integer> values = Arrays.asList(1, 2); try { Multimaps.index(values, Functions.constant(null)); fail(); } catch (NullPointerException expected) { } }
/** Null keys aren't allowed either. */ public void testUniqueIndexNullKey() { List<String> oneStringList = Lists.newArrayList("foo"); try { Maps.uniqueIndex(oneStringList, Functions.constant(null)); fail(); } catch (NullPointerException expected) { } }
/** Null values are not allowed. */ public void testUniqueIndexNullValue() { List<String> listWithNull = Lists.newArrayList((String) null); try { Maps.uniqueIndex(listWithNull, Functions.constant(1)); fail(); } catch (NullPointerException expected) { } }
/** Can't create the map if more than one value maps to the same key. */ public void testUniqueIndexDuplicates() { try { Map<Integer, String> unused = Maps.uniqueIndex(ImmutableSet.of("one", "uno"), Functions.constant(1)); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("Multimaps.index"); } }
public void testTransform_genericsNull() throws Exception { ListenableFuture<?> nullFuture = immediateFuture(null); ListenableFuture<?> transformedFuture = transform(nullFuture, constant(null), directExecutor()); assertNull(getDone(transformedFuture)); }
public void testToMapWithNullKeys() { Iterable<String> strings = Arrays.asList("one", null, "three"); try { Maps.toMap(strings, Functions.constant("foo")); fail(); } catch (NullPointerException expected) { } }
public void testToMapWithNullValues() { Iterable<String> strings = ImmutableList.of("one", "two", "three"); try { Maps.toMap(strings, Functions.constant(null)); fail(); } catch (NullPointerException expected) { } }
public void testCompositionWildcard() { Map<String, Integer> mapJapaneseToInteger = Maps.newHashMap(); Function<String, Integer> japaneseToInteger = Functions.forMap(mapJapaneseToInteger); Function<Object, String> numberToSpanish = Functions.constant("Yo no se"); Function<String, String> japaneseToSpanish = Functions.compose(numberToSpanish, japaneseToInteger); }
public void testCompose_equals() { new EqualsTester() .addEqualityGroup( Suppliers.compose(Functions.constant(1), Suppliers.ofInstance("foo")), Suppliers.compose(Functions.constant(1), Suppliers.ofInstance("foo"))) .addEqualityGroup(Suppliers.compose(Functions.constant(2), Suppliers.ofInstance("foo"))) .addEqualityGroup(Suppliers.compose(Functions.constant(1), Suppliers.ofInstance("bar"))) .testEquals(); } }
public void testCatching_rejectionPropagatesToOutput() throws Exception { SettableFuture<String> input = SettableFuture.create(); ListenableFuture<String> transformed = catching(input, Throwable.class, constant("foo"), REJECTING_EXECUTOR); input.setException(new Exception()); try { getDone(transformed); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); } }
@GwtIncompatible // SerializableTester public void testConstantSerializable() { checkCanReserialize(Functions.constant(5)); }
public void testToMap_nullValue() { try { fluent(1, 2, 3).toMap(Functions.constant(null)); fail(); } catch (NullPointerException expected) { } }
public void testIndex_nullKey() { try { ImmutableListMultimap<Object, Integer> unused = fluent(1, 2, 3).index(Functions.constant(null)); fail(); } catch (NullPointerException expected) { } }
public void testUniqueIndex_nullKey() { try { fluent(1, 2, 3).uniqueIndex(Functions.constant(null)); fail(); } catch (NullPointerException expected) { } }
public void testIndex_nullValue() { try { ImmutableListMultimap<String, Integer> unused = fluent(1, null, 2).index(Functions.constant("foo")); fail(); } catch (NullPointerException expected) { } }
public void testToMap_nullKey() { try { fluent(1, null, 2).toMap(Functions.constant("foo")); fail(); } catch (NullPointerException expected) { } }
public void testComposeOfFunctionsIsAssociative() { Map<Float, String> m = ImmutableMap.of(4.0f, "A", 3.0f, "B", 2.0f, "C", 1.0f, "D"); Function<? super Integer, Boolean> h = Functions.constant(Boolean.TRUE); Function<? super String, Integer> g = new HashCodeFunction(); Function<Float, String> f = Functions.forMap(m, "F"); Function<Float, Boolean> c1 = Functions.compose(Functions.compose(h, g), f); Function<Float, Boolean> c2 = Functions.compose(h, Functions.compose(g, f)); // Might be nice (eventually) to have: // assertEquals(c1, c2); // But for now, settle for this: assertEquals(c1.hashCode(), c2.hashCode()); assertEquals(c1.apply(1.0f), c2.apply(1.0f)); assertEquals(c1.apply(5.0f), c2.apply(5.0f)); }
private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) { Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); }
@GwtIncompatible // lazyTransform public void testLazyTransform() throws Exception { FunctionSpy<Object, String> spy = new FunctionSpy<>(constant("bar")); Future<String> input = immediateFuture("foo"); Future<String> transformed = lazyTransform(input, spy); spy.verifyCallCount(0); assertEquals("bar", getDone(transformed)); spy.verifyCallCount(1); assertEquals("bar", getDone(transformed)); spy.verifyCallCount(2); }