private static void assertEvalsToTrue(Predicate<? super Integer> predicate) { assertTrue(predicate.apply(0)); assertTrue(predicate.apply(1)); assertTrue(predicate.apply(null)); }
public void testAnd_listDefensivelyCopied() { List<Predicate<Object>> list = newArrayList(); Predicate<Object> predicate = Predicates.and(list); assertTrue(predicate.apply(1)); list.add(Predicates.alwaysFalse()); assertTrue(predicate.apply(1)); }
public void testAnd_iterableDefensivelyCopied() { final List<Predicate<Object>> list = newArrayList(); Iterable<Predicate<Object>> iterable = new Iterable<Predicate<Object>>() { @Override public Iterator<Predicate<Object>> iterator() { return list.iterator(); } }; Predicate<Object> predicate = Predicates.and(iterable); assertTrue(predicate.apply(1)); list.add(Predicates.alwaysFalse()); assertTrue(predicate.apply(1)); }
@GwtIncompatible // Predicates.subtypeOf public void testSubtypeOf_subclass() { Predicate<Class<?>> isNumber = Predicates.subtypeOf(Number.class); assertTrue(isNumber.apply(Integer.class)); assertTrue(isNumber.apply(Float.class)); }
public void testNotNull_apply() { Predicate<Integer> notNull = Predicates.notNull(); assertFalse(notNull.apply(null)); assertTrue(notNull.apply(1)); }
public void testIsEqualToNull_apply() { Predicate<Integer> isNull = Predicates.equalTo(null); assertTrue(isNull.apply(null)); assertFalse(isNull.apply(1)); }
@GwtIncompatible // Predicates.subtypeOf public void testSubtypeOf_interface() { Predicate<Class<?>> isComparable = Predicates.subtypeOf(Comparable.class); assertTrue(isComparable.apply(Integer.class)); assertTrue(isComparable.apply(Float.class)); }
@GwtIncompatible // Predicates.instanceOf public void testIsInstanceOf_interface() { Predicate<Object> isComparable = Predicates.instanceOf(Comparable.class); assertTrue(isComparable.apply(1)); assertTrue(isComparable.apply(2.0f)); assertTrue(isComparable.apply("")); assertFalse(isComparable.apply(null)); }
public void testIn_apply() { Collection<Integer> nums = Arrays.asList(1, 5); Predicate<Integer> isOneOrFive = Predicates.in(nums); assertTrue(isOneOrFive.apply(1)); assertTrue(isOneOrFive.apply(5)); assertFalse(isOneOrFive.apply(3)); assertFalse(isOneOrFive.apply(null)); }
public void testIsEqualTo_apply() { Predicate<Integer> isOne = Predicates.equalTo(1); assertTrue(isOne.apply(1)); assertFalse(isOne.apply(2)); assertFalse(isOne.apply(null)); }
@GwtIncompatible // Predicates.instanceOf public void testIsInstanceOf_subclass() { Predicate<Object> isNumber = Predicates.instanceOf(Number.class); assertTrue(isNumber.apply(1)); assertTrue(isNumber.apply(2.0f)); assertFalse(isNumber.apply("")); assertFalse(isNumber.apply(null)); }
public void testIsNull_apply() { Predicate<Integer> isNull = Predicates.isNull(); assertTrue(isNull.apply(null)); assertFalse(isNull.apply(1)); }
@GwtIncompatible // Predicates.containsPattern public void testContains_apply() { Predicate<CharSequence> isFoobar = Predicates.contains(Pattern.compile("^Fo.*o.*bar$")); assertTrue(isFoobar.apply("Foxyzoabcbar")); assertFalse(isFoobar.apply("Foobarx")); }
/** * Tests for Predicates.contains(Pattern) and .containsPattern(String). We assume the regex level * works, so there are only trivial tests of that aspect. TODO: Fix comment style once annotation * stripper is fixed. */ @GwtIncompatible // Predicates.containsPattern public void testContainsPattern_apply() { Predicate<CharSequence> isFoobar = Predicates.containsPattern("^Fo.*o.*bar$"); assertTrue(isFoobar.apply("Foxyzoabcbar")); assertFalse(isFoobar.apply("Foobarx")); }
@GwtIncompatible // Predicates.subtypeOf public void testSubtypeOf_apply() { Predicate<Class<?>> isInteger = Predicates.subtypeOf(Integer.class); assertTrue(isInteger.apply(Integer.class)); assertFalse(isInteger.apply(Float.class)); try { isInteger.apply(null); fail(); } catch (NullPointerException expected) { } }
/** * Tests for Predicates.instanceOf(x). TODO: Fix the comment style after fixing annotation * stripper to remove comments properly. Currently, all tests before the comments are removed as * well. */ @GwtIncompatible // Predicates.instanceOf public void testIsInstanceOf_apply() { Predicate<Object> isInteger = Predicates.instanceOf(Integer.class); assertTrue(isInteger.apply(1)); assertFalse(isInteger.apply(2.0f)); assertFalse(isInteger.apply("")); assertFalse(isInteger.apply(null)); }
public void testHashCodeForBooleanOperations() { Predicate<Integer> p1 = Predicates.isNull(); Predicate<Integer> p2 = isOdd(); // Make sure that hash codes are not computed per-instance. assertEqualHashCode(Predicates.not(p1), Predicates.not(p1)); assertEqualHashCode(Predicates.and(p1, p2), Predicates.and(p1, p2)); assertEqualHashCode(Predicates.or(p1, p2), Predicates.or(p1, p2)); // While not a contractual requirement, we'd like the hash codes for ands // & ors of the same predicates to not collide. assertTrue(Predicates.and(p1, p2).hashCode() != Predicates.or(p1, p2).hashCode()); }
public void testCompose() { Function<String, String> trim = TrimStringFunction.INSTANCE; Predicate<String> equalsFoo = Predicates.equalTo("Foo"); Predicate<String> equalsBar = Predicates.equalTo("Bar"); Predicate<String> trimEqualsFoo = Predicates.compose(equalsFoo, trim); Function<String, String> identity = Functions.identity(); assertTrue(trimEqualsFoo.apply("Foo")); assertTrue(trimEqualsFoo.apply(" Foo ")); assertFalse(trimEqualsFoo.apply("Foo-b-que")); new EqualsTester() .addEqualityGroup(trimEqualsFoo, Predicates.compose(equalsFoo, trim)) .addEqualityGroup(equalsFoo) .addEqualityGroup(trim) .addEqualityGroup(Predicates.compose(equalsFoo, identity)) .addEqualityGroup(Predicates.compose(equalsBar, trim)) .testEquals(); }
public void testAnd_listDefensivelyCopied() { List<Predicate<Object>> list = newArrayList(); Predicate<Object> predicate = Predicates.and(list); assertTrue(predicate.apply(1)); list.add(Predicates.alwaysFalse()); assertTrue(predicate.apply(1)); }
public void testNotNull_apply() { Predicate<Integer> notNull = Predicates.notNull(); assertFalse(notNull.apply(null)); assertTrue(notNull.apply(1)); }