-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandChainsTest.groovy
300 lines (279 loc) · 6.87 KB
/
CommandChainsTest.groovy
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
class CommandChainsTest extends GroovyTestCase {
void testCommandChainEquivalence() {
assertScript '''String left = 'left'
String right = 'right'
class Move {
def list = []
Move turn(direction) {
list << direction
this
}
Move then(direction) {
list << direction
this
}
}
def m1 = new Move()
def m2 = new Move()
m1.with {
// tag::commandchain_1[]
// equivalent to: turn(left).then(right)
turn left then right
// end::commandchain_1[]
}
m2.turn(left).then(right)
assert m1.list == ['left', 'right']
assert m1.list == m2.list
'''
assertScript '''import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
class DSL {
int qt
String medicine
int duration
def take(qty) {
qt = qty
this
}
def of(med) {
medicine = med
this
}
def after(dur) {
duration = dur
this
}
}
class IntCategory {
public static int getPills(Integer x) {
x
}
public static int getHours(Integer x) {
x
}
}
String chloroquinine = 'chloroquinine'
def m1 = new DSL()
def m2 = new DSL()
use(IntCategory) {
m1.with {
// tag::commandchain_2[]
// equivalent to: take(2.pills).of(chloroquinine).after(6.hours)
take 2.pills of chloroquinine after 6.hours
// end::commandchain_2[]
}
m2.take(2.pills).of(chloroquinine).after(6.hours)
}
assert m1.qt == 2
assert m1.medicine == 'chloroquinine'
assert m1.duration == 6
assert m1 == m2
'''
assertScript '''import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
class DSL {
String object
List<String> colors = []
def paint(obj) {
object = obj
this
}
def with(String... c) {
colors.addAll(c as List)
this
}
def and(String... c) {
colors.addAll(c as List)
this
}
}
String wall = 'wall'
String red = 'red'
String green = 'green'
String yellow = 'yellow'
def m1 = new DSL()
def m2 = new DSL()
m1.with {
// tag::commandchain_3[]
// equivalent to: paint(wall).with(red, green).and(yellow)
paint wall with red, green and yellow
// end::commandchain_3[]
}
m2.paint(wall).with(red, green).and(yellow)
assert m1 == m2
assert m1.object == 'wall'
assert m1.colors == ['red','green','yellow']
'''
assertScript '''import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
class DSL {
def map = [:]
boolean test
def check(m) { map=m; this}
def tastes(taste) {
test = taste=='good'?map.that=='margarita':false
}
}
def margarita = 'margarita'
def good = 'good'
def m1 = new DSL()
def m2 = new DSL()
m1.with {
// tag::commandchain_4[]
// with named parameters too
// equivalent to: check(that: margarita).tastes(good)
check that: margarita tastes good
// end::commandchain_4[]
}
m2.check(that: margarita).tastes(good)
assert m1 == m2
assert m1.map == [that: 'margarita']
assert m1.test == true
'''
assertScript '''
class DSL {
int count
void cpt(Closure c) { count++ }
def given(Closure c) { cpt(c) ; this }
def when(Closure c) { cpt(c) ; this }
def then(Closure c) { cpt(c) ; this }
}
def m1 = new DSL()
def m2 = new DSL()
m1.with {
// tag::commandchain_5[]
// with closures as parameters
// equivalent to: given({}).when({}).then({})
given { } when { } then { }
// end::commandchain_5[]
}
m2.given({}).when({}).then({})
assert m1.count == 3
assert m2.count == 3
'''
assertScript '''import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
class DSL {
String columns
List names
boolean unique = false
def select(columns) { this.columns = columns; this }
def unique() {
unique = true;
this
}
def from(values) {
names = unique?values.unique():values
}
}
def all = 'all'
def names = ['Bob','Alice','Alice']
def m1 = new DSL()
def m2 = new DSL()
m1.with {
// tag::commandchain_6[]
// equivalent to: select(all).unique().from(names)
select all unique() from names
// end::commandchain_6[]
}
m2.select(all).unique().from(names)
assert m1 == m2
assert (m1.names as Set) == ['Bob','Alice'] as Set
'''
assertScript '''import groovy.transform.Canonical
@Canonical
class DSL {
String thing
int qte
def take(n) {
qte = n
this
}
def propertyMissing(String name) {
thing = name
}
def methodMissing(String name, args) {
if (name.startsWith('get') && !args) {
thing = name.substring(3).toLowerCase()
}
}
}
def m1 = new DSL()
def m2 = new DSL()
def m3 = new DSL()
m1.with {
// tag::commandchain_7[]
// equivalent to: take(3).cookies
// and also this: take(3).getCookies()
take 3 cookies
// end::commandchain_7[]
}
m2.take(3).cookies
m3.take(3).getCookies()
assert m1 == m2
assert m2 == m3
assert m1.qte == 3
assert m1.thing == 'cookies'
'''
}
void testCommandChainImplementation() {
assertScript '''
// tag::commandchain_impl1[]
show = { println it }
square_root = { Math.sqrt(it) }
def please(action) {
[the: { what ->
[of: { n -> action(what(n)) }]
}]
}
// equivalent to: please(show).the(square_root).of(100)
please show the square_root of 100
// ==> 10.0
// end::commandchain_impl1[]
'''
assertScript '''
// tag::commandchain_impl2[]
@Grab('com.google.guava:guava:r09')
import com.google.common.base.*
// end::commandchain_impl2[]
// tag::commandchain_impl2_assert[]
def result = Splitter.on(',').trimResults(CharMatcher.is('_' as char)).split("_a ,_b_ ,c__").iterator().toList()
// end::commandchain_impl2_assert[]
assert result == ['a ', 'b_ ', 'c']
'''
assertScript '''
// tag::commandchain_impl3[]
@Grab('com.google.guava:guava:r09')
import com.google.common.base.*
def split(string) {
[on: { sep ->
[trimming: { trimChar ->
Splitter.on(sep).trimResults(CharMatcher.is(trimChar as char)).split(string).iterator().toList()
}]
}]
}
// end::commandchain_impl3[]
// tag::commandchain_impl3_assert[]
def result = split "_a ,_b_ ,c__" on ',' trimming '_\'
// end::commandchain_impl3_assert[]
'''
}
}