Created
October 24, 2012 14:54
-
-
Save dnoseda/3946536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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