quava testing cleanup

This commit is contained in:
eugenp 2014-10-16 12:05:37 +03:00
parent 149bdeb11d
commit 5742f31052

View File

@ -23,6 +23,14 @@ import com.google.common.collect.Lists;
public class GuavaFilterTransformCollectionsTest { public class GuavaFilterTransformCollectionsTest {
@Test
public void whenFilterWithIterables_thenFiltered() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
final Iterable<String> result = Iterables.filter(names, Predicates.containsPattern("a"));
assertThat(result, containsInAnyOrder("Jane", "Adam"));
}
@Test @Test
public void whenFilterWithCollections2_thenFiltered() { public void whenFilterWithCollections2_thenFiltered() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
@ -33,18 +41,18 @@ public class GuavaFilterTransformCollectionsTest {
result.add("anna"); result.add("anna");
assertEquals(5, names.size()); assertEquals(5, names.size());
result.remove("Adam");
assertEquals(4, names.size());
} }
@Test @Test(expected = IllegalArgumentException.class)
public void whenFilterWithIterables_thenFiltered() { public void givenFilteredCollection_whenAddingInvalidElement_thenException() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
final Iterable<String> result = Iterables.filter(names, Predicates.containsPattern("a")); final Collection<String> result = Collections2.filter(names, Predicates.containsPattern("a"));
assertThat(result, containsInAnyOrder("Jane", "Adam")); result.add("elvis");
} }
//
@Test @Test
public void whenFilterCollectionWithCustomPredicate_thenFiltered() { public void whenFilterCollectionWithCustomPredicate_thenFiltered() {
final Predicate<String> predicate = new Predicate<String>() { final Predicate<String> predicate = new Predicate<String>() {
@ -61,14 +69,7 @@ public class GuavaFilterTransformCollectionsTest {
assertThat(result, containsInAnyOrder("John", "Jane", "Adam")); assertThat(result, containsInAnyOrder("John", "Jane", "Adam"));
} }
@Test //
public void whenFilterUsingMultiplePredicates_thenFiltered() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
final Collection<String> result = Collections2.filter(names, Predicates.or(Predicates.containsPattern("J"), Predicates.not(Predicates.containsPattern("a"))));
assertEquals(3, result.size());
assertThat(result, containsInAnyOrder("John", "Jane", "Tom"));
}
@Test @Test
public void whenRemoveNullFromCollection_thenRemoved() { public void whenRemoveNullFromCollection_thenRemoved() {
@ -79,8 +80,10 @@ public class GuavaFilterTransformCollectionsTest {
assertThat(result, containsInAnyOrder("John", "Jane", "Adam", "Tom")); assertThat(result, containsInAnyOrder("John", "Jane", "Adam", "Tom"));
} }
//
@Test @Test
public void whenCheckAllElementsForCondition_thenChecked() { public void whenCheckingIfAllElementsMatchACondition_thenCorrect() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
boolean result = Iterables.all(names, Predicates.containsPattern("n|m")); boolean result = Iterables.all(names, Predicates.containsPattern("n|m"));
@ -90,8 +93,10 @@ public class GuavaFilterTransformCollectionsTest {
assertFalse(result); assertFalse(result);
} }
//
@Test @Test
public void whenTransformWithIterables_thenTransformed() { public void whenTransformingWithIterables_thenTransformed() {
final Function<String, Integer> function = new Function<String, Integer>() { final Function<String, Integer> function = new Function<String, Integer>() {
@Override @Override
public final Integer apply(final String input) { public final Integer apply(final String input) {
@ -105,6 +110,8 @@ public class GuavaFilterTransformCollectionsTest {
assertThat(result, contains(4, 4, 4, 3)); assertThat(result, contains(4, 4, 4, 3));
} }
//
@Test @Test
public void whenTransformWithCollections2_thenTransformed() { public void whenTransformWithCollections2_thenTransformed() {
final Function<String, Integer> function = new Function<String, Integer>() { final Function<String, Integer> function = new Function<String, Integer>() {
@ -124,8 +131,21 @@ public class GuavaFilterTransformCollectionsTest {
assertEquals(3, names.size()); assertEquals(3, names.size());
} }
//
@Test @Test
public void whenTransformUsingComposedFunction_thenTransformed() { public void whenCreatingAFunctionFromAPredicate_thenCorrect() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
final Collection<Boolean> result = Collections2.transform(names, Functions.forPredicate(Predicates.containsPattern("m")));
assertEquals(4, result.size());
assertThat(result, contains(false, false, true, true));
}
//
@Test
public void whenTransformingUsingComposedFunction_thenTransformed() {
final Function<String, Integer> f1 = new Function<String, Integer>() { final Function<String, Integer> f1 = new Function<String, Integer>() {
@Override @Override
public final Integer apply(final String input) { public final Integer apply(final String input) {
@ -147,17 +167,10 @@ public class GuavaFilterTransformCollectionsTest {
assertThat(result, contains(true, true, true, false)); assertThat(result, contains(true, true, true, false));
} }
@Test //
public void whenCreateFunctionFromPredicate_thenCreated() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
final Collection<Boolean> result = Collections2.transform(names, Functions.forPredicate(Predicates.containsPattern("m")));
assertEquals(4, result.size());
assertThat(result, contains(false, false, true, true));
}
@Test @Test
public void whenFilterAndTransformCollection_thenCorrect() { public void whenFilteringAndTransformingCollection_thenCorrect() {
final Predicate<String> predicate = new Predicate<String>() { final Predicate<String> predicate = new Predicate<String>() {
@Override @Override
public final boolean apply(final String input) { public final boolean apply(final String input) {
@ -179,4 +192,15 @@ public class GuavaFilterTransformCollectionsTest {
assertThat(result, containsInAnyOrder(4, 3)); assertThat(result, containsInAnyOrder(4, 3));
} }
//
@Test
public void whenFilterUsingMultiplePredicates_thenFiltered() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
final Collection<String> result = Collections2.filter(names, Predicates.or(Predicates.containsPattern("J"), Predicates.not(Predicates.containsPattern("a"))));
assertEquals(3, result.size());
assertThat(result, containsInAnyOrder("John", "Jane", "Tom"));
}
} }