SmallJava 语言的类型检查与验证
1. 实用方法与语法测试
在 SmallJava 中, SJClass 对象的特征成员包含 SJField 和 SJMethod 实例。为了能根据类型快速选择它们,编写实用方法很有必要。这些方法会作为扩展方法用于其他 Xtend 类,这样就能编写如 c.methods 和 c.fields 这样的表达式。快速访问返回语句的方法在为 DSL 编写单元测试时也很有用。
在编写 DSL 语法规则后,应立即为解析器编写单元测试。下面展示了一些有趣的测试用例,特别是关于表达式结合性的测试,如赋值和成员选择:
import static extension org.junit.Assert.*
@RunWith(XtextRunner)
@InjectWith(SmallJavaInjectorProvider)
class SmallJavaParsingTest {
@Inject extension ParseHelper<SJProgram>
@Inject extension SmallJavaModelUtil
@Test def void testMemberSelectionLeftAssociativity() {
'''
class A {
A m() { return this.m().m(); }
}
'''.parse.classes.head.methods.head.
超级会员免费看
订阅专栏 解锁全文
1237

被折叠的 条评论
为什么被折叠?



