@GwtIncompatible // SerializableTester private static <Y> void checkCanReserialize(Function<? super Integer, Y> f) { Function<? super Integer, Y> g = SerializableTester.reserializeAndAssert(f); for (int i = 1; i < 5; i++) { // convoluted way to check that the same result happens from each Y expected = null; try { expected = f.apply(i); } catch (IllegalArgumentException e) { try { g.apply(i); fail(); } catch (IllegalArgumentException ok) { continue; } } assertEquals(expected, g.apply(i)); } }
public void testForMapWithoutDefault() { Map<String, Integer> map = Maps.newHashMap(); map.put("One", 1); map.put("Three", 3); map.put("Null", null); Function<String, Integer> function = Functions.forMap(map); assertEquals(1, function.apply("One").intValue()); assertEquals(3, function.apply("Three").intValue()); assertNull(function.apply("Null")); try { function.apply("Two"); fail(); } catch (IllegalArgumentException expected) { } new EqualsTester() .addEqualityGroup(function, Functions.forMap(map)) .addEqualityGroup(Functions.forMap(map, 42)) .testEquals(); }
public void testToStringFunction_apply() { assertEquals("3", Functions.toStringFunction().apply(3)); assertEquals("hiya", Functions.toStringFunction().apply("hiya")); assertEquals( "I'm a string", Functions.toStringFunction() .apply( new Object() { @Override public String toString() { return "I'm a string"; } })); try { Functions.toStringFunction().apply(null); fail("expected NullPointerException"); } catch (NullPointerException expected) { } }
@GwtIncompatible // SerializableTester private static <Y> void checkCanReserialize(Function<? super Integer, Y> f) { Function<? super Integer, Y> g = SerializableTester.reserializeAndAssert(f); for (int i = 1; i < 5; i++) { // convoluted way to check that the same result happens from each Y expected = null; try { expected = f.apply(i); } catch (IllegalArgumentException e) { try { g.apply(i); fail(); } catch (IllegalArgumentException ok) { continue; } } assertEquals(expected, g.apply(i)); } }
public void testToStringFunction_apply() { assertEquals("3", Functions.toStringFunction().apply(3)); assertEquals("hiya", Functions.toStringFunction().apply("hiya")); assertEquals("I'm a string", Functions.toStringFunction().apply( new Object() { @Override public String toString() { return "I'm a string"; } })); try { Functions.toStringFunction().apply(null); fail("expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testForMapWithoutDefault() { Map<String, Integer> map = Maps.newHashMap(); map.put("One", 1); map.put("Three", 3); map.put("Null", null); Function<String, Integer> function = Functions.forMap(map); assertEquals(1, function.apply("One").intValue()); assertEquals(3, function.apply("Three").intValue()); assertNull(function.apply("Null")); try { function.apply("Two"); fail(); } catch (IllegalArgumentException expected) { } new EqualsTester() .addEqualityGroup(function, Functions.forMap(map)) .addEqualityGroup(Functions.forMap(map, 42)) .testEquals(); }