问题描述
如果他们的实现在Smalltalk中至少出现两次“ assert”一词,我想获得类的方法列表。
有人可以帮我吗?预先感谢!
解决方法
我不确定gnu-Smalltalk的详细信息,但是在Pharo中,您可以执行以下操作:
YourClass methods select: [ :method |
method sourceCode matchesRegex: '.*assert.*assert.*'. ]
在这里,我使用一个简单的正则表达式来查看我是否可以在源代码中匹配两个“断言”字。
但是,借助Smalltalk,可以轻松进行更精确的搜索。图像,您想查看一个方法是否发送至少两个assert:
消息。您可以通过以下方式找到此类方法:
YourClass methods select: [ :method |
| numAsserts |
numAsserts := method ast allChildren count: [ :node |
node isMessage and: [ node selector = #assert: ] ].
numAsserts >= 2
]
在上面的示例中,对于每种方法,我们只需计算消息发送的AST节点的数量,并使用assert:
选择器。然后我们检查这些节点的数量是否大于或等于2。