2323
2424public class GuavaFilterTransformCollectionsTest {
2525
26+ @ Test
27+ public void whenFilterWithIterables_thenFiltered () {
28+ final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
29+ final Iterable <String > result = Iterables .filter (names , Predicates .containsPattern ("a" ));
30+
31+ assertThat (result , containsInAnyOrder ("Jane" , "Adam" ));
32+ }
33+
2634 @ Test
2735 public void whenFilterWithCollections2_thenFiltered () {
2836 final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
@@ -33,18 +41,18 @@ public void whenFilterWithCollections2_thenFiltered() {
3341
3442 result .add ("anna" );
3543 assertEquals (5 , names .size ());
36- result .remove ("Adam" );
37- assertEquals (4 , names .size ());
3844 }
3945
40- @ Test
41- public void whenFilterWithIterables_thenFiltered () {
46+ @ Test ( expected = IllegalArgumentException . class )
47+ public void givenFilteredCollection_whenAddingInvalidElement_thenException () {
4248 final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
43- final Iterable <String > result = Iterables .filter (names , Predicates .containsPattern ("a" ));
49+ final Collection <String > result = Collections2 .filter (names , Predicates .containsPattern ("a" ));
4450
45- assertThat ( result , containsInAnyOrder ( "Jane" , "Adam" ) );
51+ result . add ( "elvis" );
4652 }
4753
54+ //
55+
4856 @ Test
4957 public void whenFilterCollectionWithCustomPredicate_thenFiltered () {
5058 final Predicate <String > predicate = new Predicate <String >() {
@@ -61,14 +69,7 @@ public final boolean apply(final String input) {
6169 assertThat (result , containsInAnyOrder ("John" , "Jane" , "Adam" ));
6270 }
6371
64- @ Test
65- public void whenFilterUsingMultiplePredicates_thenFiltered () {
66- final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
67- final Collection <String > result = Collections2 .filter (names , Predicates .or (Predicates .containsPattern ("J" ), Predicates .not (Predicates .containsPattern ("a" ))));
68-
69- assertEquals (3 , result .size ());
70- assertThat (result , containsInAnyOrder ("John" , "Jane" , "Tom" ));
71- }
72+ //
7273
7374 @ Test
7475 public void whenRemoveNullFromCollection_thenRemoved () {
@@ -79,8 +80,10 @@ public void whenRemoveNullFromCollection_thenRemoved() {
7980 assertThat (result , containsInAnyOrder ("John" , "Jane" , "Adam" , "Tom" ));
8081 }
8182
83+ //
84+
8285 @ Test
83- public void whenCheckAllElementsForCondition_thenChecked () {
86+ public void whenCheckingIfAllElementsMatchACondition_thenCorrect () {
8487 final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
8588
8689 boolean result = Iterables .all (names , Predicates .containsPattern ("n|m" ));
@@ -90,8 +93,10 @@ public void whenCheckAllElementsForCondition_thenChecked() {
9093 assertFalse (result );
9194 }
9295
96+ //
97+
9398 @ Test
94- public void whenTransformWithIterables_thenTransformed () {
99+ public void whenTransformingWithIterables_thenTransformed () {
95100 final Function <String , Integer > function = new Function <String , Integer >() {
96101 @ Override
97102 public final Integer apply (final String input ) {
@@ -105,6 +110,8 @@ public final Integer apply(final String input) {
105110 assertThat (result , contains (4 , 4 , 4 , 3 ));
106111 }
107112
113+ //
114+
108115 @ Test
109116 public void whenTransformWithCollections2_thenTransformed () {
110117 final Function <String , Integer > function = new Function <String , Integer >() {
@@ -124,8 +131,21 @@ public final Integer apply(final String input) {
124131 assertEquals (3 , names .size ());
125132 }
126133
134+ //
135+
136+ @ Test
137+ public void whenCreatingAFunctionFromAPredicate_thenCorrect () {
138+ final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
139+ final Collection <Boolean > result = Collections2 .transform (names , Functions .forPredicate (Predicates .containsPattern ("m" )));
140+
141+ assertEquals (4 , result .size ());
142+ assertThat (result , contains (false , false , true , true ));
143+ }
144+
145+ //
146+
127147 @ Test
128- public void whenTransformUsingComposedFunction_thenTransformed () {
148+ public void whenTransformingUsingComposedFunction_thenTransformed () {
129149 final Function <String , Integer > f1 = new Function <String , Integer >() {
130150 @ Override
131151 public final Integer apply (final String input ) {
@@ -147,17 +167,10 @@ public final Boolean apply(final Integer input) {
147167 assertThat (result , contains (true , true , true , false ));
148168 }
149169
150- @ Test
151- public void whenCreateFunctionFromPredicate_thenCreated () {
152- final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
153- final Collection <Boolean > result = Collections2 .transform (names , Functions .forPredicate (Predicates .containsPattern ("m" )));
154-
155- assertEquals (4 , result .size ());
156- assertThat (result , contains (false , false , true , true ));
157- }
170+ //
158171
159172 @ Test
160- public void whenFilterAndTransformCollection_thenCorrect () {
173+ public void whenFilteringAndTransformingCollection_thenCorrect () {
161174 final Predicate <String > predicate = new Predicate <String >() {
162175 @ Override
163176 public final boolean apply (final String input ) {
@@ -179,4 +192,15 @@ public final Integer apply(final String input) {
179192 assertThat (result , containsInAnyOrder (4 , 3 ));
180193 }
181194
195+ //
196+
197+ @ Test
198+ public void whenFilterUsingMultiplePredicates_thenFiltered () {
199+ final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
200+ final Collection <String > result = Collections2 .filter (names , Predicates .or (Predicates .containsPattern ("J" ), Predicates .not (Predicates .containsPattern ("a" ))));
201+
202+ assertEquals (3 , result .size ());
203+ assertThat (result , containsInAnyOrder ("John" , "Jane" , "Tom" ));
204+ }
205+
182206}
0 commit comments