I am trying to implement a simple DSL for scheduling events. I will have to put "schedule meeting" without quotes in a closure, and it should schedule a meeting. "schedule appointment" will schedule an appointment, etc. However, I cannot get rid of the quotes in my closure, as it registers 'meeting' as a property, and gives me groovy.lang.MissingPropertyException: No such property: meeting for class: groovy.SchedulerTest
.
I know we can mark the function with "infix" in Kotlin, but not exactly sure how I could simulate that in Groovy.
Here is the class for DSL:
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"
}
}
Here is the test case I am trying to pass:
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"
}
}
Your test would pass if you added the following method to the Scheduler
class:
def propertyMissing(String propertyName) {
propertyName
}