From fe5c59c08f1975a45a826fc9dd162cc2d84bd02c Mon Sep 17 00:00:00 2001 From: Alexander Bandukwala Date: Tue, 20 Jun 2023 12:50:18 -0500 Subject: [PATCH 1/3] Add Koans for Map and Filter --- .../lambdakoans/builtin/fn2/AboutFilter.java | 101 ++++++++++++++++++ .../lambdakoans/builtin/fn2/AboutMap.java | 68 ++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java create mode 100644 src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java diff --git a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java new file mode 100644 index 0000000..d66338d --- /dev/null +++ b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java @@ -0,0 +1,101 @@ +package com.jnape.palatable.lambdakoans.builtin.fn2; + +import com.jnape.palatable.lambda.functions.Fn1; +import com.jnape.palatable.lambdakoans.Koans; +import org.junit.Test; + +import java.util.List; + +import static com.jnape.palatable.lambda.functions.builtin.fn2.Filter.filter; +import static com.jnape.palatable.lambdakoans.Koans.__; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; + +public class AboutFilter { + @Test + public void filterDoesWhatForEmptyList() { + Fn1 nonEmptyString = s -> !s.isEmpty(); + + Iterable strings = emptyList(); + Iterable filteredStrings = filter(nonEmptyString, strings); + + assertThat(filteredStrings, contains(Koans.__("Replace with whatever strings you expect to be in filteredStrings"))); + } + + + @Test + public void filteredElementsStayIn() { + Fn1 nonEmptyString = s -> !s.isEmpty(); + + Iterable strings = asList("", "Hello", "", "World", "!"); + Iterable filteredStrings = filter(nonEmptyString, strings); + + assertThat(filteredStrings, contains(Koans.__("Replace with whatever strings you expect to be in filteredStrings"))); + } + + @Test + public void removesLongStrings() { + Fn1 isShortString = s -> s.length() <= 5; + + Iterable strings = asList("Hello", "World!", "Functional", "Code"); + Iterable filteredStrings = __(); + + assertThat(filteredStrings, contains("Hello", "Code")); + } + + @Test + public void filtersWithAlwaysTrue() { + Fn1 alwaysTrue = s -> true; // Same as constantly(true) + + Iterable ints = asList(1, 2, 3, 4, 5); + Iterable filteredInts = filter(alwaysTrue, ints); + + assertThat(filteredInts, contains(Koans.__("Replace with whatever strings you expect to be in filteredInts"))); + } + + @Test + public void filtersWithAlwaysFalse() { + Fn1 alwaysFalse = s -> false; // Same as constantly(false) + + Iterable ints = asList(1, 2, 3, 4, 5); + Iterable filteredInts = filter(alwaysFalse, ints); + + assertThat(filteredInts, contains(Koans.__("Replace with whatever strings you expect to be in filteredInts"))); + } + + @Test + public void removeOdds() { + Fn1 fn = __(); + + Iterable ints = asList(1, 2, 3, 4, 5, 6); + Iterable filteredInts = filter(fn, ints); + + assertThat(filteredInts, contains(2, 4, 6)); + } + + @Test + public void keepEvenStringsThatArentEmpty() { + Fn1 isEven = __("Keep Even Strings"); + Fn1 isNotEmpty = __("Remove Empty Strings"); + + List strings = asList("", "a", "ab", "abc", "abcd", "abcde", "abcdef"); + Iterable filteredInts = filter(isNotEmpty, filter(isEven, strings)); + + assertThat(filteredInts, contains("ab", "abcd", "abcdef")); + } + + // Complete AboutMap first + @Test + public void combineMapAndFilter() { + Fn1 halve = i -> i / 2; + Fn1 isEven = i -> i % 2 == 0; + + Iterable ints = asList(1, 6, 9, 14, 11, 32); + Iterable halvedEvens = __("Use Filter and Map with the above functions to return the even numbers divided by 2"); + + assertThat(halvedEvens, contains(3, 7, 16)); + } + +} diff --git a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java new file mode 100644 index 0000000..ca9018d --- /dev/null +++ b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java @@ -0,0 +1,68 @@ +package com.jnape.palatable.lambdakoans.builtin.fn2; + +import com.jnape.palatable.lambda.functions.Fn1; +import org.junit.Test; + +import java.util.List; + +import static com.jnape.palatable.lambda.functions.builtin.fn2.Map.map; +import static com.jnape.palatable.lambdakoans.Koans.__; +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; + +public class AboutMap { + @Test + public void mapsValuesWithinAType() { + Fn1 increment = __(); + + Iterable list = asList(1, 2, 3, 4, 5); + Iterable incremented = map(increment, list); + + assertThat(incremented, contains(2, 3, 4, 5, 6)); + } + + @Test + public void mapsToADifferentType() { + Fn1 halve = __(); + + Iterable list = asList(1, 2, 3, 4, 5); + Iterable halved = map(halve, list); + + assertThat(halved, contains(0.5, 1.0, 1.5, 2.0, 2.5)); + } + + @Test + public void useMapToConvertTypes() { + Fn1 serialize = b -> b ? "True" : "False"; + + Iterable list = asList(true, false, false, true); + Iterable strings = __(); + + assertThat(strings, contains("True", "False", "False", "True")); + } + + @Test + public void mappingsCompose() { + Fn1 isEven = i -> i % 2 == 0; + Fn1 serialize = b -> b ? "Even" : "Odd"; + + List list = asList(1, 2, 3, 4, 5); + Iterable strings = __(); // Call map twice to map over both functions + + assertThat(strings, contains("Odd", "Even", "Odd", "Even", "Odd")); + } + + @Test + public void predictMappings() { + Fn1 stringLength = String::length; + Fn1 isEven = i -> i % 2 == 0; + Fn1 serialize = b -> b ? "Even" : "Odd"; + + Iterable strings = asList("first", "second", "third", "fourth", "fifth"); + Iterable newStrings = map(serialize, map(isEven, map(stringLength, strings))); + + assertThat(newStrings, contains(__(), __(), __(), __(), __())); + } + +} From 03618e3a50e3fa8391be80eb3b08d658d933882e Mon Sep 17 00:00:00 2001 From: Alexander Bandukwala Date: Wed, 21 Jun 2023 09:31:26 -0500 Subject: [PATCH 2/3] Fix filtersWithAlwaysFalse --- .../jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java index d66338d..4cb3ed5 100644 --- a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java +++ b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java @@ -2,6 +2,7 @@ import com.jnape.palatable.lambda.functions.Fn1; import com.jnape.palatable.lambdakoans.Koans; +import org.hamcrest.collection.IsIterableWithSize; import org.junit.Test; import java.util.List; @@ -12,6 +13,7 @@ import static java.util.Collections.emptyList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsIterableContainingInOrder.contains; +import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize; public class AboutFilter { @Test @@ -62,7 +64,7 @@ public void filtersWithAlwaysFalse() { Iterable ints = asList(1, 2, 3, 4, 5); Iterable filteredInts = filter(alwaysFalse, ints); - assertThat(filteredInts, contains(Koans.__("Replace with whatever strings you expect to be in filteredInts"))); + assertThat(filteredInts, iterableWithSize(__("Replace with the number of elements you expect filteredInts to be"))); } @Test From 3e70872879069a06ab9ac3b3724f0de09a756b22 Mon Sep 17 00:00:00 2001 From: Alexander Bandukwala Date: Wed, 21 Jun 2023 09:39:27 -0500 Subject: [PATCH 3/3] Replace contains matcher with iterates --- .../lambdakoans/builtin/fn2/AboutFilter.java | 19 +++++++++---------- .../lambdakoans/builtin/fn2/AboutMap.java | 16 ++++++++-------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java index 4cb3ed5..54c9895 100644 --- a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java +++ b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java @@ -12,8 +12,7 @@ import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.collection.IsIterableContainingInOrder.contains; -import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize; +import static testsupport.matchers.IterableMatcher.iterates; public class AboutFilter { @Test @@ -23,7 +22,7 @@ public void filterDoesWhatForEmptyList() { Iterable strings = emptyList(); Iterable filteredStrings = filter(nonEmptyString, strings); - assertThat(filteredStrings, contains(Koans.__("Replace with whatever strings you expect to be in filteredStrings"))); + assertThat(filteredStrings, iterates(Koans.__("Replace with whatever strings you expect to be in filteredStrings"))); } @@ -34,7 +33,7 @@ public void filteredElementsStayIn() { Iterable strings = asList("", "Hello", "", "World", "!"); Iterable filteredStrings = filter(nonEmptyString, strings); - assertThat(filteredStrings, contains(Koans.__("Replace with whatever strings you expect to be in filteredStrings"))); + assertThat(filteredStrings, iterates(Koans.__("Replace with whatever strings you expect to be in filteredStrings"))); } @Test @@ -44,7 +43,7 @@ public void removesLongStrings() { Iterable strings = asList("Hello", "World!", "Functional", "Code"); Iterable filteredStrings = __(); - assertThat(filteredStrings, contains("Hello", "Code")); + assertThat(filteredStrings, iterates("Hello", "Code")); } @Test @@ -54,7 +53,7 @@ public void filtersWithAlwaysTrue() { Iterable ints = asList(1, 2, 3, 4, 5); Iterable filteredInts = filter(alwaysTrue, ints); - assertThat(filteredInts, contains(Koans.__("Replace with whatever strings you expect to be in filteredInts"))); + assertThat(filteredInts, iterates(Koans.__("Replace with whatever strings you expect to be in filteredInts"))); } @Test @@ -64,7 +63,7 @@ public void filtersWithAlwaysFalse() { Iterable ints = asList(1, 2, 3, 4, 5); Iterable filteredInts = filter(alwaysFalse, ints); - assertThat(filteredInts, iterableWithSize(__("Replace with the number of elements you expect filteredInts to be"))); + assertThat(filteredInts, iterates(__("Replace with whatever strings you expect to be in filteredInts"))); } @Test @@ -74,7 +73,7 @@ public void removeOdds() { Iterable ints = asList(1, 2, 3, 4, 5, 6); Iterable filteredInts = filter(fn, ints); - assertThat(filteredInts, contains(2, 4, 6)); + assertThat(filteredInts, iterates(2, 4, 6)); } @Test @@ -85,7 +84,7 @@ public void keepEvenStringsThatArentEmpty() { List strings = asList("", "a", "ab", "abc", "abcd", "abcde", "abcdef"); Iterable filteredInts = filter(isNotEmpty, filter(isEven, strings)); - assertThat(filteredInts, contains("ab", "abcd", "abcdef")); + assertThat(filteredInts, iterates("ab", "abcd", "abcdef")); } // Complete AboutMap first @@ -97,7 +96,7 @@ public void combineMapAndFilter() { Iterable ints = asList(1, 6, 9, 14, 11, 32); Iterable halvedEvens = __("Use Filter and Map with the above functions to return the even numbers divided by 2"); - assertThat(halvedEvens, contains(3, 7, 16)); + assertThat(halvedEvens, iterates(3, 7, 16)); } } diff --git a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java index ca9018d..a5dc3fb 100644 --- a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java +++ b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java @@ -9,7 +9,7 @@ import static com.jnape.palatable.lambdakoans.Koans.__; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.collection.IsIterableContainingInOrder.contains; +import static testsupport.matchers.IterableMatcher.iterates; public class AboutMap { @Test @@ -19,7 +19,7 @@ public void mapsValuesWithinAType() { Iterable list = asList(1, 2, 3, 4, 5); Iterable incremented = map(increment, list); - assertThat(incremented, contains(2, 3, 4, 5, 6)); + assertThat(incremented, iterates(2, 3, 4, 5, 6)); } @Test @@ -29,7 +29,7 @@ public void mapsToADifferentType() { Iterable list = asList(1, 2, 3, 4, 5); Iterable halved = map(halve, list); - assertThat(halved, contains(0.5, 1.0, 1.5, 2.0, 2.5)); + assertThat(halved, iterates(0.5, 1.0, 1.5, 2.0, 2.5)); } @Test @@ -37,9 +37,9 @@ public void useMapToConvertTypes() { Fn1 serialize = b -> b ? "True" : "False"; Iterable list = asList(true, false, false, true); - Iterable strings = __(); + Iterable strings = __(); - assertThat(strings, contains("True", "False", "False", "True")); + assertThat(strings, iterates("True", "False", "False", "True")); } @Test @@ -48,9 +48,9 @@ public void mappingsCompose() { Fn1 serialize = b -> b ? "Even" : "Odd"; List list = asList(1, 2, 3, 4, 5); - Iterable strings = __(); // Call map twice to map over both functions + Iterable strings = __(); // Call map twice to map over both functions - assertThat(strings, contains("Odd", "Even", "Odd", "Even", "Odd")); + assertThat(strings, iterates("Odd", "Even", "Odd", "Even", "Odd")); } @Test @@ -62,7 +62,7 @@ public void predictMappings() { Iterable strings = asList("first", "second", "third", "fourth", "fifth"); Iterable newStrings = map(serialize, map(isEven, map(stringLength, strings))); - assertThat(newStrings, contains(__(), __(), __(), __(), __())); + assertThat(newStrings, iterates(__(), __(), __(), __(), __())); } }