创建ceylon.test.TestRunner

时间:2017-09-10 20:15:29

标签: unit-testing ceylon

我正在尝试创建一个可以通过编程方式运行的测试套件。 (The documentation确实mention可以取笑IDE进行测试运行,但在我看来,更常规的方法是将测试套件设置为标准的Ceylon模块,它具有自己的{{ 3}}。此外,文档没有说明如何以IDE的方式实现它。

所以,我正在使用runnable unit函数创建TestRunner。所述函数采用createTestRunner s(' TestSource[]')的顺序作为其第一个参数。 TestSource是此类型的别名:

Module|Package|ClassDeclaration|FunctionDeclaration|Class<Anything,Nothing>|FunctionModel<Anything,Nothing>|String

提出问题:我如何将测试提供给测试运行器?

对于初学者来说,最简单的方法是将它们放在本地函数中然后让测试运行器以某种方式访问​​这些函数(未进一步指定)。 由于TestSource别名中包含的长类型列表似乎并不包含实际TestSource,因此我尝试寻找最接近的候选人,看起来是正确的:{{3 }}

为了进行这样的函数声明,我首先要思考我的测试包装函数实际上是什么样子。也许是这样的?

Anything myTests1 () {
    // assert something!
    return null;
}

void myTests2 () {
    // assert some more things!
}

(顺便说一下,Functions

在经过大量FunctionDeclaration后,我认为这些函数的FunctionDeclaration可以拼写出来:

// function name for function declaration:
LIdentifier funName = LIdentifier("myName");

// type of return value for function declaration:
UIdentifier returnTypeName1 = UIdentifier("Anything");
TypeNameWithTypeArguments returnTypeName2 = TypeNameWithTypeArguments(returnTypeName1);
BaseType returnType = BaseType( returnTypeName2 );

// type of parameters for function declaration:
Sequential<Parameter> parameters1 = [];  // our test wrapper functions takes no arguments
Parameters parameters2 = Parameters( parameters1 );
Sequence<Parameters> parameterLists = [parameters2];

// the actual function declaration:
FunctionDeclaration myFunctionDeclaration = FunctionDeclaration(
    funName,
    returnType,
    parameterLists
);

现在,我所要做的就是将其提供给createTestRunner函数。我只需将myFunctionDeclaration放入TestSource[]

TestSource myTestSource = myFunctionDeclaration;
TestSource[] mySourceList = [myTestSource];
TestRunner myTestRunner = createTestRunner(mySourceList);

但是第一行并不奏效。 myFunctionDeclaration类型&#39;功能声明&#39;根本就没有以TestSource类型传递。为什么不? FunctionDeclaration不是正确的TestSource类型吗?查看TestSource的别名定义,FunctionDeclaration似乎在可能的类型列表中就在那里

Module|Package|ClassDeclaration|FunctionDeclaration|Class<Anything,Nothing>|FunctionModel<Anything,Nothing>|String

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

这是一个FunctionDeclaration字面值:

`function myTests1`

(与Function相对,`myTests1`没有关键字。第一个是ceylon.language.meta.declaration中的 detyped 模型,第二个是静态类型ceylon.language.meta.model中的模型。请参阅Tour, The metamodel。)

所以我认为你应该为测试跑步者做的是:

value myTestRunner = createTestRunner([`function myTests1`, `function myTests2`]);

(但我自己从未这样做过。)

您在Herd上找到的是ceylon.ast,这是一组完全不相关的模块,可让您描述 Ceylon源代码。您的myFunctionDeclaration描述了函数的抽象语法树

Anything myName();

但仅限于语法级别:函数永远不会被编译。元模型的东西你不需要ceylon.ast。 (另请注意,这是一个函数声明,而不是函数定义。它在语法上是有效的,但不会被typechecker接受,因为它没有注释{{1} }。)

作为旁注,formal模块提供了更方便的实例化ceylon.ast.create节点的方法(而不是像你一样直接使用该模块):

ceylon.ast.core