-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathQueryTest.java
146 lines (142 loc) · 5.18 KB
/
QueryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.lambda.query;
import com.spun.util.ObjectUtils;
import com.spun.util.StringUtils;
import org.approvaltests.Approvals;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.lambda.utils.Range;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class QueryTest
{
@Test
public void testQueryAll() throws Exception
{
List<Integer> numbers = Arrays.asList(3, 5, 7, 9);
assertEquals(true, Query.all(numbers, n -> 3 <= n));
assertEquals(false, Query.all(numbers, n -> 4 <= n));
}
@Test
public void testQueryAllOfNothing() throws Exception
{
// None of the cases return false for empty data sets
List<Integer> numbers = Arrays.asList();
assertEquals(true, Query.all(numbers, n -> 3 <= n));
}
@Test
void queryVsStreamExample()
{
{
// begin-snippet: query_example
Integer[] numbers = Range.get(1, 20);
Integer[] evenQueryNumbers = Query.where(numbers, n -> n % 2 == 0).orderBy(OrderBy.Order.Descending, n -> n)
.asArray();
// end-snippet
// begin-snippet: stream_example
Integer[] evenStreamNumbers = Arrays.stream(numbers).filter(n -> n % 2 == 0)
.sorted((o1, o2) -> o2.compareTo(o1)).toArray(Integer[]::new);
// end-snippet
Assertions.assertArrayEquals(evenQueryNumbers, evenStreamNumbers);
}
{
// begin-snippet: query_sum_example
String[] names = {"Llewellyn", "Scott"};
int lengthsFromQuery = Query.sum(names, n -> n.length()).intValue();
// end-snippet
// begin-snippet: stream_sum_example
int lengthsFromStream = (int) Arrays.stream(names).map(n -> n.length()).reduce(0, (a, b) -> a + b);
// end-snippet
Assertions.assertEquals(lengthsFromQuery, lengthsFromStream);
}
{
Integer[] numbers = Range.get(1, 20);
// begin-snippet: list_is_queryable
List<String> strings = Query.select(numbers, n -> "" + n);
// end-snippet
// begin-snippet: list_from_stream
List<String> strings2 = Arrays.stream(numbers).map(n -> "" + n).collect(Collectors.toList());
// end-snippet
}
}
@Test
void testTakeAndSkip()
{
Integer[] integers = Range.get(1, 10);
Integer[] skip = Query.skip(integers, 3).asArray();
Approvals.verify(Query.take(skip, 4));
}
@Test
void testSkipTooMany()
{
Integer[] integers = Range.get(1, 3);
List<Integer> list = Arrays.asList(integers);
assertEquals(Query.skip(integers, 3).size(), 0);
assertEquals(Query.skip(integers, 4).size(), 0);
integers = null;
assertEquals(Query.skip(integers, 3).size(), 0);
assertEquals(Query.skip(list, 3).size(), 0);
assertEquals(Query.skip(list, 4).size(), 0);
list = null;
assertEquals(Query.skip(list, 3).size(), 0);
}
@Test
void testTakeTooMany()
{
Integer[] integers = Range.get(1, 3);
List<Integer> list = Arrays.asList(integers);
assertEquals(Query.take(integers, 4).size(), 3);
integers = null;
assertEquals(Query.take(integers, 3).size(), 0);
assertEquals(Query.take(list, 4).size(), 3);
list = null;
assertEquals(Query.take(list, 3).size(), 0);
}
@Test
void testMax()
{
Integer[] integers = Range.get(1, 3);
int max = Query.max(Arrays.asList(integers), (i) -> i % 3 * 10);
assertEquals(2, max);
max = Query.max(integers, (i) -> i % 3 * 10);
assertEquals(2, max);
}
@Test
void testArrayAndListParity()
{
Queryable<Method> declaredMethods = Queryable.as(Query.class.getDeclaredMethods())
.where(m -> !m.getName().startsWith("lambda$"));
Queryable<Method> arrays = declaredMethods
.where(m -> m.getParameterTypes().length >= 1 && m.getParameterTypes()[0].isArray());
Queryable<Method> iterables = declaredMethods.where(m -> m.getParameterTypes().length >= 1
&& ObjectUtils.isThisInstanceOfThat(m.getParameterTypes()[0], Iterable.class));
arrays = arrays.where(m -> !hasMatchingMethod(m, declaredMethods, true));
iterables = iterables.where(m -> !hasMatchingMethod(m, declaredMethods, false));
Queryable<String> missingMethods = arrays.combine(iterables).select(this::printMethod).orderBy(m -> m);
Approvals.verifyAll("Methods without a corresponding array or list", missingMethods, m -> m);
}
private boolean hasMatchingMethod(Method method, Queryable<Method> declaredMethods, boolean findIterable)
{
Queryable<Method> where = declaredMethods.where(m -> m.getName().equals(method.getName()))
.where(m -> m.getParameterTypes().length == method.getParameterTypes().length);
if (findIterable)
{
return where.any(m -> ObjectUtils.isThisInstanceOfThat(m.getParameterTypes()[0], Iterable.class));
}
else
{
return where.any(m -> m.getParameterTypes()[0].isArray());
}
}
private String printMethod(Method m)
{
return String.format("%s.%s(%s)", m.getDeclaringClass().getSimpleName(), m.getName(), showParameters(m));
}
private String showParameters(Method m)
{
return StringUtils.join(Query.select(m.getParameters(), p -> String.format("%s", p.getType().getSimpleName())),
",");
}
}