我正在为 Cucumber-JVM 实现 Groovy 步骤定义,我希望一个步骤能够自行存储,以便下一步可以重复 n 次。
Given(~'the (\\S+) is in the blender') { String thing ->
// do stuff...
context.repeatable = self.curry(thing)
}
上面代码中的“self”应该是什么?
我不能使用“this”,因为它指的是封闭对象(在这种情况下是什么,可能是脚本)。
由于curry
是Closure类的一个方法,直接调用 curry 适用于闭包,如果它被命名:
def context
test = { String thing ->
context = curry(thing)
thing.toUpperCase()
}
test('hello')
println context.call()
test('world')
println context.call()
=>
HELLO
WORLD
或匿名:
def context
['test'].each { String thing ->
context = curry(thing)
thing.toUpperCase()
}
println context.call()
=>
TEST
您可以尝试使用未引用curry
的方法传递接收到的参数:
clos = { String text ->
if (text) {
println "text=$text"
a = curry null
a()
} else {
println "done"
}
}
clos "closure text"
将打印:
text=closure text
done
更新
您还可以使用clone()
:
closure = {
def clone = clone()
}