2012-10-22 28 views
1

腳本執行的詳細信息,我發現here的我想要的一個很好的例子:如何獲取關於在常規

enter image description here

基本上就能夠執行字符串作爲與Groovy腳本表達式,但如果條件爲假,我想顯示爲什麼評估爲false的詳細信息。

編輯

我想要的工作,像這樣的實用方法:

def expression = "model.book.title == \"The Shining\"" 
def output = magicMethod(expression) 

// output.result: the exact result of executing expression 
// output.detail: could be a string telling me why this expression returns true or false, similar to de image 

我認爲這可能是Eval.me + assert組合和捕獲異常,以獲取詳細信息

+2

所以你想要'assert'關鍵字產生的消息,但不能只使用'assert'? –

回答

1

是的,它與斷言,謝謝你的想法@Justin Piper

這裏是摘錄:

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(Throwable e){ 
     out.detail = e.getMessage() 
    } 
    return out 
} 


def expression = "model.book.title == \"The Shining\"" 
def output = magicMethod(expression) 

println "result: ${output.result}" 
println "detail: ${output.detail}"