Groovy 删除字符串参数中的引号

Groovy Removing Quotes in String Parameters

我正在尝试实现一个简单的 DSL 来安排事件。我将不得不在结束语中加上不带引号的“安排会议”,它应该安排一次会议。 “安排约会”将安排约会等。但是,我无法在闭包中去掉引号,因为它将 'meeting' 注册为 属性,并给我 groovy.lang.MissingPropertyException: No such property: meeting for class: groovy.SchedulerTest

我知道我们可以在 Kotlin 中用“中缀”标记函数,但不确定如何在 Groovy 中模拟它。

这是 DSL 的 class:

class Scheduler {
  //var meeting = "meeting" //I could uncomment this and it would work, but then I would need to create a new variable for every type of meeting
  String type

  def static create(closure) {
    Scheduler scheduler = new Scheduler()
    closure.delegate = scheduler
    closure()
    return scheduler.print_values()
  }

  def schedule(String type) {
    this.type = type
  }
  
  def print_values() {
    return "${this.type} scheduled"
  }
}

这是我要通过的测试用例:

class SchedulerTest extends Specification {

  def "Scheduler Type Test"() {
    setup:
    String type_test = Scheduler.create {
      schedule meeting //it runs just fine if I write -> schedule "meeting". But I am not allowed to use any quotes as my requirements.
    }

    expect:
      type_test == "meeting scheduled"
  }

}

如果您将以下方法添加到 Scheduler class:

,您的测试将通过
def propertyMissing(String propertyName) {
    propertyName
}