1

我正在尝试创建一个可以以编程方式运行的测试套件。(文档确实提到可以让 IDE 进行测试运行,但在我看来,将测试套件设置为具有自己的可运行单元的标准 Ceylon 模块似乎是一种更常规的方法。此外,文档没有没有说明如何以 IDE 方式实际执行此操作)。

所以,我正在使用createTestRunner函数创建一个TestRunner 。所述函数将TestSource s (' ') 的 Sequential 作为其第一个参数。是这种类型的别名:TestSource[]TestSource

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

这提示了一个问题:我想如何将我的测试提供给测试运行器?

对于初学者来说,似乎最容易将它们放在本地函数中,然后让测试运行程序以某种方式访问​​这些函数(未进一步指定)。由于别名中包含的一长串类型TestSource似乎不包括实际的Functions,因此我尝试寻找最近的候选对象:FunctionDeclaration

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

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

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

(顺便说一下,这些函数在类型上是等效的)

经过大量Ceylon Herd 审查后,我认为FunctionDeclaration此类函数的 a 可以这样拼写:

// 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'FunctionDeclaration' 类型的类型根本不会作为TestSource类型传递。为什么不?不是FunctionDeclaration正确的 TestSource 类型吗?查看 的别名定义TestSourceFunctionDeclaration似乎就可能的类型列表中:

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

我在这里想念什么?

4

1 回答 1

1

这是一个FunctionDeclaration字面意思:

`function myTests1`

(与Function没有`myTests1`关键字的 a 不同。第一个是 中的类型模型ceylon.language.meta.declaration,第二个是 中的静态类型模型ceylon.language.meta.model。参见Tour, The metamodel。)

所以我认为你应该为测试运行者做的是:

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

(但我自己从来没有这样做过。)

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

Anything myName();

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

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

value fun = functionDefinition {
    name = "myName";
    type = baseType("Anything");
};
于 2017-09-10T20:43:29.533 回答