如何以编程方式将故障注入Pharo方法?

时间:2014-01-20 09:42:23

标签: testing smalltalk pharo fault

我想向Pharo方法注入错误。像

这样的东西
  • ifTrue:ifFalse:更改为ifFalse:ifTrue:
  • +更改为-
  • and:更改为or:

我怎么能这样做? Pharo 3中有新的/其他可能性吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

曾经有过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