You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/10/06 13:36:29 UTC
[groovy] 04/22: GROOVY-8258: test `stream` method and tweak tests
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY-8258
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit ffde5337d791a27aab75fe118cf5816691722e6c
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Oct 5 01:02:27 2020 +0800
GROOVY-8258: test `stream` method and tweak tests
---
.../linq/provider/QueryableCollectionTest.groovy | 104 +++++++++++----------
1 file changed, 54 insertions(+), 50 deletions(-)
diff --git a/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/provider/QueryableCollectionTest.groovy b/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/provider/QueryableCollectionTest.groovy
index be20ea6..344ffa4 100644
--- a/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/provider/QueryableCollectionTest.groovy
+++ b/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/provider/QueryableCollectionTest.groovy
@@ -26,21 +26,24 @@ import groovy.transform.ToString
import org.apache.groovy.linq.Queryable
import org.junit.Test
+import java.util.stream.Collectors
import java.util.stream.Stream
+import static org.apache.groovy.linq.provider.QueryableCollection.from
+
@CompileStatic
class QueryableCollectionTest {
@Test
void testFrom() {
- assert [1, 2, 3] == QueryableCollection.from(Stream.of(1, 2, 3)).toList()
- assert [1, 2, 3] == QueryableCollection.from(Arrays.asList(1, 2, 3)).toList()
+ assert [1, 2, 3] == from(Stream.of(1, 2, 3)).toList()
+ assert [1, 2, 3] == from(Arrays.asList(1, 2, 3)).toList()
}
@Test
void testInnerJoin0() {
def nums1 = [1, 2, 3]
def nums2 = [1, 2, 3]
- def result = QueryableCollection.from(nums1).innerJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).innerJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, 1], [2, 2], [3, 3]] == result
}
@@ -48,7 +51,7 @@ class QueryableCollectionTest {
void testInnerJoin1() {
def nums1 = [1, 2, 3]
def nums2 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).innerJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).innerJoin(from(nums2), (a, b) -> a == b).toList()
assert [[2, 2], [3, 3]] == result
}
@@ -56,7 +59,7 @@ class QueryableCollectionTest {
void testLeftJoin0() {
def nums1 = [1, 2, 3]
def nums2 = [1, 2, 3]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, 1], [2, 2], [3, 3]] == result
}
@@ -64,7 +67,7 @@ class QueryableCollectionTest {
void testRightJoin0() {
def nums2 = [1, 2, 3]
def nums1 = [1, 2, 3]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, 1], [2, 2], [3, 3]] == result
}
@@ -72,7 +75,7 @@ class QueryableCollectionTest {
void testLeftJoin1() {
def nums1 = [1, 2, 3]
def nums2 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3]] == result
}
@@ -80,7 +83,7 @@ class QueryableCollectionTest {
void testRightJoin1() {
def nums2 = [1, 2, 3]
def nums1 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3]] == result
}
@@ -88,7 +91,7 @@ class QueryableCollectionTest {
void testLeftJoin2() {
def nums1 = [1, 2, 3, null]
def nums2 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3], [null, null]] == result
}
@@ -96,7 +99,7 @@ class QueryableCollectionTest {
void testRightJoin2() {
def nums2 = [1, 2, 3, null]
def nums1 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3], [null, null]] == result
}
@@ -104,7 +107,7 @@ class QueryableCollectionTest {
void testLeftJoin3() {
def nums1 = [1, 2, 3, null]
def nums2 = [2, 3, 4, null]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3], [null, null]] == result
}
@@ -112,7 +115,7 @@ class QueryableCollectionTest {
void testRightJoin3() {
def nums2 = [1, 2, 3, null]
def nums1 = [2, 3, 4, null]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3], [null, null]] == result
}
@@ -120,7 +123,7 @@ class QueryableCollectionTest {
void testLeftJoin4() {
def nums1 = [1, 2, 3]
def nums2 = [2, 3, 4, null]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3]] == result
}
@@ -128,7 +131,7 @@ class QueryableCollectionTest {
void testRightJoin4() {
def nums2 = [1, 2, 3]
def nums1 = [2, 3, 4, null]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3]] == result
}
@@ -136,7 +139,7 @@ class QueryableCollectionTest {
void testLeftJoin5() {
def nums1 = [1, 2, 3, null, null]
def nums2 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3], [null, null], [null, null]] == result
}
@@ -144,7 +147,7 @@ class QueryableCollectionTest {
void testRightJoin5() {
def nums2 = [1, 2, 3, null, null]
def nums1 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3], [null, null], [null, null]] == result
}
@@ -152,7 +155,7 @@ class QueryableCollectionTest {
void testLeftJoin6() {
def nums1 = [1, 2, 3, null, null]
def nums2 = [2, 3, 4, null]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3], [null, null], [null, null]] == result
}
@@ -160,7 +163,7 @@ class QueryableCollectionTest {
void testRightJoin6() {
def nums2 = [1, 2, 3, null, null]
def nums1 = [2, 3, 4, null]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3], [null, null], [null, null]] == result
}
@@ -168,7 +171,7 @@ class QueryableCollectionTest {
void testLeftJoin7() {
def nums1 = [1, 2, 3, null, null]
def nums2 = [2, 3, 4, null, null]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3], [null, null], [null, null]] == result
}
@@ -176,7 +179,7 @@ class QueryableCollectionTest {
void testRightJoin7() {
def nums2 = [1, 2, 3, null, null]
def nums1 = [2, 3, 4, null, null]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3], [null, null], [null, null]] == result
}
@@ -184,7 +187,7 @@ class QueryableCollectionTest {
void testLeftJoin8() {
def nums1 = [1, 2, 3, null]
def nums2 = [2, 3, 4, null, null]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3], [null, null]] == result
}
@@ -192,7 +195,7 @@ class QueryableCollectionTest {
void testRightJoin8() {
def nums2 = [1, 2, 3, null]
def nums1 = [2, 3, 4, null, null]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3], [null, null]] == result
}
@@ -200,7 +203,7 @@ class QueryableCollectionTest {
void testLeftJoin9() {
def nums1 = [1, 2, 3]
def nums2 = [2, 3, 4, null, null]
- def result = QueryableCollection.from(nums1).leftJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).leftJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3]] == result
}
@@ -208,7 +211,7 @@ class QueryableCollectionTest {
void testRightJoin9() {
def nums2 = [1, 2, 3]
def nums1 = [2, 3, 4, null, null]
- def result = QueryableCollection.from(nums1).rightJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).rightJoin(from(nums2), (a, b) -> a == b).toList()
assert [[null, 1], [2, 2], [3, 3]] == result
}
@@ -216,7 +219,7 @@ class QueryableCollectionTest {
void testFullJoin() {
def nums1 = [1, 2, 3]
def nums2 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).fullJoin(QueryableCollection.from(nums2), (a, b) -> a == b).toList()
+ def result = from(nums1).fullJoin(from(nums2), (a, b) -> a == b).toList()
assert [[1, null], [2, 2], [3, 3], [null, 4]] == result
}
@@ -224,28 +227,28 @@ class QueryableCollectionTest {
void testCrossJoin() {
def nums1 = [1, 2, 3]
def nums2 = [3, 4, 5]
- def result = QueryableCollection.from(nums1).crossJoin(QueryableCollection.from(nums2)).toList()
+ def result = from(nums1).crossJoin(from(nums2)).toList()
assert [[1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 3], [3, 4], [3, 5]] == result
}
@Test
void testWhere() {
def nums = [1, 2, 3, 4, 5]
- def result = QueryableCollection.from(nums).where(e -> e > 3).toList()
+ def result = from(nums).where(e -> e > 3).toList()
assert [4, 5] == result
}
@Test
void testGroupBySelect0() {
def nums = [1, 2, 2, 3, 3, 4, 4, 5]
- def result = QueryableCollection.from(nums).groupBy(e -> e).select(e -> Tuple.tuple(e.v1, e.v2.toList())).toList()
+ def result = from(nums).groupBy(e -> e).select(e -> Tuple.tuple(e.v1, e.v2.toList())).toList()
assert [[1, [1]], [2, [2, 2]], [3, [3, 3]], [4, [4, 4]], [5, [5]]] == result
}
@Test
void testGroupBySelect1() {
def nums = [1, 2, 2, 3, 3, 4, 4, 5]
- def result = QueryableCollection.from(nums).groupBy(e -> e).select(e -> Tuple.tuple(e.v1, e.v2.count())).toList()
+ def result = from(nums).groupBy(e -> e).select(e -> Tuple.tuple(e.v1, e.v2.count())).toList()
assert [[1, 1], [2, 2], [3, 2], [4, 2], [5, 1]] == result
}
@@ -253,7 +256,7 @@ class QueryableCollectionTest {
void testGroupBySelect2() {
def nums = [1, 2, 2, 3, 3, 4, 4, 5]
def result =
- QueryableCollection.from(nums).groupBy(e -> e)
+ from(nums).groupBy(e -> e)
.select(e ->
Tuple.tuple(
e.v1,
@@ -269,7 +272,7 @@ class QueryableCollectionTest {
void testGroupBySelect3() {
def nums = [1, 2, 2, 3, 3, 4, 4, 5]
def result =
- QueryableCollection.from(nums).groupBy(e -> e, (k, q) -> k > 2)
+ from(nums).groupBy(e -> e, (k, q) -> k > 2)
.select(e ->
Tuple.tuple(
e.v1,
@@ -288,25 +291,25 @@ class QueryableCollectionTest {
Person john = new Person('John', 10)
def persons = [daniel, peter, alice, john]
- def result = QueryableCollection.from(persons).orderBy(
+ def result = from(persons).orderBy(
new Queryable.Order<Person, Comparable>((Person e) -> e.age, true),
new Queryable.Order<Person, Comparable>((Person e) -> e.name, true)
).toList()
assert [john, peter, alice, daniel] == result
- result = QueryableCollection.from(persons).orderBy(
+ result = from(persons).orderBy(
new Queryable.Order<Person, Comparable>((Person e) -> e.age, false),
new Queryable.Order<Person, Comparable>((Person e) -> e.name, true)
).toList()
assert [daniel, alice, john, peter] == result
- result = QueryableCollection.from(persons).orderBy(
+ result = from(persons).orderBy(
new Queryable.Order<Person, Comparable>((Person e) -> e.age, true),
new Queryable.Order<Person, Comparable>((Person e) -> e.name, false)
).toList()
assert [peter, john, alice, daniel] == result
- result = QueryableCollection.from(persons).orderBy(
+ result = from(persons).orderBy(
new Queryable.Order<Person, Comparable>((Person e) -> e.age, false),
new Queryable.Order<Person, Comparable>((Person e) -> e.name, false)
).toList()
@@ -316,24 +319,24 @@ class QueryableCollectionTest {
@Test
void testLimit() {
def nums = [1, 2, 3, 4, 5]
- def result = QueryableCollection.from(nums).limit(1, 2).toList()
+ def result = from(nums).limit(1, 2).toList()
assert [2, 3] == result
- result = QueryableCollection.from(nums).limit(2).toList()
+ result = from(nums).limit(2).toList()
assert [1, 2] == result
}
@Test
void testSelect() {
def nums = [1, 2, 3, 4, 5]
- def result = QueryableCollection.from(nums).select(e -> e + 1).toList()
+ def result = from(nums).select(e -> e + 1).toList()
assert [2, 3, 4, 5, 6] == result
}
@Test
void testDistinct() {
def nums = [1, 2, 2, 3, 3, 2, 3, 4, 5, 5]
- def result = QueryableCollection.from(nums).distinct().toList()
+ def result = from(nums).distinct().toList()
assert [1, 2, 3, 4, 5] == result
}
@@ -341,7 +344,7 @@ class QueryableCollectionTest {
void testUnion() {
def nums1 = [1, 2, 3]
def nums2 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).union(QueryableCollection.from(nums2)).toList()
+ def result = from(nums1).union(from(nums2)).toList()
assert [1, 2, 3, 4] == result
}
@@ -349,7 +352,7 @@ class QueryableCollectionTest {
void testUnionAll() {
def nums1 = [1, 2, 3]
def nums2 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).unionAll(QueryableCollection.from(nums2)).toList()
+ def result = from(nums1).unionAll(from(nums2)).toList()
assert [1, 2, 3, 2, 3, 4] == result
}
@@ -357,7 +360,7 @@ class QueryableCollectionTest {
void testIntersect() {
def nums1 = [1, 2, 2, 3]
def nums2 = [2, 3, 3, 4]
- def result = QueryableCollection.from(nums1).intersect(QueryableCollection.from(nums2)).toList()
+ def result = from(nums1).intersect(from(nums2)).toList()
assert [2, 3] == result
}
@@ -365,7 +368,7 @@ class QueryableCollectionTest {
void testMinus() {
def nums1 = [1, 1, 2, 3]
def nums2 = [2, 3, 4]
- def result = QueryableCollection.from(nums1).minus(QueryableCollection.from(nums2)).toList()
+ def result = from(nums1).minus(from(nums2)).toList()
assert [1] == result
}
@@ -374,8 +377,8 @@ class QueryableCollectionTest {
def nums1 = [1, 2, 3, 4, 5]
def nums2 = [0, 1, 2, 3, 4, 5, 6]
def result =
- QueryableCollection.from(nums1)
- .innerJoin(QueryableCollection.from(nums2), (a, b) -> a == b)
+ from(nums1)
+ .innerJoin(from(nums2), (a, b) -> a == b)
.where(t -> t.v1 > 1)
.limit(1, 2)
.select(t -> t.v1 + 1)
@@ -383,11 +386,12 @@ class QueryableCollectionTest {
assert [4, 5] == result
}
-// @Test void testIterator() {
-// def nums = [1, 2, 3]
-// def result = from(nums).iterator().toList()
-// assert nums == result
-// }
+ @Test
+ void testStream() {
+ def nums = [1, 2, 3]
+ def result = from(nums).stream().collect(Collectors.toList())
+ assert nums == result
+ }
@ToString
@EqualsAndHashCode