Skip to content

Instantly share code, notes, and snippets.

@dnoseda
Created October 24, 2012 14:54
Show Gist options
  • Save dnoseda/3946536 to your computer and use it in GitHub Desktop.
Save dnoseda/3946536 to your computer and use it in GitHub Desktop.
def model = [model:[book:[title:"The Shinning"]]]
def magicMethod= { String exp ->
def out = [:]
out.result = Eval.x(model,"x.with{${exp}}")
try{
if(out.result){
Eval.x(model,"x.with{assert !(${exp})}")
}else{
Eval.x(model,"x.with{assert ${exp}}")
}
}catch(org.codehaus.groovy.runtime.powerassert.PowerAssertionError e){
//out.detail = e.getMessage()
def extra =""
if(e.getMessage().contains("!(")){
extra = "!("
out.detail = e.getMessage().split("\n")[0..-2]
}else{
out.detail = e.getMessage().split("\n")
}
out.detail = out.detail.collect({ it.substring(" ${extra}".length()) }).join("\n")
}
return out
}
def expression = "model.book.title == \"The Shinning\""
def output = magicMethod(expression)
println "result: ${output.result}"
println "detail:\n${output.detail}"
expression = "model.book.title == \"what ever\""
output = magicMethod(expression)
println "result: ${output.result}"
println "detail:\n${output.detail}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment