[英]How to programmatically inject faults into Pharo methods?
我想將錯誤注入到Pharo方法中。 就像是
ifTrue:ifFalse:
更改為ifFalse:ifTrue:
+
更改為-
, and:
至or:
, 等等
我該怎么辦? Pharo 3中是否有新的/其他可能性?
Pharo 4將為此提供解決方案,但現在您必須手動完成...
對於#+,#-等普通消息,您可以修改方法的文字數組,但不適用於#ifTrue:ifFalse:和#ifFalse:ifTrue:等消息,因為編譯器會內聯代碼以獲得更好的性能。 一種解決方案是復制該方法的AST,對其進行修改,對其進行編譯並將其安裝在類中。 這樣的事情應該起作用:
FaultInjector>>#replace: aSelector with: anotherSelector in: aCompiledMethod
| newAST |
"Save the original method to restore it later"
replacedMethods add: aCompiledMethod.
"Copy the AST"
newAST := aCompiledMethod ast copy.
"Rewriting the AST"
newAST nodesDo: [ :each |
(each isMessage and: [ each selector = aSelector ])
ifTrue: [ each selector: anotherSelector ] ].
"Compile the AST and install the new method"
aCompiledMethod methodClass
addSelectorSilently: aCompiledMethod selector
withMethod: (newAST generate: aCompiledMethod trailer).
然后,您應該有一種方法來還原替換的方法:
FaultInjector>>#restore
replacedMethods do: [ :each |
each methodClass
addSelectorSilently: each selector
withMethod: each ].
replacedMethods removeAll
曾經有這樣的Squeak框架,請搜索MutationTesting
http://www.slideshare.net/esug/mutation-testing
http://www.squeaksource.com/MutationTesting.html
我懷疑它是否可以在Pharo 2.0 / 3.0中正常工作,並且我不知道是否已經有Pharo端口,但這可能值得嘗試,無論如何它應該是一個不錯的起點。
同時從Hasso Plattner Institute搜索MutationEngine
http://lists.squeakfoundation.org/pipermail/squeak-dev/2012-October/166011.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.