创建 ceylon.test.TestRunner

Creating a ceylon.test.TestRunner

我正在尝试创建一个我可以 运行 编程的测试套件。 (The documentation does mention that one can tease an IDE into doing the test running, but it seems to me a more regular approach to set up the test suite as a standard Ceylon module that has its own runnable unit。此外,文档没有说明如何以 IDE 方式实际执行此操作)。

所以,我正在创建一个 TestRunner using the createTestRunner function. Said function takes a Sequential of TestSources ('TestSource[]') 作为它的第一个参数。 TestSource 是此类型的别名:

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

这引发了问题:我想如何将我的测试提供给测试 运行ner?

对于初学者来说,将它们放在本地函数中似乎最简单,然后让测试 运行ner 以某种方式访问​​这些函数(未进一步指定)。 由于 TestSource 别名中包含的一长串类型似乎不包括实际的 Functions, I tried to go for the nearest candidate that looked like the right thing: FunctionDeclaration.

为了做出这样的函数声明,我首先不得不考虑我的测试包装函数实际上是什么样子的。也许是这样的?

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

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

(these functions are typewise equivalent, 顺便说一下)

经过大量的Ceylon Herd scrutiny,我认为这样的函数的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);

但是第一行不起作用。 'FunctionDeclaration' 类型的 myFunctionDeclaration 根本不会作为 TestSource 类型传递。为什么不? FunctionDeclaration 不是正确的 TestSource 类型吗?查看 TestSource 的别名定义,FunctionDeclaration 似乎 就在 可能类型列表中:

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

我在这里错过了什么?

这是一个 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 元模型的东西。 (还要注意,这是一个函数 declaration,而不是函数 definition。它在语法上是有效的,但不会被类型检查器接受,因为它是未注释 formal.)

附带说明一下,ceylon.ast.create 模块提供了更方便的方法来实例化 ceylon.ast.core 节点(而不是像您那样直接使用该模块):

value fun = functionDefinition {
    name = "myName";
    type = baseType("Anything");
};