2

我正在尝试在 Meson 中创建一个单元测试目标列表,每个测试用例都从一个源文件构建。源文件在子目录中使用 files() 命令定义:

my_test_files = files(['test_foo.c','test_bar.c','test_baz.c'])

我想做的是在顶级构建中是这样的:

foreach t_file : my_test_files
    t_name = t.split('.')[0]
    test(t_name, executable(t_name, t_file, ...))
endforeach

我知道如果文件名是纯字符串,则可以执行此操作,但上述方法失败并出现“文件对象不可调用”错误。

是否有更“中音”的方式从源文件名派生可执行/测试名称?

4

1 回答 1

3

如果您将变量简单地定义为数组,它应该可以工作,例如:

my_test_files = ['test_foo.c','test_bar.c','test_baz.c']

循环保持不变,除了一些错字修复:

foreach t_file : my_test_files
    t_name = t_file.split('.')[0]
    test(t_name, executable(t_name, t_file, ...))
endforeach

而不是构建文件对象数组。这是因为 executable()接受多种形式的输入文件:作为文件对象(您尝试这样做)和作为字符串,源文件(应该编译)或目标文件(要链接) - 由文件扩展名检测。

为了获得更大的灵活性和更好的控制,可以使用数组数组(当然,它是可扩展的,并且可能包含生成测试所需的任何内容):

foo_files = files('test_foo.c')
bar_files = files('test_bar.c')
baz_files = files('test_baz.c')

test_files = [
  ['foo', foo_files, foo_deps],
  ['bar', bar_files, []],
  ['baz', baz_files, baz_deps]]

foreach t : test_files
    test(t[0], executable(t[0], t[1], dependencies=t[2], ...))
endforeach
于 2019-04-11T15:10:54.947 回答