gpt4 book ai didi

testing - 我如何测试导入的 Perl 6 例程是否存在?

转载 作者:行者123 更新时间:2023-11-28 19:56:59 25 4
gpt4 key购买 nike

我想检查当前范围内是否未定义可导出子例程,我还想测试未定义子例程。这两种情况都应该在不运行代码的情况下发生,我真的很喜欢一种无需 EVAL 的方法。我在 roast 中找到的示例执行了测试人员假定已经存在的子例程。

这是我拼凑的东西,但对于我希望能够检查事物的语言来说,它主要是不令人满意的:

use Test;

module Foo {
sub this-exists is export { say "This exists"; return 1 }
sub this-is-a-stub is export { !!! }
sub this-is-a-todo is export { ... }
sub not-exported { say "Not exported" }
}

import Foo; # https://github.com/perl6/doc/issues/359

# Is the class there?
ok ::("Foo") !~~ Failure, "module Foo is defined";
ok ::("Bar") ~~ Failure, "module Bar is not defined";

# this should work, but note the first one compiles because it works.
# if the routine isn't there, the &some-name is a compilation error.
ok &this-exists, "this-exists is there";
ok &("not-there"), "this-exists is there";

# these are exported but they aren't "defined" because they
# are stubs. How is this supposed to work?
dies-ok { EVAL Q/&this-is-a-stub/ }, "this-is-a-stub is not defined";
nok &("this-is-a-stub"), "this-is-a-stub is not defined";
nok &("this-is-a-todo"), "this-is-a-todo is not defined";

# these shouldn't be defined here
dies-ok { EVAL Q/ &not-exported / }, "not-exported is not defined";
dies-ok { EVAL Q/ &not-there / }, "not-there is not defined";

done-testing();

最佳答案

say so ::('&non-existent'); # False

say so ::('&say'); # True

if ::('&say') -> &say {
say 4; # 4␤
}

请注意,这些也适用于引用可能存在或可能不存在的子例程。

&::('say')
::{'&say','&put'}
::<&say &put> # similar to previous line

&CORE::('say')
CORE::('&say')
CORE::{'&say','&put'}
CORE::<&say &put> # similar to previous line

此外,&(…) 用于将某些内容强制转换为代码类型对象,因此仅适用于已经是 Callable 或具有 CALL-我方法。

关于testing - 我如何测试导入的 Perl 6 例程是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com