2

我正在为 Cucumber-JVM 实现 Groovy 步骤定义,我希望一个步骤能够自行存储,以便下一步可以重复 n 次。

Given(~'the (\\S+) is in the blender') { String thing ->
    // do stuff...
    context.repeatable = self.curry(thing)
}

上面代码中的“self”应该是什么?

我不能使用“this”,因为它指的是封闭对象(在这种情况下是什么,可能是脚本)。

4

2 回答 2

1

由于curryClosure类的一个方法,直接调用 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
于 2013-03-29T20:37:49.237 回答
0

您可以尝试使用未引用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()
}
于 2013-03-29T19:50:14.587 回答