绑定和关闭groovy [英] binding and closures groovy

查看:150
本文介绍了绑定和关闭groovy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用绑定与闭包在grooy.I写了一个测试代码,当运行它,它说,缺少方法setBinding作为参数的闭包。

  void testMeasurement(){
prepareData(someClosure)
}
def someClosure = {
assertEquals(apple,a)
}


void prepareData(testCase){
def binding = new Binding()
binding.setVariable(a,apple)
testCase.setBinding(binding)
testCase.call()

}


解决方案

这适用于我与Groovy 1.7.3:

  someClosure = {
assertapple== a
}
void testMeasurement(){
prepareData(someClosure)
}
void prepareData(testCase){
def binding = new Binding()
binding.setVariable(a,apple)
testCase.setBinding )
testCase.call()
}
testMeasurement()

在这个脚本示例中,setBinding调用在脚本绑定中设置 a (您可以看到从Closure文档,没有setBinding调用)。所以在setBinding调用后,可以调用

  println a 

并且会打印出apple



所以要在类中设置委托对于闭包(当在本地不能找到属性时,闭包将恢复到此委托),如下所示:

  class TestClass { 
void testMeasurement(){
prepareData(someClosure)
}

def someClosure = { - >
assertapple== a
}

void prepareData(testCase){
def binding = new Binding()
binding.setVariable a,apple)
testCase.delegate = binding
testCase.call()
}
}

并且它应该从代理类中获取 a 的值(在这种情况下,绑定)



此页在这里通过使用委托和变量的范围在Closures



确实,而不是使用一个Binding对象,你应该能使用一个简单的Map如下:

  void prepareData(testCase){
testCase.delegate = [a: apple']
testCase.call()
}

希望它有帮助!


I dont know how to use binding with closures in grooy.I wrote a test code and while running it, it said, missing method setBinding on the closure passed as parameter.

void testMeasurement() {
    prepareData(someClosure)
}
def someClosure = {
  assertEquals("apple", a)
}


  void prepareData(testCase) {
    def binding = new Binding()
    binding.setVariable("a", "apple")
    testCase.setBinding(binding)
    testCase.call()

  }

解决方案

This works for me with Groovy 1.7.3:

someClosure = {
  assert "apple" == a
}
void testMeasurement() {
  prepareData(someClosure)
}
void prepareData(testCase) {
  def binding = new Binding()
  binding.setVariable("a", "apple")
  testCase.setBinding(binding)
  testCase.call()
}
testMeasurement()

In this script example, the setBinding call is setting a in the scripts binding (as you can see from the Closure documentation, there is no setBinding call). So after the setBinding call, you can call

println a

and it will print out "apple"

So to do this in the class, you can set the delegate for the closure (the closure will revert back to this delegate when a property cannot be found locally) like so:

class TestClass {
  void testMeasurement() {
    prepareData(someClosure)
  }

  def someClosure = { ->
    assert "apple" == a
  }

  void prepareData( testCase ) {
    def binding = new Binding()
    binding.setVariable("a", "apple")
    testCase.delegate = binding
    testCase.call()
  }
}

And it should grab the value fro a from the delegate class (in this case, a binding)

This page here goes through the usage of delegate and the scope of variables in Closures

Indeed, instead of using a Binding object, you should be able to use a simple Map like so:

  void prepareData( testCase ) {
    testCase.delegate = [ a:'apple' ]
    testCase.call()
  }

Hope it helps!

这篇关于绑定和关闭groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆