【问题标题】:How to programmatically inject faults into Pharo methods?如何以编程方式将故障注入 Pharo 方法?
【发布时间】:2014-01-20 09:42:22
【问题描述】:

我想将错误注入到 Pharo 方法中。类似的东西

  • ifTrue:ifFalse:更改为ifFalse:ifTrue:
  • +更改为-
  • and:更改为or:

等等

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

【问题讨论】:

    标签: testing smalltalk pharo fault


    【解决方案1】:

    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
    

    【讨论】:

    • 在 Pharo 3 中工作,非常酷!谢谢! (在 Pharo 2 中,RBMethodNode 还不理解#generate:但这只是一个注释,我对 Pharo 3 很好)
    【解决方案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

    【讨论】:

      猜你喜欢
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多